remove arc unwrapping

This commit is contained in:
slonkazoid 2024-05-02 17:41:06 +03:00
parent 3be39df282
commit 086ddb7665
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
2 changed files with 13 additions and 17 deletions

View file

@ -316,29 +316,23 @@ async fn main() -> eyre::Result<()> {
}
// write cache to file
let AppState { config, posts } = Arc::<AppState>::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,
)?
&mut zstd::stream::write::Encoder::new(cache_file, compression_level)?
.auto_finish(),
&serialized,
)

View file

@ -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<Vec<PostMetadata>, PostError> {
@ -206,7 +206,9 @@ impl PostManager {
tag: Option<&String>,
) -> Result<Vec<PostMetadata>, 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<Cache> {
self.cache
pub fn cache(&self) -> Option<&Cache> {
self.cache.as_ref()
}
pub async fn cleanup(&self) {