aerogramme/src/dav/calencoder.rs

100 lines
3.2 KiB
Rust
Raw Normal View History

2024-02-29 19:40:40 +00:00
use super::encoder::{QuickWritable, Context};
use super::caltypes::*;
use super::types::Extension;
use quick_xml::Error as QError;
use quick_xml::events::{Event, BytesEnd, BytesStart, BytesText};
use quick_xml::writer::{ElementWriter, Writer};
use quick_xml::name::PrefixDeclaration;
use tokio::io::AsyncWrite;
2024-03-01 07:32:02 +00:00
impl Context for CalExtension {
2024-02-29 19:40:40 +00:00
fn child(&self) -> Self {
Self { root: false }
}
fn create_dav_element(&self, name: &str) -> BytesStart {
2024-02-29 21:32:07 +00:00
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
}
2024-03-01 09:12:19 +00:00
async fn hook_property(&self, prop: &Self::Property, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError> {
prop.write(xml, self.child()).await
}
2024-03-01 09:29:16 +00:00
async fn hook_resourcetype(&self, restype: &Self::ResourceType, xml: &mut Writer<impl AsyncWrite+Unpin>) -> Result<(), QError> {
restype.write(xml, self.child()).await
}
2024-02-29 21:32:07 +00:00
}
2024-03-01 07:32:02 +00:00
impl CalExtension {
2024-02-29 21:32:07 +00:00
fn create_ns_element(&self, ns: &str, name: &str) -> BytesStart {
let mut start = BytesStart::new(format!("{}:{}", ns, name));
2024-02-29 19:40:40 +00:00
if self.root {
start.push_attribute(("xmlns:D", "DAV:"));
start.push_attribute(("xmlns:C", "urn:ietf:params:xml:ns:caldav"));
}
start
}
2024-02-29 21:32:07 +00:00
fn create_cal_element(&self, name: &str) -> BytesStart {
self.create_ns_element("C", name)
2024-02-29 19:40:40 +00:00
}
}
2024-03-01 07:32:02 +00:00
impl QuickWritable<CalExtension> for Violation {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalExtension) -> Result<(), QError> {
2024-02-29 19:40:40 +00:00
match self {
2024-02-29 21:32:07 +00:00
Self::SupportedFilter => {
let start = ctx.create_cal_element("supported-filter");
xml.write_event_async(Event::Empty(start)).await?;
},
2024-02-29 19:40:40 +00:00
};
Ok(())
}
}
2024-03-01 09:12:19 +00:00
impl QuickWritable<CalExtension> for Property {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalExtension) -> Result<(), QError> {
unimplemented!();
}
}
2024-03-01 09:29:16 +00:00
impl QuickWritable<CalExtension> for ResourceType {
async fn write(&self, xml: &mut Writer<impl AsyncWrite+Unpin>, ctx: CalExtension) -> Result<(), QError> {
match self {
Self::Calendar => xml.write_event_async(Event::Empty(ctx.create_dav_element("calendar"))).await,
}
}
}
2024-02-29 19:40:40 +00:00
#[cfg(test)]
mod tests {
use super::*;
use crate::dav::types::{Error, Violation as DavViolation};
use tokio::io::AsyncWriteExt;
#[tokio::test]
async fn test_violation() {
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
2024-03-01 07:32:02 +00:00
let res = Error(vec![
2024-02-29 19:40:40 +00:00
DavViolation::Extension(Violation::SupportedFilter),
]);
2024-03-01 07:32:02 +00:00
res.write(&mut writer, CalExtension { root: true }).await.expect("xml serialization");
2024-02-29 19:40:40 +00:00
tokio_buffer.flush().await.expect("tokio buffer flush");
let expected = r#"<D:error xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
<C:supported-filter/>
</D:error>"#;
let got = std::str::from_utf8(buffer.as_slice()).unwrap();
assert_eq!(got, expected);
}
}