Simplify code

This commit is contained in:
Quentin 2024-03-01 08:32:02 +01:00
parent 33a02ff695
commit e88e448179
Signed by: quentin
GPG key ID: E9602264D639FF68
4 changed files with 31 additions and 36 deletions

View file

@ -8,10 +8,7 @@ use quick_xml::writer::{ElementWriter, Writer};
use quick_xml::name::PrefixDeclaration;
use tokio::io::AsyncWrite;
pub struct CalCtx {
root: bool
}
impl Context<CalExtension> for CalCtx {
impl Context for CalExtension {
fn child(&self) -> Self {
Self { root: false }
}
@ -23,7 +20,8 @@ impl Context<CalExtension> for CalCtx {
err.write(xml, self.child()).await
}
}
impl CalCtx {
impl CalExtension {
fn create_ns_element(&self, ns: &str, name: &str) -> BytesStart {
let mut start = BytesStart::new(format!("{}:{}", ns, name));
if self.root {
@ -37,8 +35,8 @@ impl CalCtx {
}
}
impl QuickWritable<CalExtension, CalCtx> for Violation {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalCtx) -> Result<(), QError> {
impl QuickWritable<CalExtension> for Violation {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalExtension) -> Result<(), QError> {
match self {
Self::SupportedFilter => {
let start = ctx.create_cal_element("supported-filter");
@ -70,11 +68,11 @@ mod tests {
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
let res: Error<CalExtension> = Error(vec![
let res = Error(vec![
DavViolation::Extension(Violation::SupportedFilter),
]);
res.write(&mut writer, CalCtx{ root: true }).await.expect("xml serialization");
res.write(&mut writer, CalExtension { root: true }).await.expect("xml serialization");
tokio_buffer.flush().await.expect("tokio buffer flush");
let expected = r#"<D:error xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">

View file

@ -1,7 +1,9 @@
use super::types::*;
pub struct CalExtension {}
pub struct CalExtension {
pub root: bool
}
impl Extension for CalExtension {
type Error = Violation;
type Property = Property;

View file

@ -11,23 +11,19 @@ use super::types::*;
//-------------- TRAITS ----------------------
/// Basic encode trait to make a type encodable
pub trait QuickWritable<E: Extension, C: Context<E>> {
pub trait QuickWritable<C: Context> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError>;
}
/// Encoding context
pub trait Context<E: Extension> {
pub trait Context: Extension {
fn child(&self) -> Self;
fn create_dav_element(&self, name: &str) -> BytesStart;
async fn hook_error(&self, err: &E::Error, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError>;
async fn hook_error(&self, err: &Self::Error, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError>;
}
/// -------------- NoExtension Encoding Context
/// (Might be tied to the type maybe)
pub struct NoExtCtx {
root: bool
}
impl Context<NoExtension> for NoExtCtx {
impl Context for NoExtension {
fn child(&self) -> Self {
Self { root: false }
}
@ -47,7 +43,7 @@ impl Context<NoExtension> for NoExtCtx {
//--------------------- ENCODING --------------------
// --- XML ROOTS
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Multistatus<E> {
impl<C: Context> QuickWritable<C> for Multistatus<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("multistatus");
let end = start.to_end();
@ -67,7 +63,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Multistatus<E> {
// --- XML inner elements
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Href {
impl<C: Context> QuickWritable<C> for Href {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("href");
let end = start.to_end();
@ -80,7 +76,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Href {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Response<E> {
impl<C: Context> QuickWritable<C> for Response<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("href");
let end = start.to_end();
@ -103,7 +99,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Response<E> {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for StatusOrPropstat<E> {
impl<C: Context> QuickWritable<C> for StatusOrPropstat<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
match self {
Self::Status(status) => status.write(xml, ctx.child()).await,
@ -118,7 +114,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for StatusOrPropstat<E> {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Status {
impl<C: Context> QuickWritable<C> for Status {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("status");
let end = start.to_end();
@ -134,7 +130,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Status {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for ResponseDescription {
impl<C: Context> QuickWritable<C> for ResponseDescription {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("responsedescription");
let end = start.to_end();
@ -147,7 +143,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for ResponseDescription {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Location {
impl<C: Context> QuickWritable<C> for Location {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("location");
let end = start.to_end();
@ -160,7 +156,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Location {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for PropStat<E> {
impl<C: Context> QuickWritable<C> for PropStat<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("propstat");
let end = start.to_end();
@ -180,7 +176,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for PropStat<E> {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Prop<E> {
impl<C: Context> QuickWritable<C> for Prop<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("prop");
let end = start.to_end();
@ -196,7 +192,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Prop<E> {
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Property<E> {
impl<C: Context> QuickWritable<C> for Property<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
use Property::*;
match self {
@ -211,7 +207,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Property<E> {
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Error<E> {
impl<C: Context> QuickWritable<C> for Error<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
let start = ctx.create_dav_element("error");
let end = start.to_end();
@ -226,7 +222,7 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Error<E> {
}
}
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Violation<E> {
impl<C: Context> QuickWritable<C> for Violation<C> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
match self {
Violation::LockTokenMatchesRequestUri => xml.write_event_async(Event::Empty(ctx.create_dav_element("lock-token-matches-request-uri"))).await?,
@ -276,7 +272,7 @@ mod tests {
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
let ctx = NoExtCtx{ root: false };
let ctx = NoExtension { root: false };
Href("/SOGo/dav/so/".into()).write(&mut writer, ctx).await.expect("xml serialization");
tokio_buffer.flush().await.expect("tokio buffer flush");
@ -290,8 +286,8 @@ mod tests {
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
let ctx = NoExtCtx{ root: true };
let xml: Multistatus<NoExtension> = Multistatus { responses: vec![], responsedescription: Some(ResponseDescription("Hello world".into())) };
let ctx = NoExtension { root: true };
let xml = Multistatus { responses: vec![], responsedescription: Some(ResponseDescription("Hello world".into())) };
xml.write(&mut writer, ctx).await.expect("xml serialization");
tokio_buffer.flush().await.expect("tokio buffer flush");

View file

@ -11,9 +11,8 @@ pub trait Extension {
}
/// No extension
pub struct NoExtension {}
pub enum Namespace {
Dav
pub struct NoExtension {
pub root: bool
}
impl Extension for NoExtension {
type Error = Disabled;