diff options
| author | Fuwn <[email protected]> | 2023-02-04 02:01:43 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-02-04 02:01:43 +0000 |
| commit | 1cfa3421e55c28dd9e7a88a53e4669f25e486bd4 (patch) | |
| tree | 3199eccf8e04cb9ba9205bbb3c52502e3d5694a3 /src | |
| parent | fix(gemini_to_html.rs): html entity-encode gemini (diff) | |
| download | september-1cfa3421e55c28dd9e7a88a53e4669f25e486bd4.tar.xz september-1cfa3421e55c28dd9e7a88a53e4669f25e486bd4.zip | |
build(rustup): move from nightly to stable)
Diffstat (limited to 'src')
| -rw-r--r-- | src/gemini_to_html.rs | 48 | ||||
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/response.rs | 41 | ||||
| -rw-r--r-- | src/url.rs | 19 |
4 files changed, 58 insertions, 68 deletions
diff --git a/src/gemini_to_html.rs b/src/gemini_to_html.rs index 845c568..8359cfe 100644 --- a/src/gemini_to_html.rs +++ b/src/gemini_to_html.rs @@ -47,22 +47,19 @@ pub fn gemini_to_html( )); let ast = ast_tree.inner(); let mut html = String::new(); - let mut title = "".to_string(); + let mut title = String::new(); for node in ast { match node { - Node::Text(text) => html.push_str(&format!("<p>{}</p>", text)), - Node::Link { - to, - text, - } => { + Node::Text(text) => html.push_str(&format!("<p>{text}</p>")), + Node::Link { to, text } => { let mut href = to.clone(); let mut surface = false; if href.contains("://") && !href.starts_with("gemini://") { surface = true; } else if !href.starts_with("gemini://") && !href.starts_with('/') { - href = format!("./{}", href); + href = format!("./{href}"); } else if href.starts_with('/') || !href.contains("://") { href = link_from_host_href(url, &href); } @@ -78,7 +75,7 @@ pub fn gemini_to_html( .trim_end_matches('/') .split('/') .collect::<Vec<_>>() - .get(0) + .first() .unwrap() != &url.authority.host.as_str() { @@ -115,13 +112,10 @@ pub fn gemini_to_html( html.push_str(&format!( "<p><a href=\"{}\">{}</a></p>\n", href, - text.clone().unwrap_or_else(|| "".to_string()) + text.clone().unwrap_or_default() )); } - Node::Heading { - level, - text, - } => { + Node::Heading { level, text } => { if title.is_empty() && *level == 1 { title = text.clone(); } @@ -137,21 +131,19 @@ pub fn gemini_to_html( text )); } - Node::List(items) => - html.push_str(&format!( - "<ul>{}</ul>", - items - .iter() - .map(|i| format!("<li>{}</li>", i)) - .collect::<Vec<String>>() - .join("\n") - )), - Node::Blockquote(text) => - html.push_str(&format!("<blockquote>{}</blockquote>", text)), - Node::PreformattedText { - text, .. - } => { - html.push_str(&format!("<pre>{}</pre>", text)); + 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>")); } Node::Whitespace => {} } diff --git a/src/main.rs b/src/main.rs index 3b6f740..a5c5330 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,19 +64,15 @@ async fn main() -> std::io::Result<()> { "0.0.0.0", // If the `PORT` environment variable is present, try to use it, otherwise; // use port `80`. - if let Ok(port) = var("PORT") { - match port.parse::<_>() { - Ok(port) => port, - Err(e) => { - warn!("could not use PORT from environment variables: {}", e); - warn!("proceeding with default port: 80"); + var("PORT").map_or(80, |port| match port.parse::<_>() { + Ok(port) => port, + Err(e) => { + warn!("could not use PORT from environment variables: {}", e); + warn!("proceeding with default port: 80"); - 80 - } + 80 } - } else { - 80 - }, + }), ))? .run() .await diff --git a/src/response.rs b/src/response.rs index b114441..bd79024 100644 --- a/src/response.rs +++ b/src/response.rs @@ -36,7 +36,7 @@ pub async fn default( { format!("?{}", req.query_string()) } else { - "".to_string() + String::new() } }), false, @@ -49,7 +49,7 @@ pub async fn default( return Ok( HttpResponse::BadRequest() .content_type("text/plain") - .body(format!("{}", e)), + .body(format!("{e}")), ); } }; @@ -75,7 +75,7 @@ pub async fn default( return Ok( HttpResponse::BadRequest() .content_type("text/plain") - .body(format!("{}", e)), + .body(format!("{e}")), ); } }) { @@ -95,7 +95,7 @@ pub async fn default( let language = meta .parameters() .get("lang") - .map_or_else(|| "".to_string(), ToString::to_string); + .map_or_else(String::new, ToString::to_string); // Reset timer for below timer = Instant::now(); @@ -107,9 +107,9 @@ pub async fn default( format!( "<!DOCTYPE html><html{}><head>", if language.is_empty() { - "".to_string() + String::new() } else { - format!(" lang=\"{}\"", language) + format!(" lang=\"{language}\"") } ) }; @@ -123,7 +123,7 @@ pub async fn default( return Ok( HttpResponse::Ok() - .content_type(format!("{}; charset={}", meta.mime(), charset)) + .content_type(format!("{}; charset={charset}", meta.mime())) .body(html_context), ); } @@ -157,18 +157,19 @@ pub async fn default( } // Add a title to HTML response - html_context.push_str(&format!("<title>{}</title>", gemini_title)); + html_context.push_str(&format!("<title>{gemini_title}</title>")); html_context.push_str("</head><body>"); match response.status { - gmi::protocol::StatusCode::Success(_) => - html_context.push_str(&gemini_html.1), + gmi::protocol::StatusCode::Success(_) => { + html_context.push_str(&gemini_html.1); + } _ => html_context.push_str(&format!("<p>{}</p>", response.meta)), } // Add proxy information to footer of HTML response html_context.push_str(&format!( - "<details>\n<summary>Proxy information</summary> + "<details>\n<summary>Proxy information</summary> <dl> <dt>Original URL</dt><dd><a href=\"{}\">{0}</a></dd> <dt>Status code</dt> @@ -182,14 +183,14 @@ pub async fn default( <p>This content has been proxied by \ <a href=\"https://github.com/gemrest/september{}\">September ({})</a>.</p> </details></body></html>", - url, - response.status, - response.meta, - response_time_taken.as_nanos() as f64 / 1_000_000.0, - convert_time_taken.as_nanos() as f64 / 1_000_000.0, - format_args!("/tree/{}", env!("VERGEN_GIT_SHA")), - env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), - )); + url, + response.status, + response.meta, + response_time_taken.as_nanos() as f64 / 1_000_000.0, + convert_time_taken.as_nanos() as f64 / 1_000_000.0, + format_args!("/tree/{}", env!("VERGEN_GIT_SHA")), + env!("VERGEN_GIT_SHA").get(0..5).unwrap_or("UNKNOWN"), + )); if let Ok(plain_texts) = var("PLAIN_TEXT_ROUTE") { if plain_texts.split(',').any(|r| r == req.path()) { @@ -202,7 +203,7 @@ pub async fn default( Ok( HttpResponse::Ok() - .content_type(format!("text/html; charset={}", charset)) + .content_type(format!("text/html; charset={charset}")) .body(html_context), ) } @@ -66,16 +66,17 @@ pub fn make( format!( "{}{}{}", { - if let Ok(root) = std::env::var("ROOT") { - root - } else { - warn!( - "could not use ROOT from environment variables, proceeding with \ - default root: gemini://fuwn.me" - ); + std::env::var("ROOT").map_or_else( + |_| { + warn!( + "could not use ROOT from environment variables, proceeding \ + with default root: gemini://fuwn.me" + ); - "gemini://fuwn.me".to_string() - } + "gemini://fuwn.me".to_string() + }, + |root| root, + ) }, path, if fallback { "/" } else { "" } |