Skip to main content

nextcloud_dav/xml/
missing.rs

1//! The error a decode step raises for an element it required.
2
3use std::error::Error;
4use std::fmt;
5
6/// An element a decode step required but could not find.
7///
8/// Carries the qualified name so a [`DecodeError`](nextcloud_sdk_core::DecodeError)
9/// wrapping it says *which* element was absent, not just that decoding failed.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct MissingElement {
12    name: String,
13}
14
15impl MissingElement {
16    /// Records the name that was not found.
17    pub fn new(name: &str) -> Self {
18        Self {
19            name: name.to_owned(),
20        }
21    }
22
23    /// Qualified name that was not found.
24    pub fn name(&self) -> &str {
25        &self.name
26    }
27}
28
29impl fmt::Display for MissingElement {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "missing element `{}`", self.name)
32    }
33}
34
35impl Error for MissingElement {}
36
37#[cfg(test)]
38mod tests {
39    use std::error::Error;
40
41    use super::MissingElement;
42
43    #[test]
44    fn names_the_element_it_reports() {
45        assert_eq!(MissingElement::new("d:href").name(), "d:href");
46        assert!(MissingElement::new("d:href").to_string().contains("d:href"));
47    }
48
49    #[test]
50    fn is_a_standard_error_usable_as_a_source() {
51        let error: Box<dyn Error + Send + Sync> = Box::new(MissingElement::new("d:multistatus"));
52        assert!(error.to_string().contains("d:multistatus"));
53    }
54}