Skip to main content

nextcloud_dav/method/
overwrite.rs

1/// Header specifying whether to overwrite a destination resource (RFC 4918 §10.6).
2pub const OVERWRITE_HEADER: &str = "Overwrite";
3
4/// Behaviour when the destination resource already exists (RFC 4918 §10.6).
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6pub enum Overwrite {
7    /// Overwrite the target if it already exists (`"T"`).
8    Allow,
9    /// Do not overwrite the target if it already exists (`"F"`).
10    Forbidden,
11}
12
13impl Overwrite {
14    /// Wire representation for the [`OVERWRITE_HEADER`] value.
15    pub const fn as_header_value(self) -> &'static str {
16        match self {
17            Self::Allow => "T",
18            Self::Forbidden => "F",
19        }
20    }
21}
22
23impl From<bool> for Overwrite {
24    fn from(value: bool) -> Self {
25        if value { Self::Allow } else { Self::Forbidden }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::{OVERWRITE_HEADER, Overwrite};
32
33    #[test]
34    fn header_values_use_the_specification_tokens() {
35        assert_eq!(Overwrite::Allow.as_header_value(), "T");
36        assert_eq!(Overwrite::Forbidden.as_header_value(), "F");
37    }
38
39    #[test]
40    fn header_name_keeps_its_canonical_spelling() {
41        assert_eq!(OVERWRITE_HEADER, "Overwrite");
42    }
43
44    #[test]
45    fn boolean_conversion_matches_specification() {
46        assert_eq!(Overwrite::from(true), Overwrite::Allow);
47        assert_eq!(Overwrite::from(false), Overwrite::Forbidden);
48    }
49}