From b2858ff245f0f1b8a16ff9ca9180eb305c5e4fee Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 2 Apr 2023 01:23:33 +0000 Subject: refactor(src): clean up string generics --- src/response.rs | 32 ++++++++++++++++++++------------ src/router.rs | 43 +++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/response.rs b/src/response.rs index 9c1629a..c693ac8 100644 --- a/src/response.rs +++ b/src/response.rs @@ -75,9 +75,9 @@ impl Response { response!(certificate_not_valid, 62); - pub fn success(content: S) -> Self - where S: Into + AsRef { - Self::new(20, content.into()) + #[allow(clippy::needless_pass_by_value)] + pub fn success(content: impl ToString) -> Self { + Self::new(20, content.to_string()) .with_mime("text/gemini") .with_language("en") .with_character_set("utf-8") @@ -85,7 +85,10 @@ impl Response { } #[must_use] - pub fn binary_success(content: &[u8], mime: &str) -> Self { + pub fn binary_success( + content: &[u8], + mime: impl Into + AsRef, + ) -> Self { Self::new(21, String::from_utf8_lossy(content)) .with_mime(mime) .clone() @@ -100,8 +103,7 @@ impl Response { } #[must_use] - pub fn new(status: i32, content: S) -> Self - where S: Into + AsRef { + pub fn new(status: i32, content: impl Into + AsRef) -> Self { Self { status, mime: None, @@ -111,22 +113,28 @@ impl Response { } } - pub fn with_mime(&mut self, mime: S) -> &mut Self - where S: Into + AsRef { + pub fn with_mime( + &mut self, + mime: impl Into + AsRef, + ) -> &mut Self { self.mime = Some(mime.into()); self } - pub fn with_character_set(&mut self, character_set: S) -> &mut Self - where S: Into + AsRef { + pub fn with_character_set( + &mut self, + character_set: impl Into + AsRef, + ) -> &mut Self { self.character_set = Some(character_set.into()); self } - pub fn with_language(&mut self, language: S) -> &mut Self - where S: Into + AsRef { + pub fn with_language( + &mut self, + language: impl Into + AsRef, + ) -> &mut Self { self.language = Some(language.into()); self diff --git a/src/router.rs b/src/router.rs index b2c0435..04e955e 100644 --- a/src/router.rs +++ b/src/router.rs @@ -93,13 +93,10 @@ impl Router { /// ```rust /// windmark::Router::new().set_private_key_file("windmark_private.pem"); /// ``` - pub fn set_private_key_file( + pub fn set_private_key_file( &mut self, - private_key_file_name: S, - ) -> &mut Self - where - S: Into + AsRef, - { + private_key_file_name: impl Into + AsRef, + ) -> &mut Self { self.private_key_file_name = private_key_file_name.into(); self @@ -112,8 +109,10 @@ impl Router { /// ```rust /// windmark::Router::new().set_certificate_file("windmark_public.pem"); /// ``` - pub fn set_certificate_file(&mut self, certificate_name: S) -> &mut Self - where S: Into + AsRef { + pub fn set_certificate_file( + &mut self, + certificate_name: impl Into + AsRef, + ) -> &mut Self { self.ca_file_name = certificate_name.into(); self @@ -140,8 +139,11 @@ impl Router { /// # Panics /// /// if the route cannot be mounted. - pub fn mount(&mut self, route: S, handler: RouteResponse) -> &mut Self - where S: Into + AsRef { + pub fn mount( + &mut self, + route: impl Into + AsRef, + handler: RouteResponse, + ) -> &mut Self { self .routes .insert(route.into(), Arc::new(Mutex::new(handler))) @@ -518,14 +520,11 @@ impl Router { /// // .set_log_level("your_crate_name=trace", false); /// ``` #[cfg(feature = "logger")] - pub fn set_log_level( + pub fn set_log_level( &mut self, - log_level: S, + log_level: impl Into + AsRef, log_windmark: bool, - ) -> &mut Self - where - S: Into + AsRef, - { + ) -> &mut Self { std::env::set_var( "RUST_LOG", format!( @@ -699,8 +698,10 @@ impl Router { /// ```rust /// windmark::Router::new().set_character_set("utf-8"); /// ``` - pub fn set_character_set(&mut self, character_set: S) -> &mut Self - where S: Into + AsRef { + pub fn set_character_set( + &mut self, + character_set: impl Into + AsRef, + ) -> &mut Self { self.character_set = character_set.into(); self @@ -717,8 +718,10 @@ impl Router { /// ```rust /// windmark::Router::new().set_language("en"); /// ``` - pub fn set_language(&mut self, language: S) -> &mut Self - where S: Into + AsRef { + pub fn set_language( + &mut self, + language: impl Into + AsRef, + ) -> &mut Self { self.language = language.into(); self -- cgit v1.2.3