Compare commits

..

No commits in common. "44a4b6a365bf15e72450646e9b4dcaa15625817f" and "65cfe8b28fe116d7ab630ba77186c555d79cdd57" have entirely different histories.

3 changed files with 30 additions and 28 deletions

View file

@ -87,8 +87,8 @@ pub async fn load() -> Result<Config> {
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf) file.read_to_string(&mut buf)
.await .await
.context("couldn't read configuration file")?; .with_context(|| "couldn't read configuration file")?;
toml::from_str(&buf).context("couldn't parse configuration") toml::from_str(&buf).with_context(|| "couldn't parse configuration")
} }
Err(err) => match err.kind() { Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => { std::io::ErrorKind::NotFound => {
@ -104,7 +104,7 @@ pub async fn load() -> Result<Config> {
Ok(mut file) => file Ok(mut file) => file
.write_all( .write_all(
toml::to_string_pretty(&config) toml::to_string_pretty(&config)
.context("couldn't serialize configuration")? .with_context(|| "couldn't serialize configuration")?
.as_bytes(), .as_bytes(),
) )
.await .await

View file

@ -141,7 +141,7 @@ async fn main() -> eyre::Result<()> {
let config = config::load() let config = config::load()
.await .await
.context("couldn't load configuration")?; .with_context(|| "couldn't load configuration")?;
let mut tasks = JoinSet::new(); let mut tasks = JoinSet::new();
let mut cancellation_tokens = Vec::new(); let mut cancellation_tokens = Vec::new();
@ -154,7 +154,7 @@ async fn main() -> eyre::Result<()> {
let compressed = tokio::task::spawn_blocking(|| compress_epicly("static")) let compressed = tokio::task::spawn_blocking(|| compress_epicly("static"))
.await .await
.unwrap() .unwrap()
.context("couldn't compress static")?; .with_context(|| "couldn't compress static")?;
let _handle = span.enter(); let _handle = span.enter();
@ -170,7 +170,7 @@ async fn main() -> eyre::Result<()> {
tasks.spawn(async move { tasks.spawn(async move {
watch(span, passed_token, Default::default()) watch(span, passed_token, Default::default())
.await .await
.context("failed to watch static") .with_context(|| "failed to watch static")
.unwrap() .unwrap()
}); });
cancellation_tokens.push(token); cancellation_tokens.push(token);
@ -186,14 +186,14 @@ async fn main() -> eyre::Result<()> {
let load_cache = async { let load_cache = async {
let mut cache_file = tokio::fs::File::open(&path) let mut cache_file = tokio::fs::File::open(&path)
.await .await
.context("failed to open cache file")?; .with_context(|| "failed to open cache file")?;
let mut serialized = Vec::with_capacity(4096); let mut serialized = Vec::with_capacity(4096);
cache_file cache_file
.read_to_end(&mut serialized) .read_to_end(&mut serialized)
.await .await
.context("failed to read cache file")?; .with_context(|| "failed to read cache file")?;
let cache = let cache = bitcode::deserialize(serialized.as_slice())
bitcode::deserialize(serialized.as_slice()).context("failed to parse cache")?; .with_context(|| "failed to parse cache")?;
Ok::<PostManager, color_eyre::Report>(PostManager::new_with_cache( Ok::<PostManager, color_eyre::Report>(PostManager::new_with_cache(
config.posts_dir.clone(), config.posts_dir.clone(),
config.render.clone(), config.render.clone(),
@ -260,7 +260,7 @@ async fn main() -> eyre::Result<()> {
})?; })?;
let local_addr = listener let local_addr = listener
.local_addr() .local_addr()
.context("couldn't get socket address")?; .with_context(|| "couldn't get socket address")?;
info!("listening on http://{}", local_addr); info!("listening on http://{}", local_addr);
let sigint = signal::ctrl_c(); let sigint = signal::ctrl_c();
@ -284,7 +284,7 @@ async fn main() -> eyre::Result<()> {
tokio::select! { tokio::select! {
result = &mut server => { result = &mut server => {
result.context("failed to serve app")?; result.with_context(|| "failed to serve app")?;
}, },
_ = sigint => { _ = sigint => {
info!("received SIGINT, exiting gracefully"); info!("received SIGINT, exiting gracefully");
@ -299,9 +299,9 @@ async fn main() -> eyre::Result<()> {
for token in cancellation_tokens { for token in cancellation_tokens {
token.cancel(); token.cancel();
} }
server.await.context("failed to serve app")?; server.await.with_context(|| "failed to serve app")?;
while let Some(task) = tasks.join_next().await { while let Some(task) = tasks.join_next().await {
task.context("failed to join task")?; task.with_context(|| "failed to join task")?;
} }
// write cache to file // write cache to file
@ -311,14 +311,15 @@ async fn main() -> eyre::Result<()> {
}); });
if let Some(path) = config.cache_file.as_ref() { if let Some(path) = config.cache_file.as_ref() {
let cache = posts.into_cache(); let cache = posts.into_cache();
let mut serialized = bitcode::serialize(&cache).context("failed to serialize cache")?; let mut serialized =
bitcode::serialize(&cache).with_context(|| "failed to serialize cache")?;
let mut cache_file = tokio::fs::File::create(path) let mut cache_file = tokio::fs::File::create(path)
.await .await
.with_context(|| format!("failed to open cache at {}", path.display()))?; .with_context(|| format!("failed to open cache at {}", path.display()))?;
cache_file cache_file
.write_all(serialized.as_mut_slice()) .write_all(serialized.as_mut_slice())
.await .await
.context("failed to write cache to file")?; .with_context(|| "failed to write cache to file")?;
info!("wrote cache to {}", path.display()); info!("wrote cache to {}", path.display());
} }
Ok::<(), color_eyre::Report>(()) Ok::<(), color_eyre::Report>(())
@ -335,7 +336,7 @@ async fn main() -> eyre::Result<()> {
tokio::select! { tokio::select! {
result = cleanup => { result = cleanup => {
result.context("cleanup failed, oh well")?; result.with_context(|| "cleanup failed, oh well")?;
}, },
_ = sigint => { _ = sigint => {
warn!("received second signal, exiting"); warn!("received second signal, exiting");

View file

@ -1,9 +1,8 @@
use std::hash::{DefaultHasher, Hash, Hasher}; use std::hash::{DefaultHasher, Hash, Hasher};
use scc::HashMap; use scc::HashMap;
use serde::de::Visitor; use serde::de::{SeqAccess, Visitor};
use serde::ser::SerializeMap; use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::config::RenderConfig; use crate::config::RenderConfig;
use crate::post::PostMetadata; use crate::post::PostMetadata;
@ -114,13 +113,15 @@ impl Serialize for Cache {
S: Serializer, S: Serializer,
{ {
let cache = self.clone().into_inner(); let cache = self.clone().into_inner();
let mut map = serializer.serialize_map(Some(cache.len()))?; let mut seq = serializer.serialize_seq(Some(cache.len()))?;
let mut entry = cache.first_entry(); let mut entry = cache.first_entry();
while let Some(occupied) = entry { while let Some(occupied) = entry {
map.serialize_entry(occupied.key(), occupied.get())?; let key = occupied.key().clone();
let value = occupied.get().clone();
seq.serialize_element(&(key, value))?;
entry = occupied.next(); entry = occupied.next();
} }
map.end() seq.end()
} }
} }
@ -137,16 +138,16 @@ impl<'de> Deserialize<'de> for Cache {
write!(formatter, "meow") write!(formatter, "meow")
} }
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where where
A: serde::de::MapAccess<'de>, A: SeqAccess<'de>,
{ {
let cache = match map.size_hint() { let cache = match seq.size_hint() {
Some(size) => HashMap::with_capacity(size), Some(size) => HashMap::with_capacity(size),
None => HashMap::new(), None => HashMap::new(),
}; };
while let Some((key, value)) = map.next_entry::<String, CacheValue>()? { while let Some((key, value)) = seq.next_element::<(String, CacheValue)>()? {
cache.insert(key, value).ok(); cache.insert(key, value).ok();
} }
@ -154,6 +155,6 @@ impl<'de> Deserialize<'de> for Cache {
} }
} }
deserializer.deserialize_map(CoolVisitor) deserializer.deserialize_seq(CoolVisitor)
} }
} }