use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TrashItem {
pub id: String,
pub href: String,
pub filename: String,
pub original_location: String,
pub deletion_time: Option<u64>,
pub last_modified: Option<String>,
pub is_collection: bool,
}
impl TrashItem {
pub fn id(&self) -> &str {
&self.id
}
pub fn href(&self) -> &str {
&self.href
}
pub fn filename(&self) -> &str {
&self.filename
}
pub fn original_filename(&self) -> &str {
&self.filename
}
pub fn original_location(&self) -> &str {
&self.original_location
}
pub fn deletion_time(&self) -> Option<u64> {
self.deletion_time
}
pub fn last_modified(&self) -> Option<&str> {
self.last_modified.as_deref()
}
pub fn is_collection(&self) -> bool {
self.is_collection
}
}
impl fmt::Display for TrashItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = if self.is_collection {
"collection"
} else {
"file"
};
write!(f, "{kind} {} (id: {})", self.filename, self.id)
}
}
#[cfg(test)]
mod tests {
use super::TrashItem;
#[test]
fn item_accessors_and_display() {
let item = TrashItem {
id: "report.pdf.d1609459200".to_owned(),
href: "/remote.php/dav/trashbin/alice/trash/report.pdf.d1609459200".to_owned(),
filename: "report.pdf".to_owned(),
original_location: "Documents/report.pdf".to_owned(),
deletion_time: Some(1609459200),
last_modified: Some("Fri, 01 Jan 2021 00:00:00 GMT".to_owned()),
is_collection: false,
};
assert_eq!(item.id(), "report.pdf.d1609459200");
assert_eq!(
item.href(),
"/remote.php/dav/trashbin/alice/trash/report.pdf.d1609459200"
);
assert_eq!(item.filename(), "report.pdf");
assert_eq!(item.original_filename(), "report.pdf");
assert_eq!(item.original_location(), "Documents/report.pdf");
assert_eq!(item.deletion_time(), Some(1609459200));
assert_eq!(item.last_modified(), Some("Fri, 01 Jan 2021 00:00:00 GMT"));
assert!(!item.is_collection());
let rendered = item.to_string();
assert_eq!(rendered, "file report.pdf (id: report.pdf.d1609459200)");
let dir = TrashItem {
id: "Photos.d1609459300".to_owned(),
href: "/remote.php/dav/trashbin/alice/trash/Photos.d1609459300/".to_owned(),
filename: "Photos".to_owned(),
original_location: "Photos".to_owned(),
deletion_time: None,
last_modified: None,
is_collection: true,
};
assert!(dir.is_collection());
assert_eq!(
dir.to_string(),
"collection Photos (id: Photos.d1609459300)"
);
}
}