From 9016a32b591020ba74f7b05cd65bd73068b106cc Mon Sep 17 00:00:00 2001 From: p11 Date: Sat, 31 May 2025 14:53:22 +0200 Subject: [PATCH] Implemented to_stream for site info instead of shared site info --- src/main.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index a2ef664..238d4ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -178,13 +178,10 @@ fn fail(mut stream: TcpStream) { let _ = writeln!(stream, "Page not found!"); } -impl ToStream for SharedSiteInfo { +impl ToStream for SiteInfo { fn to_stream(&self, stream: &mut W) -> Result<()> { - let Ok(comments) = self.comments.lock() else { - return Err(std::io::ErrorKind::ResourceBusy.into()); - }; - S::size_to_stream(comments.len(), stream)?; - for Comment { name, text } in comments.iter() { + 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)?; @@ -194,9 +191,9 @@ impl ToStream for SharedSiteInfo { stream.write_all(text_bytes)?; } - S::size_to_stream(self.visits.load(Ordering::Acquire), stream)?; - S::size_to_stream(self.up.load(Ordering::Acquire), stream)?; - S::size_to_stream(self.down.load(Ordering::Acquire), stream)?; + S::size_to_stream(self.visits, stream)?; + S::size_to_stream(self.up, stream)?; + S::size_to_stream(self.down, stream)?; Ok(()) } @@ -597,19 +594,21 @@ fn handle_relative_connection( progress, } = RequestData::parse(body); + let info = info.update(name, text, up, down); + + if let Ok(mut file) = File::create(file_paths.data) { + if to_stream::(&info, &mut file).is_err() { + eprintln!("Error saving data!"); + eprintln!(); + } + } + let SiteInfo { comments, visits, up, down, - } = info.update(name, text, up, down); - - if let Ok(mut file) = File::create(file_paths.data) { - if to_stream::(&*info, &mut file).is_err() { - eprintln!("Error saving data!"); - eprintln!(); - } - } + } = info; let _ = write!(stream, "HTTP/1.1 200 OK\r\n"); let _ = write!(stream, "Content-Type: text/html; charset=\"utf-8\"\r\n");