diff options
| -rw-r--r-- | crates/germ/examples/html.rs | 2 | ||||
| -rw-r--r-- | crates/germ/examples/markdown.rs | 2 | ||||
| -rw-r--r-- | crates/germ/src/ast/container.rs | 37 | ||||
| -rw-r--r-- | crates/germ/src/ast/node.rs | 2 | ||||
| -rw-r--r-- | crates/germ/src/convert/html.rs | 8 | ||||
| -rw-r--r-- | crates/germ/src/convert/markdown.rs | 14 | ||||
| -rw-r--r-- | crates/germ/src/meta.rs | 2 | ||||
| -rw-r--r-- | crates/germ/src/quick.rs | 6 | ||||
| -rw-r--r-- | crates/germ/src/request.rs | 2 | ||||
| -rw-r--r-- | crates/germ/src/request/status.rs | 6 |
10 files changed, 38 insertions, 43 deletions
diff --git a/crates/germ/examples/html.rs b/crates/germ/examples/html.rs index 541c01e..8dfe034 100644 --- a/crates/germ/examples/html.rs +++ b/crates/germ/examples/html.rs @@ -47,7 +47,7 @@ That was a link without text."#; fn main() { std::fs::write( - "examples/convert.html", + "crates/germ/examples/convert.html", germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML), ) .expect("could not write to file"); diff --git a/crates/germ/examples/markdown.rs b/crates/germ/examples/markdown.rs index c14cdc5..bb3610b 100644 --- a/crates/germ/examples/markdown.rs +++ b/crates/germ/examples/markdown.rs @@ -47,7 +47,7 @@ That was a link without text."#; fn main() { std::fs::write( - "examples/convert.md", + "crates/germ/examples/convert.md", germ::convert::from_string( EXAMPLE_GEMTEXT, &germ::convert::Target::Markdown, diff --git a/crates/germ/src/ast/container.rs b/crates/germ/src/ast/container.rs index 793473b..6cbc677 100644 --- a/crates/germ/src/ast/container.rs +++ b/crates/germ/src/ast/container.rs @@ -63,11 +63,11 @@ impl Ast { #[must_use] pub fn to_gemtext(&self) -> String { - let mut gemtext = "".to_string(); + let mut gemtext = String::new(); for node in &self.inner { match node { - Node::Text(text) => gemtext.push_str(&format!("{}\n", text)), + Node::Text(text) => gemtext.push_str(&format!("{text}\n")), Node::Link { to, text, @@ -77,7 +77,7 @@ impl Ast { to, text .clone() - .map_or_else(|| "".to_string(), |text| format!(" {}", text)), + .map_or_else(String::new, |text| format!(" {text}")), )), Node::Heading { level, @@ -98,18 +98,18 @@ impl Ast { "{}\n", items .iter() - .map(|i| format!("* {}", i)) + .map(|i| format!("* {i}")) .collect::<Vec<String>>() .join("\n"), )), - Node::Blockquote(text) => gemtext.push_str(&format!("> {}\n", text)), + Node::Blockquote(text) => gemtext.push_str(&format!("> {text}\n")), Node::PreformattedText { alt_text, text, } => gemtext.push_str(&format!( "```{}\n{}```\n", - alt_text.clone().unwrap_or_else(|| "".to_string()), + alt_text.clone().unwrap_or_default(), text )), Node::Whitespace => gemtext.push('\n'), @@ -177,19 +177,16 @@ impl Ast { "#" => { // If the Gemtext line starts with an "#", it is a heading, so let's // find out how deep it goes. - let level = match line.get(0..3) { - Some(root) => - if root.contains("###") { - 3 - } else if root.contains("##") { - 2 - } else if root.contains('#') { - 1 - } else { - 0 - }, - None => 0, - }; + let level = line.get(0..3).map_or(0, |root| { + if root.contains("###") { + 3 + } else if root.contains("##") { + 2 + } else { + // Converting the boolean response of `contains` to an integer + usize::from(root.contains('#')) + } + }); nodes.push(Node::Heading { level, @@ -263,7 +260,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!("{}\n", line)); + preformatted.push_str(&format!("{line}\n")); if let Some(next_line) = lines.next() { line = next_line; diff --git a/crates/germ/src/ast/node.rs b/crates/germ/src/ast/node.rs index 8fdeff6..e80ef84 100644 --- a/crates/germ/src/ast/node.rs +++ b/crates/germ/src/ast/node.rs @@ -26,7 +26,7 @@ /// - [Gemtext Documentation](https://gemini.circumlunar.space/docs/gemtext.gmi) /// - [Gemtext Cheatsheet](https://gemini.circumlunar.space/docs/cheatsheet.gmi). /// - [Gemini Specification](https://gemini.circumlunar.space/docs/specification.gmi). -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Eq)] pub enum Node { /// A text line /// diff --git a/crates/germ/src/convert/html.rs b/crates/germ/src/convert/html.rs index 7b1cafe..581cbab 100644 --- a/crates/germ/src/convert/html.rs +++ b/crates/germ/src/convert/html.rs @@ -25,7 +25,7 @@ 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!("<p>{}</p>", text)), + Node::Text(text) => html.push_str(&format!("<p>{text}</p>")), Node::Link { to, text, @@ -56,16 +56,16 @@ pub fn convert(source: &[Node]) -> String { "<ul>{}</ul>", items .iter() - .map(|i| format!("<li>{}</li>", i)) + .map(|i| format!("<li>{i}</li>")) .collect::<Vec<String>>() .join("\n") )), Node::Blockquote(text) => - html.push_str(&format!("<blockquote>{}</blockquote>", text)), + html.push_str(&format!("<blockquote>{text}</blockquote>")), Node::PreformattedText { text, .. } => { - html.push_str(&format!("<pre>{}</pre>", text)); + html.push_str(&format!("<pre>{text}</pre>")); } Node::Whitespace => {} } diff --git a/crates/germ/src/convert/markdown.rs b/crates/germ/src/convert/markdown.rs index a38da9f..3401940 100644 --- a/crates/germ/src/convert/markdown.rs +++ b/crates/germ/src/convert/markdown.rs @@ -25,14 +25,14 @@ 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!("{}\n", text)), + Node::Text(text) => markdown.push_str(&format!("{text}\n")), Node::Link { to, text, } => - markdown.push_str(&*text.clone().map_or_else( - || format!("<{}>\n", to), - |text| format!("[{}]({})\n", text, to), + markdown.push_str(&text.clone().map_or_else( + || format!("<{to}>\n"), + |text| format!("[{text}]({to})\n"), )), Node::Heading { level, @@ -54,18 +54,18 @@ pub fn convert(source: &[Node]) -> String { "{}\n", items .iter() - .map(|i| format!("- {}", i)) + .map(|i| format!("- {i}")) .collect::<Vec<String>>() .join("\n"), )), - Node::Blockquote(text) => markdown.push_str(&format!("> {}\n", text)), + Node::Blockquote(text) => markdown.push_str(&format!("> {text}\n")), Node::PreformattedText { alt_text, text, } => { markdown.push_str(&format!( "```{}\n{}```\n", - alt_text.clone().unwrap_or_else(|| "".to_string()), + alt_text.clone().unwrap_or_default(), text )); } diff --git a/crates/germ/src/meta.rs b/crates/germ/src/meta.rs index 4ff7885..12c37cb 100644 --- a/crates/germ/src/meta.rs +++ b/crates/germ/src/meta.rs @@ -52,7 +52,7 @@ impl ToString for Meta { parameters.reverse(); if parameters.is_empty() { - "".to_string() + String::new() } else { format!("; {}", parameters.join("; ")) } diff --git a/crates/germ/src/quick.rs b/crates/germ/src/quick.rs index d952b08..3ecf5a2 100644 --- a/crates/germ/src/quick.rs +++ b/crates/germ/src/quick.rs @@ -36,7 +36,7 @@ pub fn heading(text: &str, level: &HeadingLevel) -> String { } #[must_use] -pub fn list_item(text: &str) -> String { format!("* {}", text) } +pub fn list_item(text: &str) -> String { format!("* {text}") } #[must_use] pub fn list_items(items: &[&str]) -> String { @@ -52,12 +52,12 @@ pub fn link(text: &str, location: Option<&str>) -> String { format!( "=> {}{}", text, - location.map_or_else(|| "".to_string(), |l| format!(" {}", l)) + location.map_or_else(String::new, |l| format!(" {l}")) ) } #[must_use] -pub fn block_quote(text: &str) -> String { format!("> {}", text) } +pub fn block_quote(text: &str) -> String { format!("> {text}") } #[must_use] pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String { diff --git a/crates/germ/src/request.rs b/crates/germ/src/request.rs index 07d3552..85ee72d 100644 --- a/crates/germ/src/request.rs +++ b/crates/germ/src/request.rs @@ -60,7 +60,7 @@ pub fn request(url: &url::Url) -> anyhow::Result<Response> { ))?; let mut tls = rustls::Stream::new(&mut connection, &mut stream); - tls.write_all(format!("{}\r\n", url).as_bytes())?; + tls.write_all(format!("{url}\r\n").as_bytes())?; let mut plain_text = Vec::new(); diff --git a/crates/germ/src/request/status.rs b/crates/germ/src/request/status.rs index 793b0fc..c18f171 100644 --- a/crates/germ/src/request/status.rs +++ b/crates/germ/src/request/status.rs @@ -28,7 +28,7 @@ use std::{fmt, fmt::Formatter}; /// assert_eq!(Status::from(10), Status::Input); /// assert_eq!(i32::from(Status::Input), 10); /// ``` -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Eq)] pub enum Status { Input, SensitiveInput, @@ -108,7 +108,5 @@ impl From<i32> for Status { } impl fmt::Display for Status { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{self:?}") } } |