From 171bfc98a90d1aa9dda1b53eb7b6512287240185 Mon Sep 17 00:00:00 2001 From: neri Date: Fri, 7 Oct 2022 15:52:12 +0200 Subject: [PATCH] use inline string formatting --- src/db.rs | 13 +++++-------- src/mime_relations.rs | 2 +- src/multipart.rs | 13 +++++-------- src/rate_limit.rs | 2 +- src/template.rs | 6 +++--- src/upload.rs | 14 +++++++------- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/db.rs b/src/db.rs index 1a7d49e..a00359f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -30,18 +30,15 @@ fn get_db_url() -> String { let auth = if let Ok(user) = env::var("DATABASE_USER") { if let Ok(pass) = env::var("DATABASE_PASS") { - format!("{}:{}@", user, pass) + format!("{user}:{pass}@") } else { - format!("{}@", user) + format!("{user}@") } } else { String::new() }; - format!( - "postgresql://{auth}{host}/{name}", - auth = auth, - host = env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string()), - name = env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string()) - ) + let host = env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string()); + let name = env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string()); + format!("postgresql://{auth}{host}/{name}") } diff --git a/src/mime_relations.rs b/src/mime_relations.rs index de0d8a6..391c266 100644 --- a/src/mime_relations.rs +++ b/src/mime_relations.rs @@ -16,7 +16,7 @@ fn load_mime_aliases() -> HashMap { tree_magic_db::aliases() .lines() .flat_map(|line| line.split_once(' ')) - .flat_map(|(a, b)| Some((Mime::from_str(a).ok()?, Mime::from_str(b).ok()?))) + .flat_map(|(alias, mime)| Some((Mime::from_str(alias).ok()?, Mime::from_str(mime).ok()?))) .collect() } diff --git a/src/multipart.rs b/src/multipart.rs index 561582a..d0d42d0 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -47,7 +47,7 @@ pub(crate) async fn parse_multipart( content_type = Some(if mime == APPLICATION_OCTET_STREAM { get_content_type(file_path) } else { - mime.clone() + mime_relations::get_alias(mime) }); } "text" => { @@ -69,11 +69,10 @@ pub(crate) async fn parse_multipart( let content_type = content_type.ok_or_else(|| error::ErrorBadRequest("no content type found"))?; - let content_type = mime_relations::get_alias(content_type); let keep_for = keep_for_seconds .map(|k| k.parse()) .transpose() - .map_err(|e| error::ErrorBadRequest(format!("field keep_for is not a number: {}", e)))? + .map_err(|e| error::ErrorBadRequest(format!("field keep_for is not a number: {e}")))? .map(Duration::seconds) .unwrap_or(DEFAULT_UPLOAD_DURATION); let valid_till = OffsetDateTime::now_utc() + keep_for; @@ -105,8 +104,7 @@ fn check_requirements( if *keep_for > MAX_UPLOAD_DURATION { return Err(error::ErrorBadRequest(format!( - "maximum allowed validity is {}, but you specified {}", - MAX_UPLOAD_DURATION, keep_for + "maximum allowed validity is {MAX_UPLOAD_DURATION}, but you specified {keep_for}" ))); } @@ -136,7 +134,7 @@ fn get_field_name(field: &Field) -> Result { async fn parse_string(name: &str, field: actix_multipart::Field) -> Result { let data = read_content(field).await?; String::from_utf8(data) - .map_err(|_| error::ErrorBadRequest(format!("could not parse field {} as utf-8", name))) + .map_err(|_| error::ErrorBadRequest(format!("could not parse field {name} as utf-8"))) } async fn read_content(mut field: actix_multipart::Field) -> Result, error::Error> { @@ -172,8 +170,7 @@ async fn write_to_file( if let Some(max_size) = max_size { if written_bytes > max_size { return Err(error::ErrorBadRequest(format!( - "exceeded maximum file size of {} bytes", - max_size + "exceeded maximum file size of {max_size} bytes" ))); } } diff --git a/src/rate_limit.rs b/src/rate_limit.rs index 57a802b..4718ab2 100644 --- a/src/rate_limit.rs +++ b/src/rate_limit.rs @@ -34,7 +34,7 @@ impl KeyExtractor for ForwardedPeerIpKeyExtractor { .wait_time_from(DefaultClock::default().now()) .as_secs(); ( - format!("too many requests, retry in {}s", wait_time), + format!("too many requests, retry in {wait_time}s"), ContentType::plaintext(), ) } diff --git a/src/template.rs b/src/template.rs index 0046fa6..aa02594 100644 --- a/src/template.rs +++ b/src/template.rs @@ -61,7 +61,7 @@ fn render_file_size(size: u64) -> String { let magnitude = cmp::min((size as f64).log(1024.0) as u32, 5); let prefix = ["", "ki", "Mi", "Gi", "Ti", "Pi"][magnitude as usize]; let value = size / (1024_u64.pow(magnitude)); - format!("{}{}B", value, prefix) + format!("{value}{prefix}B") } fn render_duration(duration: Duration) -> String { @@ -88,8 +88,8 @@ fn render_duration(duration: Duration) -> String { fn pluralize(number: i64, word: &str, suffix: &str) -> Option { match number { 0 => None, - 1 => Some(format!("{} {}", number, word)), - _ => Some(format!("{} {}{}", number, word, suffix)), + 1 => Some(format!("{number} {word}")), + _ => Some(format!("{number} {word}{suffix}")), } } diff --git a/src/upload.rs b/src/upload.rs index 14159c1..4b2e912 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -66,8 +66,7 @@ pub async fn upload( let file_name = original_name.clone().unwrap_or_else(|| { format!( - "{}.{}", - file_id, + "{file_id}.{}", mime_relations::get_extension(&content_type).unwrap_or("txt") ) }); @@ -108,15 +107,15 @@ pub async fn upload( let redirect = if let Some(original_name) = original_name.as_ref() { let encoded_name = urlencoding::encode(original_name); - format!("/upload/{}/{}", file_id, encoded_name) + format!("/upload/{file_id}/{encoded_name}") } else { - format!("/upload/{}", file_id) + format!("/upload/{file_id}") }; let url = get_file_url(&req, &file_id, original_name.as_deref()); Ok(HttpResponse::SeeOther() .insert_header((LOCATION, redirect)) - .body(format!("{}\n", url))) + .body(format!("{url}\n"))) } async fn create_unique_file( @@ -149,11 +148,12 @@ fn gen_file_id() -> String { } fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String { + let host = template::get_host_url(req); if let Some(name) = name { let encoded_name = urlencoding::encode(name); - format!("{}/{}/{}", template::get_host_url(req), id, encoded_name) + format!("{host}/{id}/{encoded_name}") } else { - format!("{}/{}", template::get_host_url(req), id) + format!("{host}/{id}") } }