tauri_plugin_android_fs/models/
entry.rs1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
6#[serde(rename_all = "camelCase")]
7pub enum EntryType {
8 File {
9 mime_type: String
10 },
11 Dir,
12}
13
14impl EntryType {
15
16 pub fn is_file(&self) -> bool {
17 matches!(self, Self::File { .. })
18 }
19
20 pub fn is_dir(&self) -> bool {
21 matches!(self, Self::Dir)
22 }
23
24 pub fn file_mime_type(&self) -> Option<&str> {
27 match self {
28 EntryType::File { mime_type } => Some(&mime_type),
29 EntryType::Dir => None,
30 }
31 }
32
33 pub fn into_file_mime_type(self) -> Option<String> {
36 match self {
37 EntryType::File { mime_type } => Some(mime_type),
38 EntryType::Dir => None,
39 }
40 }
41
42 pub fn file_mime_type_or_err(&self) -> Result<&str> {
45 self.file_mime_type().ok_or_else(|| Error::with("This is not a file"))
46 }
47
48 pub fn into_file_mime_type_or_err(self) -> Result<String> {
51 self.into_file_mime_type().ok_or_else(|| Error::with("This is not a file"))
52 }
53
54
55 #[deprecated = "Use FileType::file_mime_type instead"]
56 pub fn mime_type(&self) -> Option<&str> {
57 self.file_mime_type()
58 }
59
60 #[deprecated = "Use FileType::into_file_mime_type instead"]
61 pub fn into_mime_type(self) -> Option<String> {
62 self.into_file_mime_type()
63 }
64}
65
66#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
67#[serde(rename_all = "camelCase")]
68pub enum Entry {
69
70 #[non_exhaustive]
71 File {
72 uri: FileUri,
73 name: String,
74 last_modified: std::time::SystemTime,
75 len: u64,
76 mime_type: String,
77 },
78
79 #[non_exhaustive]
80 Dir {
81 uri: FileUri,
82 name: String,
83 last_modified: std::time::SystemTime,
84 }
85}
86
87impl Entry {
88
89 pub fn is_file(&self) -> bool {
90 matches!(self, Self::File { .. })
91 }
92
93 pub fn is_dir(&self) -> bool {
94 matches!(self, Self::Dir { .. })
95 }
96
97 pub fn uri(&self) -> &FileUri {
98 match self {
99 Entry::File { uri, .. } => uri,
100 Entry::Dir { uri, .. } => uri,
101 }
102 }
103
104 pub fn name(&self) -> &str {
105 match self {
106 Entry::File { name, .. } => name,
107 Entry::Dir { name, .. } => name,
108 }
109 }
110
111 pub fn last_modified(&self) -> std::time::SystemTime {
112 match self {
113 Entry::File { last_modified, .. } => *last_modified,
114 Entry::Dir { last_modified, .. } => *last_modified,
115 }
116 }
117
118 pub fn file_mime_type(&self) -> Option<&str> {
121 match self {
122 Entry::File { mime_type, .. } => Some(mime_type),
123 Entry::Dir { .. } => None,
124 }
125 }
126
127 pub fn file_len(&self) -> Option<u64> {
130 match self {
131 Entry::File { len, .. } => Some(*len),
132 Entry::Dir { .. } => None,
133 }
134 }
135
136 pub fn file_mime_type_or_err(&self) -> Result<&str> {
139 self.file_mime_type().ok_or_else(|| Error::with("This is not a file"))
140 }
141
142 pub fn file_len_or_err(&self) -> Result<u64> {
145 self.file_len().ok_or_else(|| Error::with("This is not a file"))
146 }
147 }
148
149#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
150#[serde(rename_all = "camelCase")]
151pub enum OptionalEntry {
152
153 #[non_exhaustive]
154 File {
155 uri: Option<FileUri>,
158
159 name: Option<String>,
162
163 last_modified: Option<std::time::SystemTime>,
166
167 len: Option<u64>,
170
171 mime_type: Option<String>,
174 },
175
176 #[non_exhaustive]
177 Dir {
178 uri: Option<FileUri>,
181
182 name: Option<String>,
185
186 last_modified: Option<std::time::SystemTime>,
189 }
190}
191
192impl OptionalEntry {
193
194 pub fn is_file(&self) -> bool {
195 matches!(self, Self::File { .. })
196 }
197
198 pub fn is_dir(&self) -> bool {
199 matches!(self, Self::Dir { .. })
200 }
201
202 pub fn uri_or_err(&self) -> Result<&FileUri> {
205 self.uri().ok_or_else(|| Error::with("Missing value: uri"))
206 }
207
208 pub fn name_or_err(&self) -> Result<&str> {
211 self.name().ok_or_else(|| Error::with("Missing value: name"))
212 }
213
214 pub fn last_modified_or_err(&self) -> Result<std::time::SystemTime> {
217 self.last_modified().ok_or_else(|| Error::with("Missing value: last_modified"))
218 }
219
220 pub fn file_mime_type_or_err(&self) -> Result<&str> {
223 self.file_mime_type().ok_or_else(|| Error::with("Not file or missing value: mime_type"))
224 }
225
226 pub fn file_len_or_err(&self) -> Result<u64> {
229 self.file_len().ok_or_else(|| Error::with("Not file or missing value: len"))
230 }
231
232 pub fn uri(&self) -> Option<&FileUri> {
235 match self {
236 Self::File { uri, .. } => uri.as_ref(),
237 Self::Dir { uri, .. } => uri.as_ref(),
238 }
239 }
240
241 pub fn name(&self) -> Option<&str> {
244 match self {
245 Self::File { name, .. } => name.as_deref(),
246 Self::Dir { name, .. } => name.as_deref(),
247 }
248 }
249
250 pub fn last_modified(&self) -> Option<std::time::SystemTime> {
253 match self {
254 Self::File { last_modified, .. } => *last_modified,
255 Self::Dir { last_modified, .. } => *last_modified,
256 }
257 }
258
259 pub fn file_mime_type(&self) -> Option<&str> {
262 match self {
263 Self::File { mime_type, .. } => mime_type.as_deref(),
264 Self::Dir { .. } => None,
265 }
266 }
267
268 pub fn file_len(&self) -> Option<u64> {
271 match self {
272 Self::File { len, .. } => *len,
273 Self::Dir { .. } => None,
274 }
275 }
276 }
277
278#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
279#[serde(rename_all = "camelCase")]
280pub struct EntryOptions {
281 pub uri: bool,
282 pub name: bool,
283 pub last_modified: bool,
284 pub len: bool,
285 pub mime_type: bool,
286}
287
288impl EntryOptions {
289
290 pub const ALL: EntryOptions = EntryOptions {
291 uri: true,
292 name: true,
293 last_modified: true,
294 len: true,
295 mime_type: true
296 };
297
298 pub const NONE: EntryOptions = EntryOptions {
299 uri: false,
300 name: false,
301 last_modified: false,
302 len: false,
303 mime_type: false
304 };
305
306 pub const URI_ONLY: EntryOptions = EntryOptions {
307 uri: true,
308 ..Self::NONE
309 };
310
311 pub const URI_AND_NAME: EntryOptions = EntryOptions {
312 uri: true,
313 name: true,
314 ..Self::NONE
315 };
316}
317
318impl TryFrom<OptionalEntry> for Entry {
319 type Error = crate::Error;
320
321 fn try_from(value: OptionalEntry) -> std::result::Result<Self, Self::Error> {
322 Ok(match value {
323 OptionalEntry::File { uri, name, last_modified, len, mime_type } => Entry::File {
324 uri: uri.ok_or_else(|| Error::with("Missing value: uri"))?,
325 name: name.ok_or_else(|| Error::with("Missing value: name"))?,
326 last_modified: last_modified.ok_or_else(|| Error::with("Missing value: last_modified"))?,
327 len: len.ok_or_else(|| Error::with("Missing value: len"))?,
328 mime_type: mime_type.ok_or_else(|| Error::with("Missing value: mime_type"))?,
329 },
330 OptionalEntry::Dir { uri, name, last_modified } => Entry::Dir {
331 uri: uri.ok_or_else(|| Error::with("Missing value: uri"))?,
332 name: name.ok_or_else(|| Error::with("Missing value: name"))?,
333 last_modified: last_modified.ok_or_else(|| Error::with("Missing value: last_modified"))?,
334 },
335 })
336 }
337}