aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-05-27 16:16:35 +0000
committerFuwn <[email protected]>2025-05-27 16:16:35 +0000
commit884aa53553e4a811dc269d60fd6fc59e837390d3 (patch)
tree53718aa956dd7623465a9dd2fa142d024d4f9726
parentci(ga): bump rustc (diff)
downloadgerm-884aa53553e4a811dc269d60fd6fc59e837390d3.tar.xz
germ-884aa53553e4a811dc269d60fd6fc59e837390d3.zip
refactor: Use latest best practices and formatting
-rw-r--r--.github/workflows/check.yaml2
-rw-r--r--examples/meta.rs2
-rw-r--r--rust-toolchain.toml2
-rw-r--r--rustfmt.toml2
-rw-r--r--src/ast/container.rs65
-rw-r--r--src/convert/html.rs42
-rw-r--r--src/convert/markdown.rs43
-rw-r--r--src/meta.rs5
-rw-r--r--src/request/response.rs1
-rw-r--r--src/request/verifier.rs5
-rw-r--r--tests/ast.rs4
-rw-r--r--tests/convert.rs2
12 files changed, 105 insertions, 70 deletions
diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml
index bd21375..257dfc4 100644
--- a/.github/workflows/check.yaml
+++ b/.github/workflows/check.yaml
@@ -19,7 +19,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
- toolchain: 1.79.0
+ toolchain: stable
components: rustfmt, clippy
override: true
- name: Check ✅
diff --git a/examples/meta.rs b/examples/meta.rs
index e9d4cec..16cd45b 100644
--- a/examples/meta.rs
+++ b/examples/meta.rs
@@ -28,7 +28,7 @@ fn main() {
// Convert the structured meta representation back to a string, identical to
// the original meta section
- println!("{}", meta.to_string());
+ println!("{}", meta);
// The MIME type of the meta section
println!("{}", meta.mime());
diff --git a/rust-toolchain.toml b/rust-toolchain.toml
index 628740b..292fe49 100644
--- a/rust-toolchain.toml
+++ b/rust-toolchain.toml
@@ -1,2 +1,2 @@
[toolchain]
-channel = "1.79.0"
+channel = "stable"
diff --git a/rustfmt.toml b/rustfmt.toml
index 27653e1..13ea463 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -20,5 +20,5 @@ tab_spaces = 2
use_field_init_shorthand = true
use_small_heuristics = "Max"
use_try_shorthand = true
-version = "Two"
+style_edition = "2024"
wrap_comments = true
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 <[email protected]>
// 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<String> + ?Sized)) -> Self {
+ pub fn from_string(value: impl Into<String>) -> 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<Node>) -> Self { Self { inner: nodes } }
+ pub const fn from_nodes(nodes: Vec<Node>) -> 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::<Vec<String>>()
- .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::<Vec<String>>()
+ .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 <[email protected]>
// 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!("<p>{text}</p>")),
+ Node::Text(text) => {
+ let _ = write!(&mut html, "<p>{text}</p>");
+ }
Node::Link { to, text } => {
- html.push_str(&format!(
+ let _ = write!(
+ &mut html,
"<a href=\"{}\">{}</a><br>",
to,
text.clone().unwrap_or_else(|| to.clone())
- ));
+ );
}
Node::Heading { level, text } => {
- html.push_str(&format!(
+ let _ = write!(
+ &mut html,
"<{}>{}</{0}>",
match level {
1 => "h1",
@@ -43,20 +47,24 @@ pub fn convert(source: &[Node]) -> String {
_ => "p",
},
text
- ));
+ );
+ }
+ Node::List(items) => {
+ let _ = write!(
+ &mut html,
+ "<ul>{}</ul>",
+ items
+ .iter()
+ .map(|i| format!("<li>{i}</li>"))
+ .collect::<Vec<String>>()
+ .join("\n")
+ );
+ }
+ Node::Blockquote(text) => {
+ let _ = write!(&mut html, "<blockquote>{text}</blockquote>");
}
- Node::List(items) => html.push_str(&format!(
- "<ul>{}</ul>",
- items
- .iter()
- .map(|i| format!("<li>{i}</li>"))
- .collect::<Vec<String>>()
- .join("\n")
- )),
- Node::Blockquote(text) =>
- html.push_str(&format!("<blockquote>{text}</blockquote>")),
Node::PreformattedText { text, .. } => {
- html.push_str(&format!("<pre>{text}</pre>"));
+ let _ = write!(&mut html, "<pre>{text}</pre>");
}
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 <[email protected]>
// 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::<Vec<String>>()
+ .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::<Vec<String>>()
- .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<String, String> {
+ pub const fn parameters_mut(&mut self) -> &mut HashMap<String, String> {
&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,
};
diff --git a/tests/ast.rs b/tests/ast.rs
index 021fd96..37320a7 100644
--- a/tests/ast.rs
+++ b/tests/ast.rs
@@ -19,8 +19,8 @@
#[cfg(test)]
mod test {
use germ::{
- ast::{Ast, Node},
EXAMPLE_GEMTEXT,
+ ast::{Ast, Node},
};
#[test]
@@ -51,7 +51,7 @@ mod test {
#[test]
fn build_single_element() {
assert_eq!(
- Ast::from_string("=> /test hi").inner().get(0).unwrap(),
+ Ast::from_string("=> /test hi").inner().first().unwrap(),
&Node::Link { to: "/test".to_string(), text: Some("hi".to_string()) },
);
}
diff --git a/tests/convert.rs b/tests/convert.rs
index afff5bc..84d605e 100644
--- a/tests/convert.rs
+++ b/tests/convert.rs
@@ -19,7 +19,7 @@
#[cfg(test)]
mod test {
use germ::{
- convert::{from_string, Target},
+ convert::{Target, from_string},
gemini_to_html, gemini_to_md,
};