use std::convert::TryInto; use anyhow::*; use uriparse::URIReference; use crate::types::{ResponseHeader, Body, Mime}; use crate::util::Cowy; use crate::GEMINI_MIME; pub struct Response { header: ResponseHeader, body: Option, } impl Response { pub const fn new(header: ResponseHeader) -> Self { Self { header, body: None, } } pub fn input(prompt: impl Cowy) -> Result { let header = ResponseHeader::input(prompt)?; Ok(Self::new(header)) } pub fn input_lossy(prompt: impl Cowy) -> Self { let header = ResponseHeader::input_lossy(prompt); Self::new(header) } pub fn redirect_temporary_lossy<'a>(location: impl TryInto>) -> Self { let header = ResponseHeader::redirect_temporary_lossy(location); Self::new(header) } /// Create a successful response with a given body and MIME pub fn success(mime: &Mime, body: impl Into) -> Self { Self { header: ResponseHeader::success(mime), body: Some(body.into()), } } /// Create a successful response with a `text/gemini` MIME pub fn success_gemini(body: impl Into) -> Self { Self::success(&GEMINI_MIME, body) } /// Create a successful response with a `text/plain` MIME pub fn success_plain(body: impl Into) -> Self { Self::success(&mime::TEXT_PLAIN, body) } pub fn server_error(reason: impl Cowy) -> Result { let header = ResponseHeader::server_error(reason)?; Ok(Self::new(header)) } pub fn not_found() -> Self { let header = ResponseHeader::not_found(); Self::new(header) } pub fn bad_request_lossy(reason: impl Cowy) -> Self { let header = ResponseHeader::bad_request_lossy(reason); Self::new(header) } 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) -> Self { self.body = Some(body.into()); self } pub const fn header(&self) -> &ResponseHeader { &self.header } pub fn take_body(&mut self) -> Option { self.body.take() } } #[cfg(feature = "gemtext")] impl>> From for Response { fn from(doc: D) -> Self { Self::success_gemini(doc.into()) } }