From 884aa53553e4a811dc269d60fd6fc59e837390d3 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 27 May 2025 16:16:35 +0000 Subject: refactor: Use latest best practices and formatting --- src/ast/container.rs | 65 +++++++++++++++++++++++++++++-------------------- src/convert/html.rs | 42 +++++++++++++++++++------------- src/convert/markdown.rs | 43 +++++++++++++++++++------------- src/meta.rs | 5 ++-- src/request/response.rs | 1 + src/request/verifier.rs | 5 +++- 6 files changed, 98 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/ast/container.rs b/src/ast/container.rs index b255ec7..00118bb 100644 --- a/src/ast/container.rs +++ b/src/ast/container.rs @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only -use super::Node; +use {super::Node, std::fmt::Write}; /// An AST structure which contains an AST tree /// @@ -52,7 +52,7 @@ impl Ast { /// ``` #[must_use] #[allow(clippy::needless_pass_by_value)] - pub fn from_string(value: (impl Into + ?Sized)) -> Self { + pub fn from_string(value: impl Into) -> Self { Self::from_value(&value.into()) } @@ -82,7 +82,7 @@ impl Ast { )); } - if source.chars().last().map_or(false, |c| c == '\n') { + if source.ends_with('\n') { if let Some(last) = ast.last() { if !matches!(last, Node::Whitespace) { ast.push(Node::Whitespace); @@ -110,7 +110,7 @@ impl Ast { /// ); /// ``` #[must_use] - pub fn from_nodes(nodes: Vec) -> Self { Self { inner: nodes } } + pub const fn from_nodes(nodes: Vec) -> Self { Self { inner: nodes } } #[must_use] pub fn to_gemtext(&self) -> String { @@ -118,29 +118,42 @@ impl Ast { for node in &self.inner { match node { - Node::Text(text) => gemtext.push_str(&format!("{text}\n")), - Node::Link { to, text } => gemtext.push_str(&format!( - "=> {}{}\n", - to, - text.clone().map_or_else(String::new, |text| format!(" {text}")), - )), - Node::Heading { level, text } => - gemtext.push_str(&format!("{} {}\n", "#".repeat(*level), text)), - Node::List(items) => gemtext.push_str(&format!( - "{}\n", - items - .iter() - .map(|i| format!("* {i}")) - .collect::>() - .join("\n"), - )), - Node::Blockquote(text) => gemtext.push_str(&format!("> {text}\n")), - Node::PreformattedText { alt_text, text } => - gemtext.push_str(&format!( - "```{}\n{}```\n", + Node::Text(text) => { + let _ = writeln!(&mut gemtext, "{text}"); + } + Node::Link { to, text } => { + let _ = writeln!( + &mut gemtext, + "=> {}{}", + to, + text.clone().map_or_else(String::new, |text| format!(" {text}")), + ); + } + Node::Heading { level, text } => { + let _ = writeln!(&mut gemtext, "{} {}", "#".repeat(*level), text); + } + Node::List(items) => { + let _ = writeln!( + &mut gemtext, + "{}", + items + .iter() + .map(|i| format!("* {i}")) + .collect::>() + .join("\n"), + ); + } + Node::Blockquote(text) => { + let _ = writeln!(&mut gemtext, "> {text}"); + } + Node::PreformattedText { alt_text, text } => { + let _ = writeln!( + &mut gemtext, + "```{}\n{}```", alt_text.clone().unwrap_or_default(), text - )), + ); + } Node::Whitespace => gemtext.push('\n'), } } @@ -281,7 +294,7 @@ impl Ast { if *in_preformatted { // If we are in a preformatted line context, add the line to the // preformatted blocks content and increment the line. - preformatted.push_str(&format!("{line}\n")); + let _ = writeln!(&mut preformatted, "{line}"); if let Some(next_line) = lines.next() { line = next_line; diff --git a/src/convert/html.rs b/src/convert/html.rs index a6b0426..4608cd5 100644 --- a/src/convert/html.rs +++ b/src/convert/html.rs @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only -use crate::ast::Node; +use {crate::ast::Node, std::fmt::Write}; pub fn convert(source: &[Node]) -> String { let mut html = String::new(); @@ -25,16 +25,20 @@ pub fn convert(source: &[Node]) -> String { // this AST tree to an alternative markup format. for node in source { match node { - Node::Text(text) => html.push_str(&format!("

{text}

")), + Node::Text(text) => { + let _ = write!(&mut html, "

{text}

"); + } Node::Link { to, text } => { - html.push_str(&format!( + let _ = write!( + &mut html, "{}
", to, text.clone().unwrap_or_else(|| to.clone()) - )); + ); } Node::Heading { level, text } => { - html.push_str(&format!( + let _ = write!( + &mut html, "<{}>{}", match level { 1 => "h1", @@ -43,20 +47,24 @@ pub fn convert(source: &[Node]) -> String { _ => "p", }, text - )); + ); + } + Node::List(items) => { + let _ = write!( + &mut html, + "
    {}
", + items + .iter() + .map(|i| format!("
  • {i}
  • ")) + .collect::>() + .join("\n") + ); + } + Node::Blockquote(text) => { + let _ = write!(&mut html, "
    {text}
    "); } - Node::List(items) => html.push_str(&format!( - "
      {}
    ", - items - .iter() - .map(|i| format!("
  • {i}
  • ")) - .collect::>() - .join("\n") - )), - Node::Blockquote(text) => - html.push_str(&format!("
    {text}
    ")), Node::PreformattedText { text, .. } => { - html.push_str(&format!("
    {text}
    ")); + let _ = write!(&mut html, "
    {text}
    "); } Node::Whitespace => {} } diff --git a/src/convert/markdown.rs b/src/convert/markdown.rs index 71e54f2..d90790f 100644 --- a/src/convert/markdown.rs +++ b/src/convert/markdown.rs @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only -use crate::ast::Node; +use {crate::ast::Node, std::fmt::Write}; pub fn convert(source: &[Node]) -> String { let mut markdown = String::new(); @@ -25,14 +25,17 @@ pub fn convert(source: &[Node]) -> String { // this AST tree to an alternative markup format. for node in source { match node { - Node::Text(text) => markdown.push_str(&format!("{text}\n")), + Node::Text(text) => { + let _ = writeln!(&mut markdown, "{text}"); + } Node::Link { to, text } => markdown.push_str(&text.clone().map_or_else( || format!("<{to}>\n"), |text| format!("[{text}]({to})\n"), )), Node::Heading { level, text } => { - markdown.push_str(&format!( - "{} {}\n", + let _ = writeln!( + &mut markdown, + "{} {}", match level { 1 => "#", 2 => "##", @@ -40,23 +43,29 @@ pub fn convert(source: &[Node]) -> String { _ => "", }, text - )); + ); + } + Node::List(items) => { + let _ = writeln!( + &mut markdown, + "{}", + items + .iter() + .map(|i| format!("- {i}")) + .collect::>() + .join("\n"), + ); + } + Node::Blockquote(text) => { + let _ = writeln!(&mut markdown, "> {text}"); } - Node::List(items) => markdown.push_str(&format!( - "{}\n", - items - .iter() - .map(|i| format!("- {i}")) - .collect::>() - .join("\n"), - )), - Node::Blockquote(text) => markdown.push_str(&format!("> {text}\n")), Node::PreformattedText { alt_text, text } => { - markdown.push_str(&format!( - "```{}\n{}```\n", + let _ = writeln!( + &mut markdown, + "```{}\n{}```", alt_text.clone().unwrap_or_default(), text - )); + ); } Node::Whitespace => markdown.push('\n'), } diff --git a/src/meta.rs b/src/meta.rs index dca963a..95ff4f1 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -100,6 +100,7 @@ impl Meta { /// "text/gemini", /// ); /// ``` + #[allow(clippy::missing_const_for_fn)] #[must_use] pub fn mime(&self) -> Cow<'_, str> { Cow::Borrowed(&self.mime) } @@ -112,7 +113,7 @@ impl Meta { /// /// *meta.mime_mut() = "text/gemini".to_string(); /// ``` - pub fn mime_mut(&mut self) -> &mut String { &mut self.mime } + pub const fn mime_mut(&mut self) -> &mut String { &mut self.mime } /// Obtain non-mutable access to the parameters of the `Meta` /// @@ -144,7 +145,7 @@ impl Meta { /// /// *meta.parameters_mut() = parameters; /// ``` - pub fn parameters_mut(&mut self) -> &mut HashMap { + pub const fn parameters_mut(&mut self) -> &mut HashMap { &mut self.parameters } } diff --git a/src/request/response.rs b/src/request/response.rs index 8f7ba48..7c3b1fd 100644 --- a/src/request/response.rs +++ b/src/request/response.rs @@ -64,6 +64,7 @@ impl Response { #[must_use] pub const fn status(&self) -> &Status { &self.status } + #[allow(clippy::missing_const_for_fn)] #[must_use] pub fn meta(&self) -> Cow<'_, str> { Cow::Borrowed(&self.meta) } diff --git a/src/request/verifier.rs b/src/request/verifier.rs index a858b72..8e5d015 100644 --- a/src/request/verifier.rs +++ b/src/request/verifier.rs @@ -17,7 +17,10 @@ // SPDX-License-Identifier: GPL-3.0-only use { - rustls::{client, client::ServerCertVerified, Certificate}, + rustls::{ + Certificate, + client::{self, ServerCertVerified}, + }, std::time::SystemTime, }; -- cgit v1.2.3