1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use chrono::prelude::{DateTime, Utc};
use std::cmp::Ordering;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct EntryBase {
#[serde(with = "crate::date")]
pub created: DateTime<Utc>,
#[serde(with = "crate::date")]
pub modified: DateTime<Utc>,
#[serde(rename = "parentfolderid")]
pub parent_folder_id: Option<u64>,
pub icon: String,
pub id: String,
pub name: String,
pub path: Option<String>,
pub thumb: bool,
#[serde(rename = "isshared")]
pub is_shared: bool,
#[serde(rename = "ismine")]
pub is_mine: bool,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct File {
#[serde(flatten)]
pub base: EntryBase,
#[serde(rename = "fileid")]
pub file_id: u64,
pub size: Option<usize>,
pub hash: Option<usize>,
#[serde(rename = "contenttype")]
pub content_type: Option<String>,
}
impl Eq for File {}
impl PartialEq for File {
fn eq(&self, other: &Self) -> bool {
self.base.id.eq(&other.base.id)
}
}
impl Ord for File {
fn cmp(&self, other: &Self) -> Ordering {
self.base.name.cmp(&other.base.name)
}
}
impl PartialOrd for File {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Folder {
#[serde(flatten)]
pub base: EntryBase,
#[serde(rename = "folderid")]
pub folder_id: u64,
pub contents: Option<Vec<Entry>>,
}
impl Eq for Folder {}
impl PartialEq for Folder {
fn eq(&self, other: &Self) -> bool {
self.base.id.eq(&other.base.id)
}
}
impl Ord for Folder {
fn cmp(&self, other: &Self) -> Ordering {
self.base.name.cmp(&other.base.name)
}
}
impl PartialOrd for Folder {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Folder {
pub fn find_entry(&self, name: &str) -> Option<&Entry> {
self.contents
.as_ref()
.and_then(|list| list.iter().find(|item| item.base().name == name))
}
pub fn find_file(&self, name: &str) -> Option<&File> {
self.contents.as_ref().and_then(|list| {
list.iter()
.filter_map(|item| item.as_file())
.find(|item| item.base.name == name)
})
}
pub fn find_folder(&self, name: &str) -> Option<&Folder> {
self.contents.as_ref().and_then(|list| {
list.iter()
.filter_map(|item| item.as_folder())
.find(|item| item.base.name == name)
})
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
pub enum Entry {
File(File),
Folder(Folder),
}
impl PartialOrd for Entry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Entry {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::File(self_file), Self::File(other_file)) => self_file.cmp(other_file),
(Self::File(_), Self::Folder(_)) => Ordering::Greater,
(Self::Folder(self_folder), Self::Folder(other_folder)) => {
self_folder.cmp(other_folder)
}
(Self::Folder(_), Self::File(_)) => Ordering::Less,
}
}
}
impl From<File> for Entry {
fn from(value: File) -> Self {
Self::File(value)
}
}
impl From<Folder> for Entry {
fn from(value: Folder) -> Self {
Self::Folder(value)
}
}
impl Entry {
pub fn base(&self) -> &EntryBase {
match self {
Self::File(file) => &file.base,
Self::Folder(folder) => &folder.base,
}
}
pub fn file_id(&self) -> Option<u64> {
match self {
Self::File(item) => Some(item.file_id),
_ => None,
}
}
pub fn is_file(&self) -> bool {
matches!(self, Self::File(_))
}
pub fn into_file(self) -> Option<File> {
match self {
Self::File(value) => Some(value),
_ => None,
}
}
pub fn as_file(&self) -> Option<&File> {
match self {
Self::File(value) => Some(value),
_ => None,
}
}
pub fn folder_id(&self) -> Option<u64> {
match self {
Self::Folder(item) => Some(item.folder_id),
_ => None,
}
}
pub fn is_folder(&self) -> bool {
matches!(self, Self::Folder(_))
}
pub fn into_folder(self) -> Option<Folder> {
match self {
Self::Folder(value) => Some(value),
_ => None,
}
}
pub fn as_folder(&self) -> Option<&Folder> {
match self {
Self::Folder(value) => Some(value),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_file(id: u64, name: &str) -> File {
File {
base: EntryBase {
created: Utc::now(),
modified: Utc::now(),
parent_folder_id: None,
icon: "".into(),
id: format!("f{}", id),
name: name.into(),
path: None,
thumb: false,
is_shared: false,
is_mine: false,
},
file_id: id,
size: Some(42),
hash: Some(42),
content_type: None,
}
}
fn create_folder(id: u64, name: &str) -> Folder {
Folder {
base: EntryBase {
created: Utc::now(),
modified: Utc::now(),
parent_folder_id: None,
icon: "".into(),
id: format!("d{}", id),
name: name.into(),
path: None,
thumb: false,
is_shared: false,
is_mine: false,
},
folder_id: id,
contents: None,
}
}
#[test]
fn sorting() {
let mut data: Vec<Entry> = vec![
create_file(1, "cccc").into(),
create_folder(2, "dddd").into(),
create_file(3, "aaaa").into(),
create_folder(4, "eeee").into(),
];
data.sort();
let ids: Vec<_> = data.iter().map(|item| item.base().id.clone()).collect();
assert_eq!(ids, vec!["d2", "d4", "f3", "f1"]);
}
}