From 086ddb76654e4e14be77a1e71203b3124d792515 Mon Sep 17 00:00:00 2001 From: slonkazoid Date: Thu, 2 May 2024 17:41:06 +0300 Subject: [PATCH] remove arc unwrapping --- src/main.rs | 20 +++++++------------- src/post/mod.rs | 10 ++++++---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index d94fcab..ea7acae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -316,30 +316,24 @@ async fn main() -> eyre::Result<()> { } // write cache to file - let AppState { config, posts } = Arc::::try_unwrap(state).unwrap_or_else(|state| { - warn!("couldn't unwrap Arc over AppState, more than one strong reference exists for Arc. cloning instead"); - // TODO: only do this when persistence is enabled - // first check config from inside the arc, then try unwrap - AppState::clone(state.as_ref()) - }); + let config = &state.config; + let posts = &state.posts; if config.cache.enable && config.cache.persistence - && let Some(cache) = posts.into_cache() + && let Some(cache) = posts.cache() { let path = &config.cache.file; - let serialized = bitcode::serialize(&cache).context("failed to serialize cache")?; + let serialized = bitcode::serialize(cache).context("failed to serialize cache")?; let mut cache_file = tokio::fs::File::create(path) .await .with_context(|| format!("failed to open cache at {}", path.display()))?; + let compression_level = config.cache.compression_level; if config.cache.compress { let cache_file = cache_file.into_std().await; tokio::task::spawn_blocking(move || { std::io::Write::write_all( - &mut zstd::stream::write::Encoder::new( - cache_file, - config.cache.compression_level, - )? - .auto_finish(), + &mut zstd::stream::write::Encoder::new(cache_file, compression_level)? + .auto_finish(), &serialized, ) }) diff --git a/src/post/mod.rs b/src/post/mod.rs index ceed862..0ecee71 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -151,7 +151,7 @@ impl PostManager { Ok((metadata, post, (parsing, rendering))) } - pub async fn list_posts( + pub async fn get_all_post_metadata_filtered( &self, filter: impl Fn(&PostMetadata) -> bool, ) -> Result, PostError> { @@ -206,7 +206,9 @@ impl PostManager { tag: Option<&String>, ) -> Result, PostError> { let mut posts = self - .list_posts(|metadata| !tag.is_some_and(|tag| !metadata.tags.contains(tag))) + .get_all_post_metadata_filtered(|metadata| { + !tag.is_some_and(|tag| !metadata.tags.contains(tag)) + }) .await?; posts.sort_unstable_by_key(|metadata| metadata.created_at.unwrap_or_default()); @@ -257,8 +259,8 @@ impl PostManager { } } - pub fn into_cache(self) -> Option { - self.cache + pub fn cache(&self) -> Option<&Cache> { + self.cache.as_ref() } pub async fn cleanup(&self) {