1use time::{format_description::BorrowedFormatItem, macros::format_description, PrimitiveDateTime};
2
3use crate::parser::models::{ContentEntry, DesktopFile, Entry, EntrySet, Group, TopLevelEntry};
4
5const DATE_FORMAT: &[BorrowedFormatItem] =
6 format_description!("[year]-[month]-[day]T[hour repr:24]:[minute]:[second]");
7
8const GROUP_NAME: &str = "Trash Info";
9
10#[cfg_attr(docsrs, doc(cfg(feature = "trash")))]
11#[derive(Debug, Eq, PartialEq, Clone)]
13pub struct TrashFile {
14 desktop_file: DesktopFile,
15 pub path: String,
17 pub deletion_date: PrimitiveDateTime,
19}
20
21impl TryFrom<TrashFile> for DesktopFile {
22 type Error = crate::error::Error;
23
24 fn try_from(trash_file: TrashFile) -> Result<Self, Self::Error> {
25 let mut desktop_file = trash_file.desktop_file;
26 let group = desktop_file.find_mut(GROUP_NAME);
27
28 let raw_date = trash_file
29 .deletion_date
30 .format(&DATE_FORMAT)
31 .map_err(crate::error::Error::DateFormat)?;
32
33 let new_deletion_date = Entry::Content(ContentEntry {
34 key: String::from("DeletionDate"),
35 values: vec![raw_date.clone()],
36 locale: None,
37 });
38
39 let new_path = Entry::Content(ContentEntry {
40 key: String::from("Path"),
41 values: vec![trash_file.path.clone()],
42 locale: None,
43 });
44
45 if let Some(group) = group {
46 let path = group.find_mut("Path");
47 if let Some(path) = path {
48 path.values = vec![trash_file.path];
49 } else {
50 group.content.push(new_path);
51 }
52
53 let date = group.find_mut("DeletionDate");
54 if let Some(date) = date {
55 date.values = vec![raw_date];
56 } else {
57 group.content.push(new_deletion_date);
58 }
59 } else {
60 let group = Group {
61 header: String::from(GROUP_NAME),
62 content: vec![new_path, new_deletion_date],
63 };
64 desktop_file.content.push(TopLevelEntry::Group(group));
65 }
66
67 Ok(desktop_file)
68 }
69}
70
71impl TryFrom<DesktopFile> for TrashFile {
72 type Error = crate::error::Error;
73
74 fn try_from(desktop: DesktopFile) -> Result<Self, Self::Error> {
75 let group = desktop.get(GROUP_NAME)?;
76 let raw_date = group.get("DeletionDate")?;
77 let raw_path = group.get("Path")?;
78
79 let date = PrimitiveDateTime::parse(&raw_date.values[0], &DATE_FORMAT)
80 .map_err(crate::error::Error::DateParsing)?;
81
82 let path = raw_path.values[0].to_owned();
83
84 Ok(Self {
85 desktop_file: desktop,
86 path,
87 deletion_date: date,
88 })
89 }
90}
91
92#[cfg(test)]
93mod test {
94 use time::macros::datetime;
95
96 use crate::parser::models::CommentEntry;
97
98 use super::*;
99
100 #[test]
101 fn parse_proper_file() {
102 let trash_file = "[Trash Info]
103Path=~/Downloads/file
104DeletionDate=2025-08-12T00:14:20";
105
106 assert_eq!(
107 TrashFile::try_from(DesktopFile::try_from(trash_file).unwrap()).unwrap(),
108 TrashFile {
109 desktop_file: DesktopFile {
110 content: vec![TopLevelEntry::Group(Group {
111 header: String::from("Trash Info"),
112 content: vec![
113 Entry::Content(ContentEntry {
114 key: String::from("Path"),
115 values: vec![String::from("~/Downloads/file")],
116 locale: None
117 }),
118 Entry::Content(ContentEntry {
119 key: String::from("DeletionDate"),
120 values: vec![String::from("2025-08-12T00:14:20")],
121 locale: None
122 })
123 ],
124 })],
125 },
126 path: String::from("~/Downloads/file"),
127 deletion_date: datetime!(2025-08-12 00:14:20)
128 }
129 )
130 }
131 #[test]
132 fn parse_double_entry_file() {
133 let trash_file = "[Trash Info]
134DeletionDate=2025-08-12T00:14:20
135Path=~/Downloads/file
136Path=/wrong/
137DeletionDate=2025-08-14T00:00:00
138";
139
140 assert_eq!(
141 TrashFile::try_from(DesktopFile::try_from(trash_file).unwrap()).unwrap(),
142 TrashFile {
143 desktop_file: DesktopFile {
144 content: vec![TopLevelEntry::Group(Group {
145 header: String::from("Trash Info"),
146 content: vec![
147 Entry::Content(ContentEntry {
148 key: String::from("DeletionDate"),
149 values: vec![String::from("2025-08-12T00:14:20")],
150 locale: None
151 }),
152 Entry::Content(ContentEntry {
153 key: String::from("Path"),
154 values: vec![String::from("~/Downloads/file")],
155 locale: None
156 }),
157 Entry::Content(ContentEntry {
158 key: String::from("Path"),
159 values: vec![String::from("/wrong/")],
160 locale: None
161 }),
162 Entry::Content(ContentEntry {
163 key: String::from("DeletionDate"),
164 values: vec![String::from("2025-08-14T00:00:00")],
165 locale: None
166 })
167 ],
168 })],
169 },
170 path: String::from("~/Downloads/file"),
171 deletion_date: datetime!(2025-08-12 00:14:20)
172 }
173 )
174 }
175
176 #[test]
177 fn edit_and_convert_file() {
178 let mut trash_file = TrashFile {
179 desktop_file: DesktopFile {
180 content: vec![TopLevelEntry::Group(Group {
181 header: String::from("Trash Info"),
182 content: vec![
183 Entry::Content(ContentEntry {
184 key: String::from("DeletionDate"),
185 values: vec![String::from("2025-08-12T00:14:20")],
186 locale: None,
187 }),
188 Entry::Content(ContentEntry {
189 key: String::from("Path"),
190 values: vec![String::from("~/Downloads/file")],
191 locale: None,
192 }),
193 Entry::Content(ContentEntry {
194 key: String::from("Path"),
195 values: vec![String::from("/wrong/")],
196 locale: None,
197 }),
198 Entry::Content(ContentEntry {
199 key: String::from("DeletionDate"),
200 values: vec![String::from("2025-08-14T00:00:00")],
201 locale: None,
202 }),
203 ],
204 })],
205 },
206 path: String::from("~/Downloads/file"),
207 deletion_date: datetime!(2025-08-12 00:14:20),
208 };
209
210 assert_eq!(
211 DesktopFile::try_from(trash_file.clone())
212 .unwrap()
213 .to_string(),
214 "[Trash Info]
215DeletionDate=2025-08-12T00:14:20
216Path=~/Downloads/file
217Path=/wrong/
218DeletionDate=2025-08-14T00:00:00"
219 );
220
221 trash_file.path = String::from("/new/path");
222
223 assert_eq!(
224 DesktopFile::try_from(trash_file.clone())
225 .unwrap()
226 .to_string(),
227 "[Trash Info]
228DeletionDate=2025-08-12T00:14:20
229Path=/new/path
230Path=/wrong/
231DeletionDate=2025-08-14T00:00:00"
232 );
233 }
234
235 #[test]
236 fn preserve_comments() {
237 let trash_file = "[Trash Info]
238
239
240DeletionDate=2025-08-12T00:14:20
241# Here is an awesome comment
242Path=~/Downloads/file
243Path=/wrong/
244
245
246DeletionDate=2025-08-14T00:00:00";
247 let trash_object = TrashFile::try_from(DesktopFile::try_from(trash_file).unwrap()).unwrap();
248
249 assert_eq!(
250 trash_object,
251 TrashFile {
252 desktop_file: DesktopFile {
253 content: vec![TopLevelEntry::Group(Group {
254 header: String::from("Trash Info"),
255 content: vec![
256 Entry::Comment(CommentEntry::Blank(String::from("\n\n"))),
257 Entry::Content(ContentEntry {
258 key: String::from("DeletionDate"),
259 values: vec![String::from("2025-08-12T00:14:20")],
260 locale: None
261 }),
262 Entry::Comment(CommentEntry::Text(String::from(
263 "Here is an awesome comment"
264 ))),
265 Entry::Content(ContentEntry {
266 key: String::from("Path"),
267 values: vec![String::from("~/Downloads/file")],
268 locale: None
269 }),
270 Entry::Content(ContentEntry {
271 key: String::from("Path"),
272 values: vec![String::from("/wrong/")],
273 locale: None
274 }),
275 Entry::Comment(CommentEntry::Blank(String::from("\n\n"))),
276 Entry::Content(ContentEntry {
277 key: String::from("DeletionDate"),
278 values: vec![String::from("2025-08-14T00:00:00")],
279 locale: None
280 })
281 ],
282 })],
283 },
284 path: String::from("~/Downloads/file"),
285 deletion_date: datetime!(2025-08-12 00:14:20)
286 }
287 );
288
289 assert_eq!(
290 DesktopFile::try_from(trash_object).unwrap().to_string(),
291 trash_file
292 );
293 }
294}