tauri_plugin_android_fs/api/models/file_access.rs
1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5/// Access mode.
6///
7/// # Serialization
8/// Serialized by `serde` as the following TypeScript type:
9///
10/// ```ts
11/// // NOTE: New variants may be added in the future
12/// type FileAccessMode = "Read" | "Write" | "WriteTruncate" | "WriteAppend" | "ReadWrite" | "ReadWriteTruncate";
13/// ```
14#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
15#[non_exhaustive]
16pub enum FileAccessMode {
17
18 /// Opens the file in read-only mode.
19 ///
20 /// FileDescriptor mode: "r"
21 Read,
22
23 /// Opens the file in write-only mode.
24 ///
25 /// Until Android 10, this will always truncate existing contents.
26 /// Since Android 10, this may or may not truncate existing contents.
27 /// If the new file is smaller than the old one, **this may cause the file to become corrupted**.
28 /// <https://issuetracker.google.com/issues/180526528>
29 ///
30 /// The reason this is marked as deprecated is because of that behavior,
31 /// and it is not scheduled to be removed in the future.
32 ///
33 /// FileDescriptor mode: "w"
34 #[deprecated(note = "This may or may not truncate existing contents. If the new file is smaller than the old one, this may cause the file to become corrupted.")]
35 Write,
36
37 /// Opens the file in write-only mode.
38 /// The existing content is truncated (deleted), and new data is written from the beginning.
39 ///
40 /// FileDescriptor mode: "wt"
41 WriteTruncate,
42
43 /// Opens the file in write-only mode.
44 /// The existing content is preserved, and new data is appended to the end of the file.
45 ///
46 /// FileDescriptor mode: "wa"
47 WriteAppend,
48
49 /// Opens the file in read-write mode.
50 ///
51 /// FileDescriptor mode: "rw"
52 ReadWrite,
53
54 /// Opens the file in read-write mode.
55 /// The existing content is truncated (deleted), and new data is written from the beginning.
56 ///
57 /// FileDescriptor mode: "rwt"
58 ReadWriteTruncate,
59}
60
61#[allow(unused)]
62#[allow(deprecated)]
63impl FileAccessMode {
64
65 pub(crate) fn to_mode(&self) -> &'static str {
66 match self {
67 FileAccessMode::Read => "r",
68 FileAccessMode::Write => "w",
69 FileAccessMode::WriteTruncate => "wt",
70 FileAccessMode::WriteAppend => "wa",
71 FileAccessMode::ReadWriteTruncate => "rwt",
72 FileAccessMode::ReadWrite => "rw",
73 }
74 }
75
76 pub(crate) fn from_mode(mode: &str) -> Result<Self> {
77 match mode {
78 "r" => Ok(Self::Read),
79 "w" => Ok(Self::Write),
80 "wt" => Ok(Self::WriteTruncate),
81 "wa" => Ok(Self::WriteAppend),
82 "rwt" => Ok(Self::ReadWriteTruncate),
83 "rw" => Ok(Self::ReadWrite),
84 mode => Err(Error::with(format!("Illegal mode: {mode}")))
85 }
86 }
87}
88
89/// Uri permission
90///
91/// # Serialization
92/// Serialized by `serde` as the following TypeScript type:
93///
94/// ```ts
95/// type UriPermission = "Read" | "Write" | "ReadAndWrite" | "ReadOrWrite";
96/// ```
97#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
98pub enum UriPermission {
99
100 /// Read access.
101 Read,
102
103 /// Write access.
104 Write,
105
106 /// Read-write access.
107 ReadAndWrite,
108
109 /// Read or write access.
110 ReadOrWrite,
111}
112
113/// Persisted uri permission state
114///
115/// # Serialization
116/// Serialized by `serde` as the following TypeScript type:
117///
118/// ```ts
119/// type PersistedUriPermissionState = {
120/// type: "Dir" | "Dir",
121/// uri: FsUri,
122/// canRead: boolean,
123/// canWrite: boolean,
124/// };
125///
126/// // See `tauri_plugin_android_fs::FsUri` for details
127/// type FsUri = unknown;
128/// ```
129#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
130#[serde(tag = "type")]
131pub enum PersistedUriPermissionState {
132 File {
133 uri: FsUri,
134
135 #[serde(rename = "canRead")]
136 can_read: bool,
137
138 #[serde(rename = "canWrite")]
139 can_write: bool,
140 },
141 Dir {
142 uri: FsUri,
143
144 #[serde(rename = "canRead")]
145 can_read: bool,
146
147 #[serde(rename = "canWrite")]
148 can_write: bool,
149 }
150}
151
152impl PersistedUriPermissionState {
153
154 pub fn uri(&self) -> &FsUri {
155 match self {
156 PersistedUriPermissionState::File { uri, .. } => uri,
157 PersistedUriPermissionState::Dir { uri, .. } => uri,
158 }
159 }
160
161 pub fn into_uri(self) -> FsUri {
162 match self {
163 PersistedUriPermissionState::File { uri, .. } => uri,
164 PersistedUriPermissionState::Dir { uri, .. } => uri,
165 }
166 }
167
168 pub fn can_read(&self) -> bool {
169 match self {
170 PersistedUriPermissionState::File { can_read, .. } => *can_read,
171 PersistedUriPermissionState::Dir { can_read, .. } => *can_read,
172 }
173 }
174
175 pub fn can_write(&self) -> bool {
176 match self {
177 PersistedUriPermissionState::File { can_write, .. } => *can_write,
178 PersistedUriPermissionState::Dir { can_write, .. } => *can_write,
179 }
180 }
181
182 pub fn is_file(&self) -> bool {
183 matches!(self, PersistedUriPermissionState::File { .. })
184 }
185
186 pub fn is_dir(&self) -> bool {
187 matches!(self, PersistedUriPermissionState::Dir { .. })
188 }
189}