Skip to main content

nextcloud_ocs/query/
format.rs

1use std::fmt;
2
3/// Serialisation format to request from an OCS endpoint.
4///
5/// OCS answers in XML unless asked otherwise, so a JSON client must always
6/// send `format=json` — this enum exists to keep that requirement visible
7/// instead of burying it in a string literal.
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
9pub enum OcsFormat {
10    /// `format=json`
11    #[default]
12    Json,
13    /// `format=xml`, OCS's default when the parameter is absent.
14    Xml,
15}
16
17impl OcsFormat {
18    /// Value of the `format` query parameter.
19    pub const fn as_str(self) -> &'static str {
20        match self {
21            Self::Json => "json",
22            Self::Xml => "xml",
23        }
24    }
25
26    /// Name of the parameter carrying the format.
27    pub const PARAM: &'static str = "format";
28}
29
30impl fmt::Display for OcsFormat {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.write_str(self.as_str())
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::OcsFormat;
39
40    #[test]
41    fn json_is_the_default() {
42        assert_eq!(OcsFormat::default(), OcsFormat::Json);
43        assert_eq!(OcsFormat::Json.to_string(), "json");
44        assert_eq!(OcsFormat::Xml.to_string(), "xml");
45    }
46}