From 8a1f94bb668aa1f52a5246015ca347786e3e0a8e Mon Sep 17 00:00:00 2001 From: p11 Date: Wed, 19 Jul 2023 21:18:28 +0200 Subject: [PATCH] Made site infos saveable on their own --- src/main.rs | 99 +++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index 703d5ed..903d4b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,59 @@ struct SiteInfo { down: usize, } +impl ToStream for SiteInfo { + fn to_stream(&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 FromStream for SiteInfo { + fn from_stream(stream: &mut R) -> Result { + let size = S::size_from_stream(stream)?; + let comments = (0..size) + .map(|_| { + let name_bytes = as FromStream>::from_stream(stream)?; + let name = std::str::from_utf8(&name_bytes) + .map_err(|e| Error::new(ErrorKind::InvalidData, e))? + .into(); + + let text_bytes = as FromStream>::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::>>()?; + + 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)] struct Context { infos: HashMap, SiteInfo>, @@ -73,20 +126,7 @@ impl ToStream for Context { S::size_to_stream(key_bytes.len(), stream)?; stream.write_all(key_bytes)?; - S::size_to_stream(info.comments.len(), 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)?; + to_stream::(info, stream)?; } Ok(()) @@ -103,36 +143,7 @@ impl FromStream for Context { .map_err(|e| Error::new(ErrorKind::InvalidData, e))? .into(); - let size = S::size_from_stream(stream)?; - let comments = (0..size) - .map(|_| { - let name_bytes = as FromStream>::from_stream(stream)?; - let name = std::str::from_utf8(&name_bytes) - .map_err(|e| Error::new(ErrorKind::InvalidData, e))? - .into(); - - let text_bytes = as FromStream>::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::>>()?; - - 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, - }, - )) + Ok((key, from_stream::(stream)?)) }) .collect::>>()?;