From e88e448179239fcc41aa03efcbfb2cc2ab0c922e Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 1 Mar 2024 08:32:02 +0100 Subject: Simplify code --- src/dav/calencoder.rs | 16 +++++++--------- src/dav/caltypes.rs | 4 +++- src/dav/encoder.rs | 42 +++++++++++++++++++----------------------- src/dav/types.rs | 5 ++--- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/dav/calencoder.rs b/src/dav/calencoder.rs index aabdf36..378d2bd 100644 --- a/src/dav/calencoder.rs +++ b/src/dav/calencoder.rs @@ -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 for CalCtx { +impl Context for CalExtension { fn child(&self) -> Self { Self { root: false } } @@ -23,7 +20,8 @@ impl Context 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 for Violation { - async fn write(&self, xml: &mut Writer, ctx: CalCtx) -> Result<(), QError> { +impl QuickWritable for Violation { + async fn write(&self, xml: &mut Writer, 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 = 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#" diff --git a/src/dav/caltypes.rs b/src/dav/caltypes.rs index d913f95..c672370 100644 --- a/src/dav/caltypes.rs +++ b/src/dav/caltypes.rs @@ -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; diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index 2c9fdac..399a92e 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -11,23 +11,19 @@ use super::types::*; //-------------- TRAITS ---------------------- /// Basic encode trait to make a type encodable -pub trait QuickWritable> { +pub trait QuickWritable { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError>; } /// Encoding context -pub trait Context { +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) -> Result<(), QError>; + async fn hook_error(&self, err: &Self::Error, xml: &mut Writer) -> Result<(), QError>; } /// -------------- NoExtension Encoding Context -/// (Might be tied to the type maybe) -pub struct NoExtCtx { - root: bool -} -impl Context for NoExtCtx { +impl Context for NoExtension { fn child(&self) -> Self { Self { root: false } } @@ -47,7 +43,7 @@ impl Context for NoExtCtx { //--------------------- ENCODING -------------------- // --- XML ROOTS -impl> QuickWritable for Multistatus { +impl QuickWritable for Multistatus { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("multistatus"); let end = start.to_end(); @@ -67,7 +63,7 @@ impl> QuickWritable for Multistatus { // --- XML inner elements -impl> QuickWritable for Href { +impl QuickWritable for Href { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("href"); let end = start.to_end(); @@ -80,7 +76,7 @@ impl> QuickWritable for Href { } } -impl> QuickWritable for Response { +impl QuickWritable for Response { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("href"); let end = start.to_end(); @@ -103,7 +99,7 @@ impl> QuickWritable for Response { } } -impl> QuickWritable for StatusOrPropstat { +impl QuickWritable for StatusOrPropstat { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { match self { Self::Status(status) => status.write(xml, ctx.child()).await, @@ -118,7 +114,7 @@ impl> QuickWritable for StatusOrPropstat { } } -impl> QuickWritable for Status { +impl QuickWritable for Status { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("status"); let end = start.to_end(); @@ -134,7 +130,7 @@ impl> QuickWritable for Status { } } -impl> QuickWritable for ResponseDescription { +impl QuickWritable for ResponseDescription { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("responsedescription"); let end = start.to_end(); @@ -147,7 +143,7 @@ impl> QuickWritable for ResponseDescription { } } -impl> QuickWritable for Location { +impl QuickWritable for Location { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("location"); let end = start.to_end(); @@ -160,7 +156,7 @@ impl> QuickWritable for Location { } } -impl> QuickWritable for PropStat { +impl QuickWritable for PropStat { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("propstat"); let end = start.to_end(); @@ -180,7 +176,7 @@ impl> QuickWritable for PropStat { } } -impl> QuickWritable for Prop { +impl QuickWritable for Prop { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("prop"); let end = start.to_end(); @@ -196,7 +192,7 @@ impl> QuickWritable for Prop { } -impl> QuickWritable for Property { +impl QuickWritable for Property { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { use Property::*; match self { @@ -211,7 +207,7 @@ impl> QuickWritable for Property { -impl> QuickWritable for Error { +impl QuickWritable for Error { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("error"); let end = start.to_end(); @@ -226,7 +222,7 @@ impl> QuickWritable for Error { } } -impl> QuickWritable for Violation { +impl QuickWritable for Violation { async fn write(&self, xml: &mut Writer, 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 = 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"); diff --git a/src/dav/types.rs b/src/dav/types.rs index b0c0083..8807658 100644 --- a/src/dav/types.rs +++ b/src/dav/types.rs @@ -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; -- cgit v1.2.3