Fixed tests

This commit is contained in:
Quentin 2024-02-29 22:32:07 +01:00
parent 1e3737a590
commit fadadffc92
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 86 additions and 71 deletions

View file

@ -38,25 +38,34 @@ impl Context<CalExtension> for CalCtx {
Self { root: false } Self { root: false }
} }
fn create_dav_element(&self, name: &str) -> BytesStart { fn create_dav_element(&self, name: &str) -> BytesStart {
let mut start = BytesStart::new(format!("D:{}", name)); self.create_ns_element("D", name)
if self.root {
start.push_attribute(("xmlns:D", "DAV:"));
start.push_attribute(("xmlns:C", "urn:ietf:params:xml:ns:caldav"));
}
start
} }
async fn hook_error(&self, err: &Violation, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError> { async fn hook_error(&self, err: &Violation, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError> {
err.write(xml, self.child()).await 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<CalExtension, CalCtx> for Violation { impl QuickWritable<CalExtension, CalCtx> for Violation {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, _ctx: CalCtx) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalCtx) -> Result<(), QError> {
match self { match self {
Self::SupportedFilter => xml Self::SupportedFilter => {
.create_element("supported-filter") let start = ctx.create_cal_element("supported-filter");
.write_empty_async().await?, xml.write_event_async(Event::Empty(start)).await?;
},
}; };
Ok(()) Ok(())
} }

View file

@ -9,23 +9,21 @@ use super::types::*;
//-------------- TRAITS ---------------------- //-------------- TRAITS ----------------------
/*pub trait DavWriter<E: Extension> {
fn create_dav_element(&mut self, name: &str) -> ElementWriter<impl AsyncWrite + Unpin>;
fn child(w: &mut QWriter<impl AsyncWrite + Unpin>) -> impl DavWriter<E>;
async fn error(&mut self, err: &E::Error) -> Result<(), QError>;
}*/
/// Basic encode trait to make a type encodable /// Basic encode trait to make a type encodable
pub trait QuickWritable<E: Extension, C: Context<E>> { pub trait QuickWritable<E: Extension, C: Context<E>> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError>; async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError>;
} }
/// Encoding context
pub trait Context<E: Extension> { pub trait Context<E: Extension> {
fn child(&self) -> Self; fn child(&self) -> Self;
fn create_dav_element(&self, name: &str) -> BytesStart; 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: &E::Error, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError>;
} }
/// -------------- NoExtension Encoding Context
/// (Might be tied to the type maybe)
pub struct NoExtCtx { pub struct NoExtCtx {
root: bool root: bool
} }
@ -70,33 +68,36 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Multistatus<E> {
// --- XML inner elements // --- XML inner elements
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Href { impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Href {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, _ctx: C) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
xml.create_element("href") let start = ctx.create_dav_element("href");
.write_text_content_async(BytesText::new(&self.0)) let end = start.to_end();
.await?;
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(()) Ok(())
} }
} }
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Response<E> { impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Response<E> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
xml.create_element("response") let start = ctx.create_dav_element("href");
.write_inner_content_async::<_, _, QError>(|inner_xml| async move { let end = start.to_end();
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?;
}
Ok(inner_xml) xml.write_event_async(Event::Start(start.clone())).await?;
}) self.href.write(xml, ctx.child()).await?;
.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(()) Ok(())
} }
@ -119,11 +120,16 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for StatusOrPropstat<E> {
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Status { impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Status {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
xml.create_element("status") let start = ctx.create_dav_element("status");
.write_text_content_async( let end = start.to_end();
BytesText::new(&format!("HTTP/1.1 {} {}", self.0.as_str(), self.0.canonical_reason().unwrap_or("No reason")))
) xml.write_event_async(Event::Start(start.clone())).await?;
.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(()) Ok(())
} }
} }
@ -155,15 +161,14 @@ impl<E: Extension, C: Context<E>> QuickWritable<E,C> for PropStat<E> {
impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Error<E> { impl<E: Extension, C: Context<E>> QuickWritable<E,C> for Error<E> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
xml.create_element("error") let start = ctx.create_dav_element("error");
.write_inner_content_async::<_, _, QError>(|inner_xml| async move { let end = start.to_end();
for violation in &self.0 {
violation.write(inner_xml, ctx.child()).await?;
}
Ok(inner_xml) xml.write_event_async(Event::Start(start.clone())).await?;
}) for violation in &self.0 {
.await?; violation.write(xml, ctx.child()).await?;
}
xml.write_event_async(Event::End(end)).await?;
Ok(()) Ok(())
} }
@ -172,32 +177,33 @@ 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<E: Extension, C: Context<E>> QuickWritable<E,C> for Violation<E> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> { async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError> {
match self { match self {
Violation::LockTokenMatchesRequestUri => xml.create_element("lock-token-matches-request-uri").write_empty_async().await?, Violation::LockTokenMatchesRequestUri => xml.write_event_async(Event::Empty(ctx.create_dav_element("lock-token-matches-request-uri"))).await?,
Violation::LockTokenSubmitted(hrefs) => xml Violation::LockTokenSubmitted(hrefs) => {
.create_element("lock-token-submitted") let start = ctx.create_dav_element("lock-token-submitted");
.write_inner_content_async::<_, _, QError>(|inner_xml| async move { let end = start.to_end();
for href in hrefs {
href.write(inner_xml, ctx.child()).await?; xml.write_event_async(Event::Start(start.clone())).await?;
} for href in hrefs {
Ok(inner_xml) href.write(xml, ctx.child()).await?;
} }
).await?, xml.write_event_async(Event::End(end)).await?;
Violation::NoConflictingLock(hrefs) => xml },
.create_element("no-conflicting-lock") Violation::NoConflictingLock(hrefs) => {
.write_inner_content_async::<_, _, QError>(|inner_xml| async move { let start = ctx.create_dav_element("no-conflicting-lock");
for href in hrefs { let end = start.to_end();
href.write(inner_xml, ctx.child()).await?;
} xml.write_event_async(Event::Start(start.clone())).await?;
Ok(inner_xml) for href in hrefs {
href.write(xml, ctx.child()).await?;
} }
).await?, xml.write_event_async(Event::End(end)).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::NoExternalEntities => xml.write_event_async(Event::Empty(ctx.create_dav_element("no-external-entities"))).await?,
Violation::PropfindFiniteDepth => xml.create_element("propfind-finite-depth").write_empty_async().await?, Violation::PreservedLiveProperties => xml.write_event_async(Event::Empty(ctx.create_dav_element("preserved-live-properties"))).await?,
Violation::CannotModifyProtectedProperty => xml.create_element("cannot-modify-protected-property").write_empty_async().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) => { Violation::Extension(inner) => {
ctx.hook_error(inner, xml).await?; ctx.hook_error(inner, xml).await?;
xml
}, },
}; };
Ok(()) Ok(())
@ -218,7 +224,7 @@ mod tests {
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer); let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4); 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"); Href("/SOGo/dav/so/".into()).write(&mut writer, ctx).await.expect("xml serialization");
tokio_buffer.flush().await.expect("tokio buffer flush"); tokio_buffer.flush().await.expect("tokio buffer flush");