From 33a02ff695c57fe88d394ad4d556bb390934ccd6 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 29 Feb 2024 23:02:02 +0100 Subject: WIP encoder --- src/dav/calencoder.rs | 22 -------------- src/dav/caltypes.rs | 24 +++++---------- src/dav/encoder.rs | 56 ++++++++++++++++++++++++++++++++-- src/dav/types.rs | 83 ++++++++++++++++++++++++--------------------------- 4 files changed, 101 insertions(+), 84 deletions(-) diff --git a/src/dav/calencoder.rs b/src/dav/calencoder.rs index fbd696d..aabdf36 100644 --- a/src/dav/calencoder.rs +++ b/src/dav/calencoder.rs @@ -8,28 +8,6 @@ use quick_xml::writer::{ElementWriter, Writer}; use quick_xml::name::PrefixDeclaration; use tokio::io::AsyncWrite; -/*pub trait CalWriter: DavWriter { - fn create_cal_element(&mut self, name: &str) -> ElementWriter; -} - -impl<'a, W: AsyncWrite+Unpin> DavWriter for Writer<'a, W, CalExtension> { - fn create_dav_element(&mut self, name: &str) -> ElementWriter { - self.create_ns_element(name, Namespace::Dav) - } - fn child(w: &'a mut QWriter) -> impl DavWriter { - Self::child(w) - } - async fn error(&mut self, err: &Violation) -> Result<(), QError> { - err.write(self).await - } -} - -impl<'a, W: AsyncWrite+Unpin> CalWriter for Writer<'a, W, CalExtension> { - fn create_cal_element(&mut self, name: &str) -> ElementWriter { - self.create_ns_element(name, Namespace::CalDav) - } -}*/ - pub struct CalCtx { root: bool } diff --git a/src/dav/caltypes.rs b/src/dav/caltypes.rs index ed8496a..d913f95 100644 --- a/src/dav/caltypes.rs +++ b/src/dav/caltypes.rs @@ -1,25 +1,11 @@ use super::types::*; -pub enum Namespace { - Dav, - CalDav, -} pub struct CalExtension {} impl Extension for CalExtension { type Error = Violation; - type Namespace = Namespace; - - fn namespaces() -> &'static [(&'static str, &'static str)] { - return &[ ("D", "DAV:"), ("C", "urn:ietf:params:xml:ns:caldav") ][..] - } - - fn short_ns(ns: Self::Namespace) -> &'static str { - match ns { - Namespace::Dav => "D", - Namespace::CalDav => "C", - } - } + type Property = Property; + type PropertyRequest = Property; //@FIXME } pub enum Violation { @@ -39,3 +25,9 @@ pub enum Violation { /// param-filter*)> SupportedFilter, } + + +pub enum Property { + CalendarDescription, + CalendarTimezone, +} diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index e77e072..2c9fdac 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -149,16 +149,68 @@ impl> QuickWritable for ResponseDescription { impl> QuickWritable for Location { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { - unimplemented!(); + let start = ctx.create_dav_element("location"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + self.0.write(xml, ctx.child()).await?; + xml.write_event_async(Event::End(end)).await?; + + Ok(()) } } impl> QuickWritable for PropStat { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { - unimplemented!(); + let start = ctx.create_dav_element("propstat"); + let end = start.to_end(); + + xml.write_event_async(Event::Start(start.clone())).await?; + self.prop.write(xml, ctx.child()).await?; + self.status.write(xml, ctx.child()).await?; + if let Some(error) = &self.error { + error.write(xml, ctx.child()).await?; + } + if let Some(description) = &self.responsedescription { + description.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; + + Ok(()) } } +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(); + + xml.write_event_async(Event::Start(start.clone())).await?; + for property in &self.0 { + property.write(xml, ctx.child()).await?; + } + xml.write_event_async(Event::End(end)).await?; + + Ok(()) + } +} + + +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!(), + //@FIXME not finished + _ => unimplemented!(), + }; + Ok(()) + } +} + + + impl> QuickWritable for Error { async fn write(&self, xml: &mut Writer, ctx: C) -> Result<(), QError> { let start = ctx.create_dav_element("error"); diff --git a/src/dav/types.rs b/src/dav/types.rs index 69ddf52..b0c0083 100644 --- a/src/dav/types.rs +++ b/src/dav/types.rs @@ -6,10 +6,8 @@ use chrono::{DateTime,FixedOffset}; pub struct Disabled(()); pub trait Extension { type Error; - type Namespace; - - fn namespaces() -> &'static [(&'static str, &'static str)]; - fn short_ns(ns: Self::Namespace) -> &'static str; + type Property; + type PropertyRequest; } /// No extension @@ -19,16 +17,9 @@ pub enum Namespace { } impl Extension for NoExtension { type Error = Disabled; - type Namespace = Namespace; - - fn namespaces() -> &'static [(&'static str, &'static str)] { - return &[ ("D", "DAV:") ][..] - } - - fn short_ns(ns: Self::Namespace) -> &'static str { - "D" - } -} + type Property = Disabled; + type PropertyRequest = Disabled; + } /// 14.1. activelock XML Element /// @@ -38,11 +29,11 @@ impl Extension for NoExtension { /// pub struct ActiveLock { - lockscope: LockScope, - locktype: LockType, - depth: Depth, - owner: Option, - timeout: Option, + pub lockscope: LockScope, + pub locktype: LockType, + pub depth: Depth, + pub owner: Option, + pub timeout: Option, } /// 14.2 allprop XML Element @@ -225,7 +216,7 @@ pub struct Href(pub String); /// standards. This element MUST NOT contain text or mixed content. /// /// -pub struct Include(Vec); +pub struct Include(pub Vec>); /// 14.9. location XML Element /// @@ -241,7 +232,7 @@ pub struct Include(Vec); /// that would be used in a Location header. /// /// -pub struct Location(Href); +pub struct Location(pub Href); /// 14.10. lockentry XML Element /// @@ -252,8 +243,8 @@ pub struct Location(Href); /// /// pub struct LockEntry { - lokscope: LockScope, - locktype: LockType, + pub lokscope: LockScope, + pub locktype: LockType, } /// 14.11. lockinfo XML Element @@ -265,9 +256,9 @@ pub struct LockEntry { /// /// pub struct LockInfo { - lockscope: LockScope, - locktype: LockType, - owner: Option, + pub lockscope: LockScope, + pub locktype: LockType, + pub owner: Option, } /// 14.12. lockroot XML Element @@ -282,7 +273,7 @@ pub struct LockInfo { /// values and the response to LOCK requests. /// /// -pub struct LockRoot(Href); +pub struct LockRoot(pub Href); /// 14.13. lockscope XML Element /// @@ -369,7 +360,7 @@ pub struct Multistatus { /// text content or attributes. /// /// -pub struct Owner(String); +pub struct Owner(pub String); /// 14.18. prop XML Element /// @@ -385,7 +376,7 @@ pub struct Owner(String); /// text or mixed content. /// /// -pub struct Prop(Vec); +pub struct Prop(pub Vec>); /// 14.19. propertyupdate XML Element /// @@ -397,10 +388,10 @@ pub struct Prop(Vec); /// required to modify the properties on the resource. /// /// -pub struct PropertyUpdate(Vec); -pub enum PropertyUpdateItem { - Remove(Remove), - Set(Set), +pub struct PropertyUpdate(Vec>); +pub enum PropertyUpdateItem { + Remove(Remove), + Set(Set), } /// 14.20. propfind XML Element @@ -414,10 +405,10 @@ pub enum PropertyUpdateItem { /// values. /// /// -pub enum PropFind { +pub enum PropFind { PropName(PropName), - AllProp(AllProp, Option), - Prop(Prop), + AllProp(AllProp, Option>), + Prop(Prop), } /// 14.21. propname XML Element @@ -446,10 +437,10 @@ pub struct PropName {} /// /// pub struct PropStat { - prop: Prop, - status: Status, - error: Option>, - responsedescription: Option, + pub prop: Prop, + pub status: Status, + pub error: Option>, + pub responsedescription: Option, } /// 14.23. remove XML Element @@ -465,7 +456,7 @@ pub struct PropStat { /// the names of properties to be removed are required. /// /// -pub struct Remove(Prop); +pub struct Remove(pub Prop); /// 14.24. response XML Element /// @@ -530,7 +521,7 @@ pub struct ResponseDescription(pub String); /// property, and MUST be subsequently retrievable using PROPFIND. /// /// -pub struct Set(Prop); +pub struct Set(pub Prop); /// 14.27. shared XML Element /// @@ -595,7 +586,7 @@ pub struct Timeout(u64); /// the header value could include LWS as defined in [RFC2616], Section /// 4.2. Server implementors SHOULD strip LWS from these values before /// using as WebDAV property values. -pub enum PropertyRequest { +pub enum PropertyRequest { CreationDate, DisplayName, GetContentLanguage, @@ -606,8 +597,9 @@ pub enum PropertyRequest { LockDiscovery, ResourceType, SupportedLock, + Extension(T::PropertyRequest), } -pub enum Property { +pub enum Property { /// 15.1. creationdate Property /// /// Name: creationdate @@ -884,6 +876,9 @@ pub enum Property { /// /// SupportedLock(Vec), + + /// Any extension + Extension(T::Property), } -- cgit v1.2.3