diff options
author | Joris | 2025-02-06 19:10:54 +0100 |
---|---|---|
committer | Joris | 2025-02-06 19:10:54 +0100 |
commit | 2956aa5f5324e4183dbc87f81fbc71fd0f43dbf9 (patch) | |
tree | 41673ba3f942367a5548c1f05ef20a5e17bbba79 /src | |
parent | c70e84a1b2aa307363b7b760f9f15b7438b0afba (diff) |
Improve String handling
See https://blog.sulami.xyz/posts/string-vs-str/
Diffstat (limited to 'src')
-rw-r--r-- | src/controller/utils.rs | 4 | ||||
-rw-r--r-- | src/db/incomes.rs | 6 | ||||
-rw-r--r-- | src/db/utils.rs | 4 | ||||
-rw-r--r-- | src/jobs/weekly_report.rs | 4 | ||||
-rw-r--r-- | src/mail.rs | 14 | ||||
-rw-r--r-- | src/routes.rs | 2 | ||||
-rw-r--r-- | src/templates.rs | 10 |
7 files changed, 23 insertions, 21 deletions
diff --git a/src/controller/utils.rs b/src/controller/utils.rs index 340a5c7..ccef33c 100644 --- a/src/controller/utils.rs +++ b/src/controller/utils.rs @@ -71,8 +71,8 @@ fn server_error( ) } -pub fn text(str: String) -> Response<Full<Bytes>> { - let mut response = Response::new(str.into()); +pub fn text(str: impl Into<String>) -> Response<Full<Bytes>> { + let mut response = Response::new(str.into().into()); *response.status_mut() = StatusCode::OK; response } diff --git a/src/db/incomes.rs b/src/db/incomes.rs index 90282c0..688e9e1 100644 --- a/src/db/incomes.rs +++ b/src/db/incomes.rs @@ -373,8 +373,8 @@ fn cumulative_query(from: NaiveDate) -> String { ON users.id = incomes.user_id "#, - bounded_query(">".to_string(), from.format("%Y-%m-%d").to_string()), - bounded_query("<".to_string(), "date()".to_string()) + bounded_query(">", &from.format("%Y-%m-%d").to_string()), + bounded_query("<", "date()") ) } @@ -382,7 +382,7 @@ fn cumulative_query(from: NaiveDate) -> String { /// /// It filters incomes according to the operator and date, /// and adds the income at this date. -fn bounded_query(op: String, date: String) -> String { +fn bounded_query(op: &str, date: &str) -> String { format!( r#" SELECT diff --git a/src/db/utils.rs b/src/db/utils.rs index 30f25c9..bd4d867 100644 --- a/src/db/utils.rs +++ b/src/db/utils.rs @@ -2,7 +2,9 @@ use crate::model::report::Report; use tokio_rusqlite::{Connection, Row}; pub async fn support_foreign_keys(conn: &Connection) { - let res = conn.call(move |conn| Ok(conn.pragma_update(None, "foreign_keys", "ON"))).await; + let res = conn + .call(move |conn| Ok(conn.pragma_update(None, "foreign_keys", "ON"))) + .await; if let Err(err) = res { log::error!("Error supporting foreign keys: {err:?}"); diff --git a/src/jobs/weekly_report.rs b/src/jobs/weekly_report.rs index a91a3fb..35bf5af 100644 --- a/src/jobs/weekly_report.rs +++ b/src/jobs/weekly_report.rs @@ -23,8 +23,8 @@ pub async fn send( address: u.email, }) .collect(), - "Rapport hebdomadaire".to_string(), - report, + "Rapport hebdomadaire", + &report, ) .await } diff --git a/src/mail.rs b/src/mail.rs index b6db0cd..7017654 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -18,8 +18,8 @@ pub struct Recipient { pub async fn send( config: &Config, recipients: Vec<Recipient>, - subject: String, - message: String, + subject: &str, + message: &str, ) -> bool { let headers = format_headers(recipients.clone(), subject); @@ -72,24 +72,24 @@ pub async fn send( } } -fn format_headers(recipients: Vec<Recipient>, subject: String) -> String { +fn format_headers(recipients: Vec<Recipient>, subject: &str) -> String { let recipients = recipients .into_iter() - .map(|r| format_address(r.name, r.address)) + .map(|r| format_address(&r.name, &r.address)) .collect::<Vec<String>>() .join(", "); format!( "Date: {}\nFrom: {}\nTo: {}\nSubject: {}", Utc::now().to_rfc2822(), - format_address(FROM_NAME.to_string(), FROM_ADDRESS.to_string()), + format_address(FROM_NAME, FROM_ADDRESS), recipients, subject, ) } -fn format_address(name: String, address: String) -> String { - format!("{} <{}>", name, address) +fn format_address(name: &str, address: &str) -> String { + format!("{name} <{address}>") } async fn spawn(mut command: Command, stdin: &[u8]) -> Result<Output, Error> { diff --git a/src/routes.rs b/src/routes.rs index aca4284..7107a60 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -28,7 +28,7 @@ pub async fn routes( let response = match (method, path) { (&Method::HEAD, ["status"]) => controller::utils::ok(), - (&Method::GET, ["status"]) => controller::utils::text("ok".to_string()), + (&Method::GET, ["status"]) => controller::utils::text("ok"), (&Method::GET, ["login"]) => { controller::login::page(&assets, &templates, None).await } diff --git a/src/templates.rs b/src/templates.rs index 8f160dc..ebe8831 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -92,11 +92,11 @@ fn numeric(n: i64) -> String { format!("{}{}", sign, str) } -fn pluralize(n: i32, s: String) -> String { +fn pluralize(n: i32, s: &str) -> String { if n > 0 { format!("{s}s") } else { - s + s.to_string() } } @@ -104,7 +104,7 @@ fn round(n: f32) -> i32 { n.round() as i32 } -fn with_param(url: String, key: String, value: String) -> String { +fn with_param(url: &str, key: &str, value: String) -> String { if url.contains("?") { format!("{url}&{key}={value}") } else { @@ -130,8 +130,8 @@ fn filter( res } -fn rgrouped(str: String, n: usize) -> Vec<String> { - let mut str = str; +fn rgrouped(str: impl Into<String>, n: usize) -> Vec<String> { + let mut str = str.into(); let mut l = str.len(); let mut res = vec![]; while l > n { |