Added Response::success_with_body

This commit is contained in:
Emi Tatsuo 2020-11-17 21:41:18 -05:00
parent 387ca06611
commit 6a78b2f31a
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
3 changed files with 24 additions and 7 deletions

View File

@ -37,8 +37,10 @@ fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, 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<RwLock<HashMap<CertBytes, String>>>, 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

View File

@ -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<str>) -> Result<Self> {
@ -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<Body>) -> Self {
Self::success(mime)
.with_body(body)
}
pub fn server_error(reason: impl Cowy<str>) -> Result<Self> {
let header = ResponseHeader::server_error(reason)?;
Ok(Self::new(header))

View File

@ -20,7 +20,7 @@ pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &Mime) -> Result<Response
}
};
Ok(Response::success(&mime).with_body(file))
Ok(Response::success_with_body(mime, file))
}
pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P]) -> Result<Response> {