Initial commit

This commit is contained in:
Aleksandr 2025-09-19 18:31:17 +03:00
commit bd9b07052b
81 changed files with 5516 additions and 0 deletions

63
http/src/format.rs Normal file
View file

@ -0,0 +1,63 @@
use eva::{data, error::ShitHappens};
use eyre::Context;
#[data(copy, display("got unsupported mime type"), error)]
pub struct UnknownMimeType;
#[data(copy, display("{}", self.mime_type()))]
#[derive(Default)]
pub enum Format {
#[default]
Json,
}
impl Format {
pub fn from_mime_type(mime: &str) -> Result<Self, UnknownMimeType> {
use Format::*;
use UnknownMimeType as E;
match mime {
"application/json" => Ok(Json),
_ => Err(E),
}
}
pub const fn mime_type(self) -> &'static str {
use Format::*;
match self {
Json => "application/json",
}
}
}
#[data]
#[derive(Default)]
pub struct DumpParams {
pub pretty: bool,
}
impl Format {
pub fn dump<T: ?Sized>(self, params: DumpParams, what: &T, dst: &mut Vec<u8>)
where
T: serde::Serialize,
{
match self {
Self::Json => if params.pretty {
serde_json::to_writer_pretty(dst, what)
} else {
serde_json::to_writer(dst, what)
}
.shit_happens(),
}
}
pub fn load<'de, T>(&self, buf: &'de [u8]) -> eyre::Result<T>
where
T: serde::Deserialize<'de>,
{
match self {
Self::Json => serde_json::from_slice(buf).wrap_err("failed to deserialize JSON"),
}
}
}