Skip to content
/ germ Public

Commit 33d7fd0

Browse files
committed
refactor: Use latest best practices and formatting
1 parent b43b2b3 commit 33d7fd0

12 files changed

Lines changed: 105 additions & 70 deletions

File tree

.github/workflows/check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
uses: actions-rs/toolchain@v1
2020
with:
2121
profile: minimal
22-
toolchain: 1.79.0
22+
toolchain: stable
2323
components: rustfmt, clippy
2424
override: true
2525
- name: Check ✅

examples/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828

2929
// Convert the structured meta representation back to a string, identical to
3030
// the original meta section
31-
println!("{}", meta.to_string());
31+
println!("{}", meta);
3232

3333
// The MIME type of the meta section
3434
println!("{}", meta.mime());

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "1.79.0"
2+
channel = "stable"

rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ tab_spaces = 2
2020
use_field_init_shorthand = true
2121
use_small_heuristics = "Max"
2222
use_try_shorthand = true
23-
version = "Two"
23+
style_edition = "2024"
2424
wrap_comments = true

src/ast/container.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717
// SPDX-License-Identifier: GPL-3.0-only
1818

19-
use super::Node;
19+
use {super::Node, std::fmt::Write};
2020

2121
/// An AST structure which contains an AST tree
2222
///
@@ -52,7 +52,7 @@ impl Ast {
5252
/// ```
5353
#[must_use]
5454
#[allow(clippy::needless_pass_by_value)]
55-
pub fn from_string(value: (impl Into<String> + ?Sized)) -> Self {
55+
pub fn from_string(value: impl Into<String>) -> Self {
5656
Self::from_value(&value.into())
5757
}
5858

@@ -82,7 +82,7 @@ impl Ast {
8282
));
8383
}
8484

85-
if source.chars().last().map_or(false, |c| c == '\n') {
85+
if source.ends_with('\n') {
8686
if let Some(last) = ast.last() {
8787
if !matches!(last, Node::Whitespace) {
8888
ast.push(Node::Whitespace);
@@ -110,37 +110,50 @@ impl Ast {
110110
/// );
111111
/// ```
112112
#[must_use]
113-
pub fn from_nodes(nodes: Vec<Node>) -> Self { Self { inner: nodes } }
113+
pub const fn from_nodes(nodes: Vec<Node>) -> Self { Self { inner: nodes } }
114114

115115
#[must_use]
116116
pub fn to_gemtext(&self) -> String {
117117
let mut gemtext = String::new();
118118

119119
for node in &self.inner {
120120
match node {
121-
Node::Text(text) => gemtext.push_str(&format!("{text}\n")),
122-
Node::Link { to, text } => gemtext.push_str(&format!(
123-
"=> {}{}\n",
124-
to,
125-
text.clone().map_or_else(String::new, |text| format!(" {text}")),
126-
)),
127-
Node::Heading { level, text } =>
128-
gemtext.push_str(&format!("{} {}\n", "#".repeat(*level), text)),
129-
Node::List(items) => gemtext.push_str(&format!(
130-
"{}\n",
131-
items
132-
.iter()
133-
.map(|i| format!("* {i}"))
134-
.collect::<Vec<String>>()
135-
.join("\n"),
136-
)),
137-
Node::Blockquote(text) => gemtext.push_str(&format!("> {text}\n")),
138-
Node::PreformattedText { alt_text, text } =>
139-
gemtext.push_str(&format!(
140-
"```{}\n{}```\n",
121+
Node::Text(text) => {
122+
let _ = writeln!(&mut gemtext, "{text}");
123+
}
124+
Node::Link { to, text } => {
125+
let _ = writeln!(
126+
&mut gemtext,
127+
"=> {}{}",
128+
to,
129+
text.clone().map_or_else(String::new, |text| format!(" {text}")),
130+
);
131+
}
132+
Node::Heading { level, text } => {
133+
let _ = writeln!(&mut gemtext, "{} {}", "#".repeat(*level), text);
134+
}
135+
Node::List(items) => {
136+
let _ = writeln!(
137+
&mut gemtext,
138+
"{}",
139+
items
140+
.iter()
141+
.map(|i| format!("* {i}"))
142+
.collect::<Vec<String>>()
143+
.join("\n"),
144+
);
145+
}
146+
Node::Blockquote(text) => {
147+
let _ = writeln!(&mut gemtext, "> {text}");
148+
}
149+
Node::PreformattedText { alt_text, text } => {
150+
let _ = writeln!(
151+
&mut gemtext,
152+
"```{}\n{}```",
141153
alt_text.clone().unwrap_or_default(),
142154
text
143-
)),
155+
);
156+
}
144157
Node::Whitespace => gemtext.push('\n'),
145158
}
146159
}
@@ -281,7 +294,7 @@ impl Ast {
281294
if *in_preformatted {
282295
// If we are in a preformatted line context, add the line to the
283296
// preformatted blocks content and increment the line.
284-
preformatted.push_str(&format!("{line}\n"));
297+
let _ = writeln!(&mut preformatted, "{line}");
285298

286299
if let Some(next_line) = lines.next() {
287300
line = next_line;

src/convert/html.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717
// SPDX-License-Identifier: GPL-3.0-only
1818

19-
use crate::ast::Node;
19+
use {crate::ast::Node, std::fmt::Write};
2020

2121
pub fn convert(source: &[Node]) -> String {
2222
let mut html = String::new();
@@ -25,16 +25,20 @@ pub fn convert(source: &[Node]) -> String {
2525
// this AST tree to an alternative markup format.
2626
for node in source {
2727
match node {
28-
Node::Text(text) => html.push_str(&format!("<p>{text}</p>")),
28+
Node::Text(text) => {
29+
let _ = write!(&mut html, "<p>{text}</p>");
30+
}
2931
Node::Link { to, text } => {
30-
html.push_str(&format!(
32+
let _ = write!(
33+
&mut html,
3134
"<a href=\"{}\">{}</a><br>",
3235
to,
3336
text.clone().unwrap_or_else(|| to.clone())
34-
));
37+
);
3538
}
3639
Node::Heading { level, text } => {
37-
html.push_str(&format!(
40+
let _ = write!(
41+
&mut html,
3842
"<{}>{}</{0}>",
3943
match level {
4044
1 => "h1",
@@ -43,20 +47,24 @@ pub fn convert(source: &[Node]) -> String {
4347
_ => "p",
4448
},
4549
text
46-
));
50+
);
51+
}
52+
Node::List(items) => {
53+
let _ = write!(
54+
&mut html,
55+
"<ul>{}</ul>",
56+
items
57+
.iter()
58+
.map(|i| format!("<li>{i}</li>"))
59+
.collect::<Vec<String>>()
60+
.join("\n")
61+
);
62+
}
63+
Node::Blockquote(text) => {
64+
let _ = write!(&mut html, "<blockquote>{text}</blockquote>");
4765
}
48-
Node::List(items) => html.push_str(&format!(
49-
"<ul>{}</ul>",
50-
items
51-
.iter()
52-
.map(|i| format!("<li>{i}</li>"))
53-
.collect::<Vec<String>>()
54-
.join("\n")
55-
)),
56-
Node::Blockquote(text) =>
57-
html.push_str(&format!("<blockquote>{text}</blockquote>")),
5866
Node::PreformattedText { text, .. } => {
59-
html.push_str(&format!("<pre>{text}</pre>"));
67+
let _ = write!(&mut html, "<pre>{text}</pre>");
6068
}
6169
Node::Whitespace => {}
6270
}

src/convert/markdown.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717
// SPDX-License-Identifier: GPL-3.0-only
1818

19-
use crate::ast::Node;
19+
use {crate::ast::Node, std::fmt::Write};
2020

2121
pub fn convert(source: &[Node]) -> String {
2222
let mut markdown = String::new();
@@ -25,38 +25,47 @@ pub fn convert(source: &[Node]) -> String {
2525
// this AST tree to an alternative markup format.
2626
for node in source {
2727
match node {
28-
Node::Text(text) => markdown.push_str(&format!("{text}\n")),
28+
Node::Text(text) => {
29+
let _ = writeln!(&mut markdown, "{text}");
30+
}
2931
Node::Link { to, text } => markdown.push_str(&text.clone().map_or_else(
3032
|| format!("<{to}>\n"),
3133
|text| format!("[{text}]({to})\n"),
3234
)),
3335
Node::Heading { level, text } => {
34-
markdown.push_str(&format!(
35-
"{} {}\n",
36+
let _ = writeln!(
37+
&mut markdown,
38+
"{} {}",
3639
match level {
3740
1 => "#",
3841
2 => "##",
3942
3 => "###",
4043
_ => "",
4144
},
4245
text
43-
));
46+
);
47+
}
48+
Node::List(items) => {
49+
let _ = writeln!(
50+
&mut markdown,
51+
"{}",
52+
items
53+
.iter()
54+
.map(|i| format!("- {i}"))
55+
.collect::<Vec<String>>()
56+
.join("\n"),
57+
);
58+
}
59+
Node::Blockquote(text) => {
60+
let _ = writeln!(&mut markdown, "> {text}");
4461
}
45-
Node::List(items) => markdown.push_str(&format!(
46-
"{}\n",
47-
items
48-
.iter()
49-
.map(|i| format!("- {i}"))
50-
.collect::<Vec<String>>()
51-
.join("\n"),
52-
)),
53-
Node::Blockquote(text) => markdown.push_str(&format!("> {text}\n")),
5462
Node::PreformattedText { alt_text, text } => {
55-
markdown.push_str(&format!(
56-
"```{}\n{}```\n",
63+
let _ = writeln!(
64+
&mut markdown,
65+
"```{}\n{}```",
5766
alt_text.clone().unwrap_or_default(),
5867
text
59-
));
68+
);
6069
}
6170
Node::Whitespace => markdown.push('\n'),
6271
}

src/meta.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl Meta {
100100
/// "text/gemini",
101101
/// );
102102
/// ```
103+
#[allow(clippy::missing_const_for_fn)]
103104
#[must_use]
104105
pub fn mime(&self) -> Cow<'_, str> { Cow::Borrowed(&self.mime) }
105106

@@ -112,7 +113,7 @@ impl Meta {
112113
///
113114
/// *meta.mime_mut() = "text/gemini".to_string();
114115
/// ```
115-
pub fn mime_mut(&mut self) -> &mut String { &mut self.mime }
116+
pub const fn mime_mut(&mut self) -> &mut String { &mut self.mime }
116117

117118
/// Obtain non-mutable access to the parameters of the `Meta`
118119
///
@@ -144,7 +145,7 @@ impl Meta {
144145
///
145146
/// *meta.parameters_mut() = parameters;
146147
/// ```
147-
pub fn parameters_mut(&mut self) -> &mut HashMap<String, String> {
148+
pub const fn parameters_mut(&mut self) -> &mut HashMap<String, String> {
148149
&mut self.parameters
149150
}
150151
}

src/request/response.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl Response {
6464
#[must_use]
6565
pub const fn status(&self) -> &Status { &self.status }
6666

67+
#[allow(clippy::missing_const_for_fn)]
6768
#[must_use]
6869
pub fn meta(&self) -> Cow<'_, str> { Cow::Borrowed(&self.meta) }
6970

src/request/verifier.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
// SPDX-License-Identifier: GPL-3.0-only
1818

1919
use {
20-
rustls::{client, client::ServerCertVerified, Certificate},
20+
rustls::{
21+
Certificate,
22+
client::{self, ServerCertVerified},
23+
},
2124
std::time::SystemTime,
2225
};
2326

0 commit comments

Comments
 (0)