2020-11-18 20:26:27 +00:00
|
|
|
use std::convert::TryInto;
|
2020-11-20 14:40:57 +00:00
|
|
|
use std::borrow::Borrow;
|
2020-11-18 20:26:27 +00:00
|
|
|
|
2020-11-14 03:43:29 +00:00
|
|
|
use anyhow::*;
|
2020-11-18 20:26:27 +00:00
|
|
|
use uriparse::URIReference;
|
2020-11-14 08:55:21 +00:00
|
|
|
use crate::types::{ResponseHeader, Body, Mime, Document};
|
2020-11-14 21:46:29 +00:00
|
|
|
use crate::util::Cowy;
|
2020-11-14 08:55:21 +00:00
|
|
|
use crate::GEMINI_MIME;
|
2020-11-14 03:43:29 +00:00
|
|
|
|
|
|
|
pub struct Response {
|
|
|
|
header: ResponseHeader,
|
|
|
|
body: Option<Body>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Response {
|
2020-11-19 22:58:33 +00:00
|
|
|
pub const fn new(header: ResponseHeader) -> Self {
|
2020-11-14 03:43:29 +00:00
|
|
|
Self {
|
|
|
|
header,
|
|
|
|
body: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:40:57 +00:00
|
|
|
pub fn document(document: impl Borrow<Document>) -> Self {
|
2020-11-18 02:41:18 +00:00
|
|
|
Self::success_with_body(&GEMINI_MIME, document)
|
2020-11-14 08:55:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 21:46:29 +00:00
|
|
|
pub fn input(prompt: impl Cowy<str>) -> Result<Self> {
|
2020-11-14 03:43:29 +00:00
|
|
|
let header = ResponseHeader::input(prompt)?;
|
|
|
|
Ok(Self::new(header))
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:46:29 +00:00
|
|
|
pub fn input_lossy(prompt: impl Cowy<str>) -> Self {
|
2020-11-14 03:43:29 +00:00
|
|
|
let header = ResponseHeader::input_lossy(prompt);
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn success(mime: &Mime) -> Self {
|
2020-11-19 22:58:33 +00:00
|
|
|
let header = ResponseHeader::success(mime);
|
2020-11-14 03:43:29 +00:00
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:26:27 +00:00
|
|
|
pub fn redirect_temporary_lossy<'a>(location: impl TryInto<URIReference<'a>>) -> Self {
|
|
|
|
let header = ResponseHeader::redirect_temporary_lossy(location);
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
2020-11-18 02:41:18 +00:00
|
|
|
/// Create a successful response with a preconfigured body
|
|
|
|
///
|
|
|
|
/// This is equivilent to:
|
|
|
|
///
|
2020-11-18 22:00:49 +00:00
|
|
|
/// ```ignore
|
2020-11-18 02:41:18 +00:00
|
|
|
/// Response::success(mime)
|
|
|
|
/// .with_body(body)
|
|
|
|
/// ```
|
|
|
|
pub fn success_with_body(mime: &Mime, body: impl Into<Body>) -> Self {
|
|
|
|
Self::success(mime)
|
|
|
|
.with_body(body)
|
|
|
|
}
|
|
|
|
|
2020-11-14 21:46:29 +00:00
|
|
|
pub fn server_error(reason: impl Cowy<str>) -> Result<Self> {
|
2020-11-14 03:43:29 +00:00
|
|
|
let header = ResponseHeader::server_error(reason)?;
|
|
|
|
Ok(Self::new(header))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_found() -> Self {
|
|
|
|
let header = ResponseHeader::not_found();
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:26:27 +00:00
|
|
|
pub fn bad_request_lossy(reason: impl Cowy<str>) -> Self {
|
|
|
|
let header = ResponseHeader::bad_request_lossy(reason);
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
2020-11-14 03:43:29 +00:00
|
|
|
pub fn client_certificate_required() -> Self {
|
|
|
|
let header = ResponseHeader::client_certificate_required();
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn certificate_not_authorized() -> Self {
|
|
|
|
let header = ResponseHeader::certificate_not_authorized();
|
|
|
|
Self::new(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_body(mut self, body: impl Into<Body>) -> Self {
|
|
|
|
self.body = Some(body.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-19 22:58:33 +00:00
|
|
|
pub const fn header(&self) -> &ResponseHeader {
|
2020-11-14 03:43:29 +00:00
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_body(&mut self) -> Option<Body> {
|
|
|
|
self.body.take()
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 14:51:37 +00:00
|
|
|
|
|
|
|
impl<D: Borrow<Document>> From<D> for Response {
|
|
|
|
fn from(doc: D) -> Self {
|
|
|
|
Self::document(doc)
|
|
|
|
}
|
|
|
|
}
|