mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
Add TagList implementation for XML
This commit is contained in:
1
crates/dav/src/xml/mod.rs
Normal file
1
crates/dav/src/xml/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod tag_list;
|
||||||
64
crates/dav/src/xml/tag_list.rs
Normal file
64
crates/dav/src/xml/tag_list.rs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
use serde::ser::SerializeMap;
|
||||||
|
|
||||||
|
use serde::{
|
||||||
|
de::{MapAccess, Visitor},
|
||||||
|
Deserialize, Serialize,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub struct TagList(Vec<String>);
|
||||||
|
|
||||||
|
struct TagListVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for TagListVisitor {
|
||||||
|
type Value = TagList;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str("TagList")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut tags = Vec::new();
|
||||||
|
while let Some(key) = map.next_key::<String>()? {
|
||||||
|
tags.push(key);
|
||||||
|
}
|
||||||
|
Ok(TagList(tags))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for TagList {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
deserializer.deserialize_map(TagListVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for TagList {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
let mut map = serializer.serialize_map(Some(self.0.len()))?;
|
||||||
|
for name in &self.0 {
|
||||||
|
map.serialize_entry(&name, &())?;
|
||||||
|
}
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TagList> for Vec<String> {
|
||||||
|
fn from(value: TagList) -> Self {
|
||||||
|
value.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<String>> for TagList {
|
||||||
|
fn from(value: Vec<String>) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
48
crates/dav/tests/tag_list.rs
Normal file
48
crates/dav/tests/tag_list.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use rustical_dav::xml::tag_list::TagList;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
const INPUT: &str = r#"<Document>
|
||||||
|
<prop>
|
||||||
|
<nicename/>
|
||||||
|
<anotherprop/>
|
||||||
|
</prop>
|
||||||
|
</Document>"#;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
struct PropElement {
|
||||||
|
#[serde(flatten)]
|
||||||
|
tags: TagList,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
struct Document {
|
||||||
|
prop: PropElement,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expected_output() -> Document {
|
||||||
|
Document {
|
||||||
|
prop: PropElement {
|
||||||
|
tags: vec!["nicename".to_owned(), "anotherprop".to_owned()].into(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tagname_deserialize() {
|
||||||
|
let result: Document = quick_xml::de::from_str(INPUT).unwrap();
|
||||||
|
assert_eq!(result, expected_output());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tagname_serialize() {
|
||||||
|
let mut result = String::new();
|
||||||
|
let mut ser = quick_xml::se::Serializer::new(&mut result);
|
||||||
|
ser.indent(' ', 4);
|
||||||
|
|
||||||
|
let to_serialize = &expected_output();
|
||||||
|
to_serialize.serialize(ser).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(result, INPUT);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user