switch to scc's serde implementation

This commit is contained in:
slonkazoid 2024-04-30 09:38:50 +03:00
parent 0b762a36f3
commit 91b48850db
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
3 changed files with 4 additions and 64 deletions

1
Cargo.lock generated
View file

@ -1748,6 +1748,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec96560eea317a9cc4e0bb1f6a2c93c09a19b8c4fc5cb3fcc0ec1c094cd783e2" checksum = "ec96560eea317a9cc4e0bb1f6a2c93c09a19b8c4fc5cb3fcc0ec1c094cd783e2"
dependencies = [ dependencies = [
"sdd", "sdd",
"serde",
] ]
[[package]] [[package]]

View file

@ -25,7 +25,7 @@ comrak = { version = "0.22.0", features = ["syntect"] }
console-subscriber = { version = "0.2.0", optional = true } console-subscriber = { version = "0.2.0", optional = true }
fronma = "0.2.0" fronma = "0.2.0"
notify = "6.1.1" notify = "6.1.1"
scc = "2.1.0" scc = { version = "2.1.0", features = ["serde"] }
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
syntect = "5.2.0" syntect = "5.2.0"
thiserror = "1.0.58" thiserror = "1.0.58"

View file

@ -1,9 +1,7 @@
use std::hash::{DefaultHasher, Hash, Hasher}; use std::hash::{DefaultHasher, Hash, Hasher};
use scc::HashMap; use scc::HashMap;
use serde::de::Visitor; use serde::{Deserialize, Serialize};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::config::RenderConfig; use crate::config::RenderConfig;
use crate::post::PostMetadata; use crate::post::PostMetadata;
@ -16,14 +14,10 @@ pub struct CacheValue {
config_hash: u64, config_hash: u64,
} }
#[derive(Default, Clone)] #[derive(Serialize, Deserialize, Default, Clone)]
pub struct Cache(HashMap<String, CacheValue>); pub struct Cache(HashMap<String, CacheValue>);
impl Cache { impl Cache {
pub fn from_map(cache: HashMap<String, CacheValue>) -> Self {
Self(cache)
}
pub async fn lookup( pub async fn lookup(
&self, &self,
name: &str, name: &str,
@ -101,59 +95,4 @@ impl Cache {
pub async fn remove(&self, name: &str) -> Option<(String, CacheValue)> { pub async fn remove(&self, name: &str) -> Option<(String, CacheValue)> {
self.0.remove_async(name).await self.0.remove_async(name).await
} }
#[inline(always)]
pub fn into_inner(self) -> HashMap<String, CacheValue> {
self.0
}
}
impl Serialize for Cache {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let cache = self.clone().into_inner();
let mut map = serializer.serialize_map(Some(cache.len()))?;
let mut entry = cache.first_entry();
while let Some(occupied) = entry {
map.serialize_entry(occupied.key(), occupied.get())?;
entry = occupied.next();
}
map.end()
}
}
impl<'de> Deserialize<'de> for Cache {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CoolVisitor;
impl<'de> Visitor<'de> for CoolVisitor {
type Value = Cache;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "expected a map")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let cache = match map.size_hint() {
Some(size) => HashMap::with_capacity(size),
None => HashMap::new(),
};
while let Some((key, value)) = map.next_entry::<String, CacheValue>()? {
cache.insert(key, value).ok();
}
Ok(Cache::from_map(cache))
}
}
deserializer.deserialize_map(CoolVisitor)
}
} }