Merge pull request #18 from Alch-Emi/success-with-body

Add Response::success_with_body
This commit is contained in:
panicbit 2020-11-18 09:15:14 +01:00 committed by GitHub
commit cab1cf8ad6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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> {