61 lines
1.4 KiB
Rust
61 lines
1.4 KiB
Rust
use tokio::io::AsyncRead;
|
|
#[cfg(feature="serve_dir")]
|
|
use tokio::fs::File;
|
|
|
|
#[cfg(feature = "gemtext")]
|
|
use crate::Gemtext;
|
|
|
|
pub enum Body {
|
|
Bytes(Vec<u8>),
|
|
Reader(Box<dyn AsyncRead + Send + Sync + Unpin>),
|
|
}
|
|
|
|
#[cfg(feature = "gemtext")]
|
|
#[allow(clippy::fallible_impl_from)] // It's really not fallible but thanks
|
|
impl From<Vec<gemtext::Node>> for Body {
|
|
fn from(document: Vec<gemtext::Node>) -> Self {
|
|
let size: usize = document.iter().map(gemtext::Node::estimate_len).sum();
|
|
let mut bytes = Vec::with_capacity(size + document.len());
|
|
gemtext::render(document, &mut bytes).unwrap(); // Safe: we're only writing to a buffer
|
|
Self::Bytes(bytes)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "gemtext")]
|
|
impl From<Gemtext> for Body {
|
|
fn from(document: Gemtext) -> Self {
|
|
document.build().into()
|
|
}
|
|
}
|
|
|
|
impl From<Vec<u8>> for Body {
|
|
fn from(bytes: Vec<u8>) -> Self {
|
|
Self::Bytes(bytes)
|
|
}
|
|
}
|
|
|
|
impl<'a> From<&'a [u8]> for Body {
|
|
fn from(bytes: &[u8]) -> Self {
|
|
Self::Bytes(bytes.to_owned())
|
|
}
|
|
}
|
|
|
|
impl From<String> for Body {
|
|
fn from(text: String) -> Self {
|
|
Self::Bytes(text.into_bytes())
|
|
}
|
|
}
|
|
|
|
impl<'a> From<&'a str> for Body {
|
|
fn from(text: &str) -> Self {
|
|
Self::Bytes(text.to_owned().into_bytes())
|
|
}
|
|
}
|
|
|
|
#[cfg(feature="serve_dir")]
|
|
impl From<File> for Body {
|
|
fn from(file: File) -> Self {
|
|
Self::Reader(Box::new(file))
|
|
}
|
|
}
|