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 }
}
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<impl AsyncWrite+Unpin>) -> 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<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 {
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(())
}

View file

@ -9,23 +9,21 @@ use super::types::*;
//-------------- 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
pub trait QuickWritable<E: Extension, C: Context<E>> {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: C) -> Result<(), QError>;
}
/// Encoding context
pub trait Context<E: 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>;
}
/// -------------- NoExtension Encoding Context
/// (Might be tied to the type maybe)
pub struct NoExtCtx {
root: bool
}
@ -70,33 +68,36 @@ 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 {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, _ctx: C) -> Result<(), QError> {
xml.create_element("href")
.write_text_content_async(BytesText::new(&self.0))
.await?;
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();
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<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> {
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<E: Extension, C: Context<E>> QuickWritable<E,C> for StatusOrPropstat<E> {
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> {
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<E: Extension, C: Context<E>> QuickWritable<E,C> for PropStat<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> {
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<E: Extension, C: Context<E>> QuickWritable<E,C> for Error<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> {
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");