diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-02-28 22:00:47 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-02-28 22:00:47 +0100 |
commit | ffe4d071f6e7c63f585953741e0fa8cb4ad10488 (patch) | |
tree | 2d91fa034a7720dbcb73b7e1cd598eda0d106c77 /src/dav/encoder.rs | |
parent | c10eb33585bbe92f6cfd0f111c989cc8fda4666c (diff) | |
download | aerogramme-ffe4d071f6e7c63f585953741e0fa8cb4ad10488.tar.gz aerogramme-ffe4d071f6e7c63f585953741e0fa8cb4ad10488.zip |
Dav XML types
Diffstat (limited to 'src/dav/encoder.rs')
-rw-r--r-- | src/dav/encoder.rs | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/src/dav/encoder.rs b/src/dav/encoder.rs index 9bc564a..28f807a 100644 --- a/src/dav/encoder.rs +++ b/src/dav/encoder.rs @@ -1,16 +1,48 @@ +use std::io::Cursor; +use anyhow::Result; +use quick_xml::events::{Event, BytesEnd, BytesStart, BytesText}; +use quick_xml::writer::{ElementWriter, Writer}; +use quick_xml::name::PrefixDeclaration; +use tokio::io::AsyncWrite; +use super::types::*; + +//@FIXME a cleaner way to manager namespace would be great +//but at the same time, the quick-xml library is not cooperating. +//So instead of writing many cursed workarounds - I tried, I am just hardcoding the namespaces... pub trait Encode { - fn write(&self, a: &mut u64) -> String; + async fn write(&self, xml: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()>; +} + +impl Encode for Href { + async fn write(&self, xml: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> { + xml.create_element("D:href") + .write_text_content_async(BytesText::new(&self.0)) + .await?; + + Ok(()) + } } #[cfg(test)] mod tests { - // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; + use tokio::io::AsyncWriteExt; + + /// To run only the unit tests and avoid the behavior ones: + /// cargo test --bin aerogramme + + #[tokio::test] + async fn test_href() { + 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); + + Href("/SOGo/dav/so/".into()).write(&mut writer).await.expect("xml serialization"); + tokio_buffer.flush().await.expect("tokio buffer flush"); - #[test] - fn test_href() { + assert_eq!(buffer.as_slice(), &b"<D:href>/SOGo/dav/so/</D:href>"[..]); } } |