webdav_xml/
element.rs

1// SPDX-FileCopyrightText: d-k-bo <d-k-bo@mailbox.org>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5/// Declares an element's namespace and tag
6pub trait Element {
7    /// XML namespace of the element, e.g. `DAV:`
8    const NAMESPACE: &'static str;
9    /// The prefix used to abbreviate the namespace, e.g. `d`
10    const PREFIX: &'static str;
11    /// The local name of the element (the name inside the namespace), e.g.
12    /// `multistatus`
13    const LOCAL_NAME: &'static str;
14}
15
16pub(crate) trait ElementExt: Element {
17    fn element_name<S>() -> ElementName<S>
18    where
19        S: From<&'static str>,
20    {
21        ElementName {
22            namespace: Some(Self::NAMESPACE.into()),
23            prefix: Some(Self::PREFIX.into()),
24            local_name: Self::LOCAL_NAME.into(),
25        }
26    }
27}
28
29impl<T: Element> ElementExt for T {}
30
31#[derive(Clone, Debug)]
32pub struct ElementName<S = &'static str> {
33    pub namespace: Option<S>,
34    pub prefix: Option<S>,
35    pub local_name: S,
36}
37
38impl indexmap::Equivalent<ElementName<bytestring::ByteString>> for ElementName<&str> {
39    fn equivalent(&self, key: &ElementName<bytestring::ByteString>) -> bool {
40        self.namespace == key.namespace.as_deref() && self.local_name == &*key.local_name
41    }
42}
43
44impl<S: PartialEq> PartialEq<ElementName<S>> for ElementName<S> {
45    fn eq(&self, other: &Self) -> bool {
46        self.namespace == other.namespace && self.local_name == other.local_name
47    }
48}
49
50impl<S: PartialEq> Eq for ElementName<S> {}
51
52impl<S: std::hash::Hash> std::hash::Hash for ElementName<S> {
53    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
54        (&self.namespace, &self.local_name).hash(state)
55    }
56}