diff --git a/examples/certificates.rs b/examples/certificates.rs index 2da1e68..f0f98b3 100644 --- a/examples/certificates.rs +++ b/examples/certificates.rs @@ -37,8 +37,10 @@ fn handle_request(users: Arc>>, request: Reque if let Some(user) = users_read.get(cert_bytes) { // The user has already registered Ok( - Response::success(&GEMINI_MIME) - .with_body(format!("Welcome {}!", user)) + Response::success_with_body( + &GEMINI_MIME, + format!("Welcome {}!", user) + ) ) } else { // The user still needs to register @@ -49,11 +51,13 @@ fn handle_request(users: Arc>>, request: Reque let mut users_write = users.write().await; users_write.insert(cert_bytes.clone(), username.to_owned()); Ok( - Response::success(&GEMINI_MIME) - .with_body(format!( + Response::success_with_body( + &GEMINI_MIME, + format!( "Your account has been created {}! Welcome!", username - )) + ) + ) ) } else { // The user didn't provide input, and should be prompted diff --git a/src/types/response.rs b/src/types/response.rs index a76d6a1..74e36a3 100644 --- a/src/types/response.rs +++ b/src/types/response.rs @@ -17,7 +17,7 @@ impl Response { } pub fn document(document: Document) -> Self { - Self::success(&GEMINI_MIME).with_body(document) + Self::success_with_body(&GEMINI_MIME, document) } pub fn input(prompt: impl Cowy) -> Result { @@ -35,6 +35,19 @@ impl Response { Self::new(header) } + /// Create a successful response with a preconfigured body + /// + /// This is equivilent to: + /// + /// ```norun + /// Response::success(mime) + /// .with_body(body) + /// ``` + pub fn success_with_body(mime: &Mime, body: impl Into) -> Self { + Self::success(mime) + .with_body(body) + } + pub fn server_error(reason: impl Cowy) -> Result { let header = ResponseHeader::server_error(reason)?; Ok(Self::new(header)) diff --git a/src/util.rs b/src/util.rs index 2b1358a..9db296f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -20,7 +20,7 @@ pub async fn serve_file>(path: P, mime: &Mime) -> Result, P: AsRef>(dir: D, virtual_path: &[P]) -> Result {