#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DavNamespace {
Dav,
OwnCloud,
Nextcloud,
}
impl DavNamespace {
pub const fn prefix(self) -> &'static str {
match self {
Self::Dav => "d",
Self::OwnCloud => "oc",
Self::Nextcloud => "nc",
}
}
pub const fn uri(self) -> &'static str {
match self {
Self::Dav => "DAV:",
Self::OwnCloud => "http://owncloud.org/ns",
Self::Nextcloud => "http://nextcloud.org/ns",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DavProperty {
Etag,
ContentLength,
ContentType,
ResourceType,
LastModified,
DisplayName,
FileId,
Permissions,
HasPreview,
}
impl DavProperty {
pub const fn namespace(self) -> DavNamespace {
match self {
Self::Etag
| Self::ContentLength
| Self::ContentType
| Self::ResourceType
| Self::LastModified
| Self::DisplayName => DavNamespace::Dav,
Self::FileId | Self::Permissions => DavNamespace::OwnCloud,
Self::HasPreview => DavNamespace::Nextcloud,
}
}
pub const fn local_name(self) -> &'static str {
match self {
Self::Etag => "getetag",
Self::ContentLength => "getcontentlength",
Self::ContentType => "getcontenttype",
Self::ResourceType => "resourcetype",
Self::LastModified => "getlastmodified",
Self::DisplayName => "displayname",
Self::FileId => "fileid",
Self::Permissions => "permissions",
Self::HasPreview => "has-preview",
}
}
pub const fn qualified_name(self) -> &'static str {
match self {
Self::Etag => "d:getetag",
Self::ContentLength => "d:getcontentlength",
Self::ContentType => "d:getcontenttype",
Self::ResourceType => "d:resourcetype",
Self::LastModified => "d:getlastmodified",
Self::DisplayName => "d:displayname",
Self::FileId => "oc:fileid",
Self::Permissions => "oc:permissions",
Self::HasPreview => "nc:has-preview",
}
}
}
#[cfg(test)]
mod tests {
use super::{DavNamespace, DavProperty};
const ALL: [DavProperty; 9] = [
DavProperty::Etag,
DavProperty::ContentLength,
DavProperty::ContentType,
DavProperty::ResourceType,
DavProperty::LastModified,
DavProperty::DisplayName,
DavProperty::FileId,
DavProperty::Permissions,
DavProperty::HasPreview,
];
#[test]
fn every_property_renders_its_namespaced_tag() {
let expected = [
"d:getetag",
"d:getcontentlength",
"d:getcontenttype",
"d:resourcetype",
"d:getlastmodified",
"d:displayname",
"oc:fileid",
"oc:permissions",
"nc:has-preview",
];
for (property, tag) in ALL.iter().zip(expected) {
assert_eq!(property.qualified_name(), tag);
let prefix = property.namespace().prefix();
assert_eq!(
property.qualified_name(),
format!("{prefix}:{}", property.local_name())
);
}
}
#[test]
fn namespaces_carry_the_prefix_and_uri_they_are_declared_with() {
assert_eq!(DavNamespace::Dav.prefix(), "d");
assert_eq!(DavNamespace::Dav.uri(), "DAV:");
assert_eq!(DavNamespace::OwnCloud.uri(), "http://owncloud.org/ns");
assert_eq!(DavNamespace::Nextcloud.uri(), "http://nextcloud.org/ns");
}
#[test]
fn request_tags_match_the_response_tags() {
assert_eq!(DavProperty::HasPreview.qualified_name(), "nc:has-preview");
assert_eq!(DavProperty::FileId.qualified_name(), "oc:fileid");
}
}