Made site infos saveable on their own

This commit is contained in:
p11 2023-07-19 21:18:28 +02:00
parent 4eb59a01b7
commit 8a1f94bb66

View File

@ -60,6 +60,59 @@ struct SiteInfo {
down: usize, down: usize,
} }
impl<S: SizeSettings> ToStream<S> for SiteInfo {
fn to_stream<W: Write>(&self, stream: &mut W) -> Result<()> {
S::size_to_stream(self.comments.len(), stream)?;
for Comment { name, text } in &self.comments {
let name_bytes = name.as_bytes();
S::size_to_stream(name_bytes.len(), stream)?;
stream.write_all(name_bytes)?;
let text_bytes = text.as_bytes();
S::size_to_stream(text_bytes.len(), stream)?;
stream.write_all(text_bytes)?;
}
S::size_to_stream(self.visits, stream)?;
S::size_to_stream(self.up, stream)?;
S::size_to_stream(self.down, stream)?;
Ok(())
}
}
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 = (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();
Ok(Comment { name, text })
})
.collect::<Result<Vec<_>>>()?;
let visits = S::size_from_stream(stream)?;
let up = S::size_from_stream(stream)?;
let down = S::size_from_stream(stream)?;
Ok(SiteInfo {
comments,
visits,
up,
down,
})
}
}
#[derive(Default)] #[derive(Default)]
struct Context { struct Context {
infos: HashMap<Box<str>, SiteInfo>, infos: HashMap<Box<str>, SiteInfo>,
@ -73,20 +126,7 @@ impl<S: SizeSettings> ToStream<S> for Context {
S::size_to_stream(key_bytes.len(), stream)?; S::size_to_stream(key_bytes.len(), stream)?;
stream.write_all(key_bytes)?; stream.write_all(key_bytes)?;
S::size_to_stream(info.comments.len(), stream)?; to_stream::<S, _, _>(info, stream)?;
for Comment { name, text } in &info.comments {
let name_bytes = name.as_bytes();
S::size_to_stream(name_bytes.len(), stream)?;
stream.write_all(name_bytes)?;
let text_bytes = text.as_bytes();
S::size_to_stream(text_bytes.len(), stream)?;
stream.write_all(text_bytes)?;
}
S::size_to_stream(info.visits, stream)?;
S::size_to_stream(info.up, stream)?;
S::size_to_stream(info.down, stream)?;
} }
Ok(()) Ok(())
@ -103,36 +143,7 @@ impl<S: SizeSettings> FromStream<S> for Context {
.map_err(|e| Error::new(ErrorKind::InvalidData, e))? .map_err(|e| Error::new(ErrorKind::InvalidData, e))?
.into(); .into();
let size = S::size_from_stream(stream)?; Ok((key, from_stream::<S, SiteInfo, _>(stream)?))
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();
Ok(Comment { name, text })
})
.collect::<Result<Vec<_>>>()?;
let visits = S::size_from_stream(stream)?;
let up = S::size_from_stream(stream)?;
let down = S::size_from_stream(stream)?;
Ok((
key,
SiteInfo {
comments,
visits,
up,
down,
},
))
}) })
.collect::<Result<HashMap<_, _>>>()?; .collect::<Result<HashMap<_, _>>>()?;