From 81134d20073795b10017be6eefb474f6a338f43a Mon Sep 17 00:00:00 2001 From: p11 Date: Fri, 30 May 2025 00:22:58 +0200 Subject: [PATCH] Section info not optional anymore --- src/main.rs | 139 +++++++++++++++++++++++++--------------------------- 1 file changed, 68 insertions(+), 71 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ace602..f4ba727 100644 --- a/src/main.rs +++ b/src/main.rs @@ -651,10 +651,9 @@ fn handle_relative_connection( Comment(Vec), } - #[derive(Default)] struct Section { title: Box, - info: Option, + info: SectionInfo, } let mut sections = Vec::new(); @@ -671,7 +670,7 @@ fn handle_relative_connection( if let Some(config_map) = config_map { sections.push(Section { title: "Game".into(), - info: Some(SectionInfo::Game(config_map)), + info: SectionInfo::Game(config_map), }); } else { let Ok(pk_file) = File::open(file_paths.pk) else { @@ -689,7 +688,7 @@ fn handle_relative_connection( if !current_title.is_empty() { sections.push(Section { title: current_title, - info: Some(SectionInfo::Lines(current_lines)), + info: SectionInfo::Lines(current_lines), }) } current_title = title.into(); @@ -703,20 +702,20 @@ fn handle_relative_connection( sections.push(Section { title: current_title, - info: Some(SectionInfo::Lines(current_lines)), + info: SectionInfo::Lines(current_lines), }) } if file_paths.pki.is_some() { sections.push(Section { title: "Description".into(), - info: Some(SectionInfo::Description), + info: SectionInfo::Description, }); } sections.push(Section { title: "Comments".into(), - info: Some(SectionInfo::Comment(comments)), + info: SectionInfo::Comment(comments), }); let count = sections.len(); @@ -802,80 +801,78 @@ fn handle_relative_connection( for (i, Section { info, .. }) in sections.into_iter().enumerate() { let index = i + 1; let _ = write!(stream, r#"
"#); - if let Some(info) = info { - match info { - SectionInfo::Lines(lines) => { - convert_extended( - lines, - &mut stream, - Settings::default() - .with_handler(entry_handler(path, relative_path, censored)) - .with_start_level(start_level) - .with_use_textboxes(true), - ); + match info { + SectionInfo::Lines(lines) => { + convert_extended( + lines, + &mut stream, + Settings::default() + .with_handler(entry_handler(path, relative_path, censored)) + .with_start_level(start_level) + .with_use_textboxes(true), + ); + } + SectionInfo::Game(config_map) => { + if render_novel( + config_map, + file_paths.pk, + mlc_path.as_deref(), + file_paths.mld, + relative_path, + &mut stream, + choice, + progress, + ) + .is_err() + { + fail(stream); + return; } - SectionInfo::Game(config_map) => { - if render_novel( - config_map, - file_paths.pk, - mlc_path.as_deref(), - file_paths.mld, - relative_path, - &mut stream, - choice, - progress, - ) - .is_err() - { - fail(stream); - return; + } + SectionInfo::Description => { + let Ok(pki_file) = File::open(file_paths.pki.unwrap()) else { + unreachable!(); + }; + + let _ = writeln!(stream, "

Description

"); + + if let Some(audio_path) = &file_paths.audio { + if Path::is_file(audio_path) { + let _ = writeln!( + stream, + "

" + ); } } - SectionInfo::Description => { - let Ok(pki_file) = File::open(file_paths.pki.unwrap()) else { - unreachable!(); - }; - let _ = writeln!(stream, "

Description

"); - - if let Some(audio_path) = &file_paths.audio { - if Path::is_file(audio_path) { - let _ = writeln!( - stream, - "

" - ); - } + let lines = BufReader::new(pki_file).lines(); + convert_subheader(lines.map(Result::unwrap_or_default), &mut stream, 1); + } + SectionInfo::Comment(comments) => { + let html = html! { + h1 { "Comments" } + form method="POST" { + input type="text" name="name" value="anon" placeholder="Name"; + br; + textarea rows="5" cols="60" name="text" placeholder="Enter comment..." {} + br; + input type="submit" value="Send!"; } + form method="POST" { + input type="submit" value="💖️" name="up"; + input type="submit" value="💔️" name="down"; + } + }; + let _ = stream.write_all(html.into_string().as_bytes()); - let lines = BufReader::new(pki_file).lines(); - convert_subheader(lines.map(Result::unwrap_or_default), &mut stream, 1); - } - SectionInfo::Comment(comments) => { + for Comment { name, text } in comments { let html = html! { - h1 { "Comments" } - form method="POST" { - input type="text" name="name" value="anon" placeholder="Name"; - br; - textarea rows="5" cols="60" name="text" placeholder="Enter comment..." {} - br; - input type="submit" value="Send!"; - } - form method="POST" { - input type="submit" value="💖️" name="up"; - input type="submit" value="💔️" name="down"; + fieldset { + legend { (name) } + (maud::PreEscaped(text)) } }; let _ = stream.write_all(html.into_string().as_bytes()); - - for Comment { name, text } in comments { - let html = html! { - fieldset { - legend { (name) } - (maud::PreEscaped(text)) - } - }; - let _ = stream.write_all(html.into_string().as_bytes()); - } } } }