Skip to content

Commit

Permalink
Merge pull request #30 from Alch-Emi/doc-into-req
Browse files Browse the repository at this point in the history
Allow direct conversion for Document -> Response
  • Loading branch information
panicbit committed Nov 22, 2020
2 parents 6930a06 + 20181cc commit 3a999a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- `document` API for creating Gemini documents
- preliminary timeout API, incl a special case for complex MIMEs by [@Alch-Emi](https://github.com/Alch-Emi)
- `Response::success_with_body` by [@Alch-Emi](https://github.com/Alch-Emi)
- preliminary timeout API, incl a special case for complex MIMEs by [@Alch-Emi]
- `Response::success_with_body` by [@Alch-Emi]
- `redirect_temporary_lossy` for `Response` and `ResponseHeader`
- `bad_request_lossy` for `Response` and `ResponseHeader`
- support for a lot more mime-types in `guess_mime_from_path`, backed by the `mime_guess` crate
- customizable TLS cert & key paths by [@Alch-Emi](https://github.com/Alch-Emi)
- `server_dir` default feature for serve_dir utils [@Alch-Emi](https://github.com/Alch-Emi)
- customizable TLS cert & key paths by [@Alch-Emi]
- `server_dir` default feature for serve_dir utils [@Alch-Emi]
- Docments can be converted into responses with std::convert::Into [@Alch-Emi]
### Improved
- build time and size by [@Alch-Emi](https://github.com/Alch-Emi)
- build time and size by [@Alch-Emi]

## [0.3.0] - 2020-11-14
### Added
Expand All @@ -34,4 +35,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.2.0] - 2020-11-14
### Added
- Access to client certificates by [@Alch-Emi](https://github.com/Alch-Emi)
- Access to client certificates by [@Alch-Emi]

[@Alch-Emi]: https://github.com/Alch-Emi
10 changes: 4 additions & 6 deletions examples/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ async fn main() -> Result<()> {

fn handle_request(_request: Request) -> BoxFuture<'static, Result<Response>> {
async move {
let mut document = Document::new();

document
let response = Document::new()
.add_preformatted(include_str!("northstar_logo.txt"))
.add_blank_line()
.add_link("https://docs.rs/northstar", "Documentation")
Expand All @@ -43,9 +41,9 @@ fn handle_request(_request: Request) -> BoxFuture<'static, Result<Response>> {
.add_preformatted_with_alt("sh", concat!(
"mkdir cert && cd cert\n",
"openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365",
));

Ok(Response::document(document))
))
.into();
Ok(response)
}
.boxed()
}
8 changes: 5 additions & 3 deletions src/types/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ use tokio::io::AsyncRead;
#[cfg(feature="serve_dir")]
use tokio::fs::File;

use std::borrow::Borrow;

use crate::types::Document;

pub enum Body {
Bytes(Vec<u8>),
Reader(Box<dyn AsyncRead + Send + Sync + Unpin>),
}

impl From<Document> for Body {
fn from(document: Document) -> Self {
Self::from(document.to_string())
impl<D: Borrow<Document>> From<D> for Body {
fn from(document: D) -> Self {
Self::from(document.borrow().to_string())
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/types/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryInto;
use std::borrow::Borrow;

use anyhow::*;
use uriparse::URIReference;
Expand All @@ -19,7 +20,7 @@ impl Response {
}
}

pub fn document(document: Document) -> Self {
pub fn document(document: impl Borrow<Document>) -> Self {
Self::success_with_body(&GEMINI_MIME, document)
}

Expand Down Expand Up @@ -94,3 +95,9 @@ impl Response {
self.body.take()
}
}

impl<D: Borrow<Document>> From<D> for Response {
fn from(doc: D) -> Self {
Self::document(doc)
}
}

0 comments on commit 3a999a1

Please sign in to comment.