aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-05-18 01:52:36 +0000
committerFuwn <[email protected]>2022-05-18 01:52:36 +0000
commit11405ce0600d61f3772a7efe7fbd3de4cc2bf892 (patch)
tree24ebda9cc59ae51b4e87fdde688fe009eaf420af
parentchore(makefile): use --all-features (diff)
downloadgerm-11405ce0600d61f3772a7efe7fbd3de4cc2bf892.tar.xz
germ-11405ce0600d61f3772a7efe7fbd3de4cc2bf892.zip
fix: global clippy lint fixes
-rw-r--r--Cargo.toml2
-rw-r--r--examples/html.rs5
-rw-r--r--examples/markdown.rs4
-rw-r--r--examples/meta.rs2
-rw-r--r--examples/request.rs2
-rw-r--r--src/ast.rs6
-rw-r--r--src/convert.rs19
-rw-r--r--src/convert/html.rs4
-rw-r--r--src/convert/markdown.rs11
-rw-r--r--src/meta.rs9
-rw-r--r--src/request.rs13
-rw-r--r--src/request/response.rs13
-rw-r--r--src/request/status.rs2
-rw-r--r--src/request/verifier.rs2
-rw-r--r--tests/meta.rs6
15 files changed, 50 insertions, 50 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 5c1b568..142af1f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
[package]
name = "germ"
-version = "0.2.0"
+version = "0.2.1"
authors = ["Fuwn <[email protected]>"]
edition = "2021"
description = "The Ultimate Gemini Toolkit."
diff --git a/examples/html.rs b/examples/html.rs
index 5e1821d..541c01e 100644
--- a/examples/html.rs
+++ b/examples/html.rs
@@ -48,10 +48,7 @@ That was a link without text."#;
fn main() {
std::fs::write(
"examples/convert.html",
- germ::convert::convert_from_string(
- EXAMPLE_GEMTEXT,
- germ::convert::Target::HTML,
- ),
+ germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML),
)
.expect("could not write to file");
}
diff --git a/examples/markdown.rs b/examples/markdown.rs
index 249c3b9..c14cdc5 100644
--- a/examples/markdown.rs
+++ b/examples/markdown.rs
@@ -48,9 +48,9 @@ That was a link without text."#;
fn main() {
std::fs::write(
"examples/convert.md",
- germ::convert::convert_from_string(
+ germ::convert::from_string(
EXAMPLE_GEMTEXT,
- germ::convert::Target::Markdown,
+ &germ::convert::Target::Markdown,
),
)
.expect("could not write to file");
diff --git a/examples/meta.rs b/examples/meta.rs
index 75d894b..a9f4077 100644
--- a/examples/meta.rs
+++ b/examples/meta.rs
@@ -19,6 +19,6 @@
fn main() {
println!(
"{:?}",
- germ::meta::Meta::from_str("text/gemini; hi=2; hi2=string=2")
+ germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2")
)
}
diff --git a/examples/request.rs b/examples/request.rs
index 5791896..e33710f 100644
--- a/examples/request.rs
+++ b/examples/request.rs
@@ -17,7 +17,7 @@
// SPDX-License-Identifier: GPL-3.0-only
fn main() {
- match germ::request::request(url::Url::parse("gemini://fuwn.me").unwrap()) {
+ match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) {
Ok(response) => println!("{:?}", response),
Err(_) => {}
}
diff --git a/src/ast.rs b/src/ast.rs
index 8f8495e..724b491 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -202,6 +202,7 @@ pub fn build(source: &str) -> Vec<Node> {
ast
}
+#[allow(clippy::too_many_lines)]
fn evaluate(
line: &str,
lines: &mut std::str::Lines<'_>,
@@ -320,7 +321,7 @@ fn evaluate(
break;
}
// This as a catchall, it does a number of things.
- _ =>
+ _ => {
if *in_preformatted {
// If we are in a preformatted line context, add the line to the
// preformatted blocks content and increment the line.
@@ -342,7 +343,8 @@ fn evaluate(
nodes.push(Node::Text(line.to_string()));
break;
- },
+ }
+ }
}
}
diff --git a/src/convert.rs b/src/convert.rs
index 6661d4f..d46b854 100644
--- a/src/convert.rs
+++ b/src/convert.rs
@@ -36,15 +36,13 @@ pub enum Target {
/// ```rust
/// use germ::convert;
///
-/// convert::convert_from_ast(
+/// convert::from_ast(
/// germ::ast::build(r#"=> gemini://gem.rest/ GemRest"#),
-/// convert::Target::HTML,
+/// &convert::Target::HTML,
/// );
/// ```
-pub fn convert_from_ast(
- source: Vec<crate::ast::Node>,
- target: Target,
-) -> String {
+#[must_use]
+pub fn from_ast(source: Vec<crate::ast::Node>, target: &Target) -> String {
match target {
Target::Markdown => markdown::convert(source),
Target::HTML => html::convert(source),
@@ -58,11 +56,12 @@ pub fn convert_from_ast(
/// ```rust
/// use germ::convert;
///
-/// convert::convert_from_string(
+/// convert::from_string(
/// r#"=> gemini://gem.rest/ GemRest"#,
-/// convert::Target::HTML,
+/// &convert::Target::HTML,
/// );
/// ```
-pub fn convert_from_string(source: &str, target: Target) -> String {
- convert_from_ast(crate::ast::build(source), target)
+#[must_use]
+pub fn from_string(source: &str, target: &Target) -> String {
+ from_ast(crate::ast::build(source), target)
}
diff --git a/src/convert/html.rs b/src/convert/html.rs
index 18aec1e..2f031b0 100644
--- a/src/convert/html.rs
+++ b/src/convert/html.rs
@@ -33,7 +33,7 @@ pub fn convert(source: Vec<Node>) -> String {
html.push_str(&format!(
"<a href=\"{}\">{}</a><br>",
to,
- text.unwrap_or(to.clone())
+ text.unwrap_or_else(|| to.clone())
));
}
Node::Heading {
@@ -67,7 +67,7 @@ pub fn convert(source: Vec<Node>) -> String {
} => {
html.push_str(&format!("<pre>{}</pre>", text));
}
- _ => {}
+ Node::Whitespace => {}
}
}
diff --git a/src/convert/markdown.rs b/src/convert/markdown.rs
index b07d5f1..ccd2304 100644
--- a/src/convert/markdown.rs
+++ b/src/convert/markdown.rs
@@ -30,11 +30,10 @@ pub fn convert(source: Vec<Node>) -> String {
to,
text,
} =>
- markdown.push_str(&if let Some(text) = text {
- format!("[{}]({})", text, to)
- } else {
- format!("<{}>", to)
- }),
+ markdown.push_str(&*text.map_or_else(
+ || format!("<{}>", to),
+ |text| format!("[{}]({})", text, to),
+ )),
Node::Heading {
level,
text,
@@ -65,7 +64,7 @@ pub fn convert(source: Vec<Node>) -> String {
} => {
markdown.push_str(&format!(
"```{}\n{}```",
- alt_text.unwrap_or("".to_string()),
+ alt_text.unwrap_or_else(|| "".to_string()),
text
));
}
diff --git a/src/meta.rs b/src/meta.rs
index 24a29f3..45c90f1 100644
--- a/src/meta.rs
+++ b/src/meta.rs
@@ -28,18 +28,19 @@ pub struct Meta {
pub parameters: HashMap<String, String>,
}
impl Meta {
- pub fn from_str(meta: &str) -> Self {
- let mut metas = meta.split(";");
+ #[must_use]
+ pub fn from_string(meta: &str) -> Self {
+ let mut metas = meta.split(';');
let mime = metas.next().unwrap_or("").to_string();
let mut parameters = HashMap::new();
for parameter in metas {
let key_value = parameter
.trim_start()
- .split_at(parameter.find("=").unwrap_or(0));
+ .split_at(parameter.find('=').unwrap_or(0));
parameters.insert(
- key_value.0.to_string().replace("=", ""),
+ key_value.0.to_string().replace('=', ""),
key_value.1.to_string(),
);
}
diff --git a/src/request.rs b/src/request.rs
index f6297ef..07d3552 100644
--- a/src/request.rs
+++ b/src/request.rs
@@ -34,12 +34,17 @@ use verifier::GermVerifier;
/// # Example
///
/// ```rust
-/// match germ::request::request(url::Url::parse("gemini://fuwn.me").unwrap()) {
+/// match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap()) {
/// Ok(response) => println!("{:?}", response),
/// Err(_) => {}
/// }
/// ```
-pub fn request(url: url::Url) -> anyhow::Result<Response> {
+///
+/// # Errors
+/// - May error if the URL is invalid
+/// - May error if the TLS write fails
+/// - May error if the TLS read fails
+pub fn request(url: &url::Url) -> anyhow::Result<Response> {
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(std::sync::Arc::new(GermVerifier::new()))
@@ -59,10 +64,10 @@ pub fn request(url: url::Url) -> anyhow::Result<Response> {
let mut plain_text = Vec::new();
- tls.read_to_end(&mut plain_text).unwrap();
+ tls.read_to_end(&mut plain_text)?;
Ok(Response::new(
- plain_text,
+ &plain_text,
tls.conn.negotiated_cipher_suite(),
))
}
diff --git a/src/request/response.rs b/src/request/response.rs
index 0202967..e96cee0 100644
--- a/src/request/response.rs
+++ b/src/request/response.rs
@@ -29,21 +29,18 @@ pub struct Response {
pub suite: Option<SupportedCipherSuite>,
}
impl Response {
- pub(super) fn new(
- data: Vec<u8>,
- suite: Option<SupportedCipherSuite>,
- ) -> Self {
- let string_form = String::from_utf8_lossy(&data).to_string();
+ pub(super) fn new(data: &[u8], suite: Option<SupportedCipherSuite>) -> Self {
+ let string_form = String::from_utf8_lossy(data).to_string();
let mut content = None;
let header;
- if !string_form.ends_with("\r\n") {
+ if string_form.ends_with("\r\n") {
+ header = string_form;
+ } else {
let mut string_split = string_form.split("\r\n");
header = string_split.next().unwrap_or("").to_string();
content = Some(string_split.collect());
- } else {
- header = string_form;
}
let header_split = header.split_at(2);
diff --git a/src/request/status.rs b/src/request/status.rs
index b2065ed..8a18ce7 100644
--- a/src/request/status.rs
+++ b/src/request/status.rs
@@ -72,7 +72,7 @@ impl From<Status> for i32 {
Status::ClientCertificateRequired => 60,
Status::CertificateNotAuthorised => 61,
Status::CertificateNotValid => 62,
- _ => 0,
+ Status::Unsupported => 0,
}
}
}
diff --git a/src/request/verifier.rs b/src/request/verifier.rs
index f2c6a35..d6511c3 100644
--- a/src/request/verifier.rs
+++ b/src/request/verifier.rs
@@ -22,7 +22,7 @@ use rustls::{client, client::ServerCertVerified, Certificate};
pub(super) struct GermVerifier;
impl GermVerifier {
- pub fn new() -> Self { Self {} }
+ pub const fn new() -> Self { Self {} }
}
impl client::ServerCertVerifier for GermVerifier {
fn verify_server_cert(
diff --git a/tests/meta.rs b/tests/meta.rs
index b7fffa1..511b991 100644
--- a/tests/meta.rs
+++ b/tests/meta.rs
@@ -23,7 +23,7 @@ mod test {
#[test]
fn meta_to_map_mime() {
assert_eq!(
- Meta::from_str("text/gemini; hi=2; hi2=string=2").mime,
+ Meta::from_string("text/gemini; hi=2; hi2=string=2").mime,
"text/gemini",
);
}
@@ -31,7 +31,7 @@ mod test {
#[test]
fn meta_to_map_with_parameters() {
assert_eq!(
- Meta::from_str("text/gemini; hi=2; hi2=string=2")
+ Meta::from_string("text/gemini; hi=2; hi2=string=2")
.parameters
.get("hi2"),
Some(&"string=2".to_string()),
@@ -41,7 +41,7 @@ mod test {
#[test]
fn meta_to_map_length() {
assert_eq!(
- Meta::from_str("text/gemini; hi=2; hi2=string=2")
+ Meta::from_string("text/gemini; hi=2; hi2=string=2")
.parameters
.len(),
2,