nextcloud_trashbin/
item.rs1use std::fmt;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct TrashItem {
13 pub id: String,
15 pub href: String,
17 pub filename: String,
19 pub original_location: String,
21 pub deletion_time: Option<u64>,
23 pub last_modified: Option<String>,
25 pub is_collection: bool,
27}
28
29impl TrashItem {
30 pub fn id(&self) -> &str {
32 &self.id
33 }
34
35 pub fn href(&self) -> &str {
37 &self.href
38 }
39
40 pub fn filename(&self) -> &str {
42 &self.filename
43 }
44
45 pub fn original_filename(&self) -> &str {
47 &self.filename
48 }
49
50 pub fn original_location(&self) -> &str {
52 &self.original_location
53 }
54
55 pub fn deletion_time(&self) -> Option<u64> {
57 self.deletion_time
58 }
59
60 pub fn last_modified(&self) -> Option<&str> {
62 self.last_modified.as_deref()
63 }
64
65 pub fn is_collection(&self) -> bool {
67 self.is_collection
68 }
69}
70
71impl fmt::Display for TrashItem {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 let kind = if self.is_collection {
74 "collection"
75 } else {
76 "file"
77 };
78 write!(f, "{kind} {} (id: {})", self.filename, self.id)
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::TrashItem;
85
86 #[test]
87 fn item_accessors_and_display() {
88 let item = TrashItem {
89 id: "report.pdf.d1609459200".to_owned(),
90 href: "/remote.php/dav/trashbin/alice/trash/report.pdf.d1609459200".to_owned(),
91 filename: "report.pdf".to_owned(),
92 original_location: "Documents/report.pdf".to_owned(),
93 deletion_time: Some(1609459200),
94 last_modified: Some("Fri, 01 Jan 2021 00:00:00 GMT".to_owned()),
95 is_collection: false,
96 };
97
98 assert_eq!(item.id(), "report.pdf.d1609459200");
99 assert_eq!(
100 item.href(),
101 "/remote.php/dav/trashbin/alice/trash/report.pdf.d1609459200"
102 );
103 assert_eq!(item.filename(), "report.pdf");
104 assert_eq!(item.original_filename(), "report.pdf");
105 assert_eq!(item.original_location(), "Documents/report.pdf");
106 assert_eq!(item.deletion_time(), Some(1609459200));
107 assert_eq!(item.last_modified(), Some("Fri, 01 Jan 2021 00:00:00 GMT"));
108 assert!(!item.is_collection());
109
110 let rendered = item.to_string();
111 assert_eq!(rendered, "file report.pdf (id: report.pdf.d1609459200)");
112
113 let dir = TrashItem {
114 id: "Photos.d1609459300".to_owned(),
115 href: "/remote.php/dav/trashbin/alice/trash/Photos.d1609459300/".to_owned(),
116 filename: "Photos".to_owned(),
117 original_location: "Photos".to_owned(),
118 deletion_time: None,
119 last_modified: None,
120 is_collection: true,
121 };
122 assert!(dir.is_collection());
123 assert_eq!(
124 dir.to_string(),
125 "collection Photos (id: Photos.d1609459300)"
126 );
127 }
128}