From fadadffc927015948d38824ea1d70810392182b9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 29 Feb 2024 22:32:07 +0100 Subject: [PATCH] Fixed tests --- src/dav/calencoder.rs | 29 ++++++---- src/dav/encoder.rs | 128 ++++++++++++++++++++++-------------------- 2 files changed, 86 insertions(+), 71 deletions(-) diff --git a/src/dav/calencoder.rs b/src/dav/calencoder.rs index 918083d..fbd696d 100644 --- a/src/dav/calencoder.rs +++ b/src/dav/calencoder.rs @@ -38,25 +38,34 @@ impl Context for CalCtx { Self { root: false } } fn create_dav_element(&self, name: &str) -> BytesStart { - let mut start = BytesStart::new(format!("D:{}", name)); - if self.root { - start.push_attribute(("xmlns:D", "DAV:")); - start.push_attribute(("xmlns:C", "urn:ietf:params:xml:ns:caldav")); - } - start + self.create_ns_element("D", name) } async fn hook_error(&self, err: &Violation, xml: &mut Writer) -> Result<(), QError> { err.write(xml, self.child()).await } } +impl CalCtx { + fn create_ns_element(&self, ns: &str, name: &str) -> BytesStart { + let mut start = BytesStart::new(format!("{}:{}", ns, name)); + if self.root { + start.push_attribute(("xmlns:D", "DAV:")); + start.push_attribute(("xmlns:C", "urn:ietf:params:xml:ns:caldav")); + } + start + } + fn create_cal_element(&self, name: &str) -> BytesStart { + self.create_ns_element("C", name) + } +} impl QuickWritable for Violation { - async fn write(&self, xml: &mut Writer, _ctx: CalCtx) -> Result<(), QError> { + async fn write(&self, xml: &mut Writer, ctx: CalCtx) -> Result<(), QError> { match self { - Self::SupportedFilter => xml - .create_element("supported-filter") - .write_empty_async().await?, + Self::SupportedFilter => { + let start = ctx.create_cal_element("supported-filter"); + xml.write_event_async(Event::Empty(start)).await?; + }, }; Ok(()) } diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index ddef533..e77e072 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -9,23 +9,21 @@ use super::types::*; //-------------- TRAITS ---------------------- -/*pub trait DavWriter { - fn create_dav_element(&mut self, name: &str) -> ElementWriter; - fn child(w: &mut QWriter) -> impl DavWriter; - async fn error(&mut self, err: &E::Error) -> Result<(), QError>; -}*/ /// Basic encode trait to make a type encodable pub trait QuickWritable> { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError>; } +/// Encoding context pub trait Context { 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>; } +/// -------------- NoExtension Encoding Context +/// (Might be tied to the type maybe) pub struct NoExtCtx { root: bool } @@ -70,33 +68,36 @@ impl> QuickWritable for Multistatus { // --- XML inner elements impl> QuickWritable for Href { - async fn write(&self, xml: &mut Writer, _ctx: C) -> Result<(), QError> { - xml.create_element("href") - .write_text_content_async(BytesText::new(&self.0)) - .await?; + async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { + let start = ctx.create_dav_element("href"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(&self.0))).await?; + xml.write_event_async(Event::End(end)).await?; + Ok(()) } } impl> QuickWritable for Response { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { - xml.create_element("response") - .write_inner_content_async::<_, _, QError>(|inner_xml| async move { - self.href.write(inner_xml, ctx.child()).await?; - self.status_or_propstat.write(inner_xml, ctx.child()).await?; - if let Some(error) = &self.error { - error.write(inner_xml, ctx.child()).await?; - } - if let Some(responsedescription) = &self.responsedescription { - responsedescription.write(inner_xml, ctx.child()).await?; - } - if let Some(location) = &self.location { - location.write(inner_xml, ctx.child()).await?; - } + let start = ctx.create_dav_element("href"); + let end = start.to_end(); - Ok(inner_xml) - }) - .await?; + xml.write_event_async(Event::Start(start.clone())).await?; + self.href.write(xml, ctx.child()).await?; + self.status_or_propstat.write(xml, ctx.child()).await?; + if let Some(error) = &self.error { + error.write(xml, ctx.child()).await?; + } + if let Some(responsedescription) = &self.responsedescription { + responsedescription.write(xml, ctx.child()).await?; + } + if let Some(location) = &self.location { + location.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; Ok(()) } @@ -119,11 +120,16 @@ impl> QuickWritable for StatusOrPropstat { impl> QuickWritable for Status { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { - xml.create_element("status") - .write_text_content_async( - BytesText::new(&format!("HTTP/1.1 {} {}", self.0.as_str(), self.0.canonical_reason().unwrap_or("No reason"))) - ) - .await?; + let start = ctx.create_dav_element("status"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + + let txt = format!("HTTP/1.1 {} {}", self.0.as_str(), self.0.canonical_reason().unwrap_or("No reason")); + xml.write_event_async(Event::Text(BytesText::new(&txt))).await?; + + xml.write_event_async(Event::End(end)).await?; + Ok(()) } } @@ -155,15 +161,14 @@ impl> QuickWritable for PropStat { impl> QuickWritable for Error { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { - xml.create_element("error") - .write_inner_content_async::<_, _, QError>(|inner_xml| async move { - for violation in &self.0 { - violation.write(inner_xml, ctx.child()).await?; - } + let start = ctx.create_dav_element("error"); + let end = start.to_end(); - Ok(inner_xml) - }) - .await?; + xml.write_event_async(Event::Start(start.clone())).await?; + for violation in &self.0 { + violation.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; Ok(()) } @@ -172,32 +177,33 @@ impl> QuickWritable for Error { impl> QuickWritable for Violation { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { match self { - Violation::LockTokenMatchesRequestUri => xml.create_element("lock-token-matches-request-uri").write_empty_async().await?, - Violation::LockTokenSubmitted(hrefs) => xml - .create_element("lock-token-submitted") - .write_inner_content_async::<_, _, QError>(|inner_xml| async move { - for href in hrefs { - href.write(inner_xml, ctx.child()).await?; - } - Ok(inner_xml) + Violation::LockTokenMatchesRequestUri => xml.write_event_async(Event::Empty(ctx.create_dav_element("lock-token-matches-request-uri"))).await?, + Violation::LockTokenSubmitted(hrefs) => { + let start = ctx.create_dav_element("lock-token-submitted"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + for href in hrefs { + href.write(xml, ctx.child()).await?; } - ).await?, - Violation::NoConflictingLock(hrefs) => xml - .create_element("no-conflicting-lock") - .write_inner_content_async::<_, _, QError>(|inner_xml| async move { - for href in hrefs { - href.write(inner_xml, ctx.child()).await?; - } - Ok(inner_xml) + xml.write_event_async(Event::End(end)).await?; + }, + Violation::NoConflictingLock(hrefs) => { + let start = ctx.create_dav_element("no-conflicting-lock"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + for href in hrefs { + href.write(xml, ctx.child()).await?; } - ).await?, - Violation::NoExternalEntities => xml.create_element("no-external-entities").write_empty_async().await?, - Violation::PreservedLiveProperties => xml.create_element("preserved-live-properties").write_empty_async().await?, - Violation::PropfindFiniteDepth => xml.create_element("propfind-finite-depth").write_empty_async().await?, - Violation::CannotModifyProtectedProperty => xml.create_element("cannot-modify-protected-property").write_empty_async().await?, + xml.write_event_async(Event::End(end)).await?; + }, + Violation::NoExternalEntities => xml.write_event_async(Event::Empty(ctx.create_dav_element("no-external-entities"))).await?, + Violation::PreservedLiveProperties => xml.write_event_async(Event::Empty(ctx.create_dav_element("preserved-live-properties"))).await?, + Violation::PropfindFiniteDepth => xml.write_event_async(Event::Empty(ctx.create_dav_element("propfind-finite-depth"))).await?, + Violation::CannotModifyProtectedProperty => xml.write_event_async(Event::Empty(ctx.create_dav_element("cannot-modify-protected-property"))).await?, Violation::Extension(inner) => { ctx.hook_error(inner, xml).await?; - xml }, }; Ok(()) @@ -218,7 +224,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: true }; + let ctx = NoExtCtx{ root: false }; Href("/SOGo/dav/so/".into()).write(&mut writer, ctx).await.expect("xml serialization"); tokio_buffer.flush().await.expect("tokio buffer flush");