From 929a185f371a7f369c4437d47b5c9e9f414678cf Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 1 Mar 2024 10:12:19 +0100 Subject: Add a property hook --- src/dav/encoder.rs | 146 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 135 insertions(+), 11 deletions(-) (limited to 'src/dav/encoder.rs') diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index 5392b18..332c13a 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -20,6 +20,7 @@ pub trait Context: Extension { fn child(&self) -> Self; fn create_dav_element(&self, name: &str) -> BytesStart; async fn hook_error(&self, err: &Self::Error, xml: &mut Writer) -> Result<(), QError>; + async fn hook_property(&self, prop: &Self::Property, xml: &mut Writer) -> Result<(), QError>; } /// -------------- NoExtension Encoding Context @@ -37,6 +38,9 @@ impl Context for NoExtension { async fn hook_error(&self, err: &Disabled, xml: &mut Writer) -> Result<(), QError> { unreachable!(); } + async fn hook_property(&self, prop: &Disabled, xml: &mut Writer) -> Result<(), QError> { + unreachable!(); + } } @@ -196,23 +200,143 @@ impl QuickWritable for Property { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { use Property::*; match self { - CreationDate(date) => unimplemented!(), - DisplayName(name) => unimplemented!(), - GetContentLanguage(lang) => unimplemented!(), - GetContentLength(len) => unimplemented!(), - GetContentType(ct) => unimplemented!(), - GetEtag(et) => unimplemented!(), - GetLastModified(dt) => unimplemented!(), - LockDiscovery(locks) => unimplemented!(), - ResourceType(res) => unimplemented!(), - SupportedLock(sup) => unimplemented!(), - Extension(inner) => unimplemented!(), + CreationDate(date) => { + // 1997-12-01T17:42:21-08:00 + let start = ctx.create_dav_element("creationdate"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(&date.to_rfc3339()))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + DisplayName(name) => { + // Example collection + let start = ctx.create_dav_element("displayname"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(name))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + GetContentLanguage(lang) => { + let start = ctx.create_dav_element("getcontentlanguage"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(lang))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + GetContentLength(len) => { + // 4525 + let start = ctx.create_dav_element("getcontentlength"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(&len.to_string()))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + GetContentType(ct) => { + // text/html + let start = ctx.create_dav_element("getcontenttype"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(&ct))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + GetEtag(et) => { + // "zzyzx" + let start = ctx.create_dav_element("getetag"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(et))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + GetLastModified(date) => { + // Mon, 12 Jan 1998 09:25:56 GMT + let start = ctx.create_dav_element("getlastmodified"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + xml.write_event_async(Event::Text(BytesText::new(&date.to_rfc3339()))).await?; + xml.write_event_async(Event::End(end)).await?; + }, + LockDiscovery(many_locks) => { + // ... + let start = ctx.create_dav_element("lockdiscovery"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + for lock in many_locks.iter() { + lock.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; + }, + ResourceType(many_types) => { + // + + // + + // + // + // + // + + let start = ctx.create_dav_element("resourcetype"); + if many_types.is_empty() { + xml.write_event_async(Event::Empty(start)).await?; + } else { + let end = start.to_end(); + xml.write_event_async(Event::Start(start.clone())).await?; + for restype in many_types.iter() { + restype.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; + } + }, + SupportedLock(many_entries) => { + // + + // ... + + let start = ctx.create_dav_element("supportedlock"); + if many_entries.is_empty() { + xml.write_event_async(Event::Empty(start)).await?; + } else { + let end = start.to_end(); + xml.write_event_async(Event::Start(start.clone())).await?; + for entry in many_entries.iter() { + entry.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; + } + }, + Extension(inner) => { + ctx.hook_property(inner, xml).await?; + }, }; Ok(()) } } +impl QuickWritable for ActiveLock { + async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { + unimplemented!(); + } +} + +impl QuickWritable for ResourceType { + async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { + unimplemented!(); + } +} +impl QuickWritable for LockEntry { + async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { + unimplemented!(); + } +} impl QuickWritable for Error { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { -- cgit v1.2.3