Skip to main content

nextcloud_versions/
version.rs

1//! Metadata representing a past revision of a Nextcloud file.
2
3use std::fmt;
4
5/// A past revision of a Nextcloud file.
6///
7/// Returned by [`VersionsClient::list`](crate::VersionsClient::list).
8/// Contains the WebDAV path to the revision, the revision identifier (timestamp),
9/// and standard WebDAV properties such as last modification time, size in bytes,
10/// and ETag.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct FileVersion {
13    /// Server-relative WebDAV path to this revision.
14    pub href: String,
15    /// Identifier of this revision, typically a Unix timestamp.
16    pub revision_id: String,
17    /// Last modified timestamp as an HTTP-date string (e.g. `Fri, 01 Jan 2021 00:00:00 GMT`), if reported.
18    pub last_modified: Option<String>,
19    /// Size of the revision in bytes, if reported.
20    pub content_length: Option<u64>,
21    /// Entity tag of this revision, if reported.
22    pub etag: Option<String>,
23}
24
25impl FileVersion {
26    /// Creates a new `FileVersion`.
27    pub fn new(
28        href: impl Into<String>,
29        revision_id: impl Into<String>,
30        last_modified: Option<String>,
31        content_length: Option<u64>,
32        etag: Option<String>,
33    ) -> Self {
34        Self {
35            href: href.into(),
36            revision_id: revision_id.into(),
37            last_modified,
38            content_length,
39            etag,
40        }
41    }
42
43    /// Server-relative WebDAV path to this revision.
44    pub fn href(&self) -> &str {
45        &self.href
46    }
47
48    /// Revision identifier.
49    pub fn revision_id(&self) -> &str {
50        &self.revision_id
51    }
52
53    /// Modification timestamp as an HTTP-date string, if available.
54    pub fn last_modified(&self) -> Option<&str> {
55        self.last_modified.as_deref()
56    }
57
58    /// Byte length of the revision content, if available.
59    pub fn content_length(&self) -> Option<u64> {
60        self.content_length
61    }
62
63    /// Entity tag of the revision, if available.
64    pub fn etag(&self) -> Option<&str> {
65        self.etag.as_deref()
66    }
67}
68
69impl fmt::Display for FileVersion {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(
72            f,
73            "FileVersion(revision_id={}, href={})",
74            self.revision_id, self.href
75        )
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::FileVersion;
82
83    #[test]
84    fn file_version_accessors_and_display() {
85        let version = FileVersion::new(
86            "/remote.php/dav/versions/alice/versions/123/1609459200",
87            "1609459200",
88            Some("Fri, 01 Jan 2021 00:00:00 GMT".to_string()),
89            Some(1024),
90            Some("\"abcdef\"".to_string()),
91        );
92
93        assert_eq!(
94            version.href(),
95            "/remote.php/dav/versions/alice/versions/123/1609459200"
96        );
97        assert_eq!(version.revision_id(), "1609459200");
98        assert_eq!(
99            version.last_modified(),
100            Some("Fri, 01 Jan 2021 00:00:00 GMT")
101        );
102        assert_eq!(version.content_length(), Some(1024));
103        assert_eq!(version.etag(), Some("\"abcdef\""));
104        assert_eq!(
105            version.to_string(),
106            "FileVersion(revision_id=1609459200, href=/remote.php/dav/versions/alice/versions/123/1609459200)"
107        );
108    }
109}