Moved complete save-load-logic to unshared site info

This commit is contained in:
p11 2025-05-31 14:58:33 +02:00
parent 9016a32b59
commit 1af69ca8e5

View File

@ -49,6 +49,7 @@ struct SharedSiteInfo {
down: AtomicUsize,
}
#[derive(Default)]
struct SiteInfo {
comments: Vec<Comment>,
visits: usize,
@ -57,6 +58,22 @@ struct SiteInfo {
}
impl SharedSiteInfo {
fn new(info: SiteInfo) -> Self {
let SiteInfo {
comments,
visits,
up,
down,
} = info;
Self {
comments: Mutex::new(comments),
visits: visits.into(),
up: up.into(),
down: down.into(),
}
}
fn update(
self: &Arc<Self>,
name: Option<Box<str>>,
@ -199,30 +216,28 @@ impl<S: SizeSettings> ToStream<S> for SiteInfo {
}
}
impl<S: SizeSettings> FromStream<S> for SharedSiteInfo {
impl<S: SizeSettings> FromStream<S> for SiteInfo {
fn from_stream<R: Read>(stream: &mut R) -> Result<Self> {
let size = S::size_from_stream(stream)?;
let comments = Mutex::new(
(0..size)
.map(|_| {
let name_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?;
let name = std::str::from_utf8(&name_bytes)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?
.into();
let comments = (0..size)
.map(|_| {
let name_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?;
let name = std::str::from_utf8(&name_bytes)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?
.into();
let text_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?;
let text = std::str::from_utf8(&text_bytes)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?
.into();
let text_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?;
let text = std::str::from_utf8(&text_bytes)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?
.into();
Ok(Comment { name, text })
})
.collect::<Result<Vec<_>>>()?,
);
Ok(Comment { name, text })
})
.collect::<Result<Vec<_>>>()?;
let visits = S::size_from_stream(stream)?.into();
let up = S::size_from_stream(stream)?.into();
let down = S::size_from_stream(stream)?.into();
let visits = S::size_from_stream(stream)?;
let up = S::size_from_stream(stream)?;
let down = S::size_from_stream(stream)?;
Ok(Self {
comments,
@ -455,7 +470,10 @@ fn handle_connection(
.insert(Arc::new(
File::open(&data_path)
.map(|mut file| {
from_stream::<PortableSettings, _, _>(&mut file).unwrap_or_default()
SharedSiteInfo::new(
from_stream::<PortableSettings, _, _>(&mut file)
.unwrap_or_default(),
)
})
.unwrap_or_default(),
))