tauri_plugin_android_fs/models/
file_access.rs

1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5/// Access mode
6#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
7#[non_exhaustive]
8pub enum FileAccessMode {
9
10    /// Opens the file in read-only mode.
11    /// 
12    /// FileDescriptor mode: "r"
13    Read,
14
15    /// Opens the file in write-only mode.  
16    /// 
17    /// Since Android 10, this may or may not truncate existing contents. 
18    /// If the new file is smaller than the old one, **this may cause the file to become corrupted**.
19    /// <https://issuetracker.google.com/issues/180526528>
20    /// 
21    /// The reason this is marked as deprecated is because of that behavior, 
22    /// and it is not scheduled to be removed in the future. 
23    /// 
24    /// FileDescriptor mode: "w"
25    #[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.")]
26    Write,
27
28    /// Opens the file in write-only mode.
29    /// The existing content is truncated (deleted), and new data is written from the beginning.
30    ///
31    /// FileDescriptor mode: "wt"
32    WriteTruncate,
33
34    /// Opens the file in write-only mode.
35    /// The existing content is preserved, and new data is appended to the end of the file.
36    /// 
37    /// FileDescriptor mode: "wa"
38    WriteAppend,
39
40    /// Opens the file in read-write mode.  
41    /// 
42    /// FileDescriptor mode: "rw"
43    ReadWrite,
44
45    /// Opens the file in read-write mode.
46    /// The existing content is truncated (deleted), and new data is written from the beginning.
47    ///
48    /// FileDescriptor mode: "rwt"
49    ReadWriteTruncate,
50}
51
52#[allow(unused)]
53#[allow(deprecated)]
54impl FileAccessMode {
55 
56    pub(crate) fn to_mode(&self) -> &'static str {
57        match self {
58            FileAccessMode::Read => "r",
59            FileAccessMode::Write => "w",
60            FileAccessMode::WriteTruncate => "wt",
61            FileAccessMode::WriteAppend => "wa",
62            FileAccessMode::ReadWriteTruncate => "rwt",
63            FileAccessMode::ReadWrite => "rw",
64        }
65    }
66
67    pub(crate) fn from_mode(mode: &str) -> Result<Self> {
68        match mode {
69            "r" => Ok(Self::Read),
70            "w" => Ok(Self::Write),
71            "wt" => Ok(Self::WriteTruncate),
72            "wa" => Ok(Self::WriteAppend),
73            "rwt" => Ok(Self::ReadWriteTruncate),
74            "rw" => Ok(Self::ReadWrite),
75            mode => Err(Error { msg: format!("Illegal mode: {mode}").into() })
76        }
77    }
78}
79
80/// Access mode
81#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
82pub enum PersistableAccessMode {
83
84    /// Read access.
85    Read,
86
87    /// Write access.
88    Write,
89
90    /// Read-write access.
91    ReadAndWrite,
92}
93
94#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
95pub enum PersistedUriPermission {
96    File {
97        uri: FileUri,
98        can_read: bool,
99        can_write: bool,
100    },
101    Dir {
102        uri: FileUri,
103        can_read: bool,
104        can_write: bool,
105    }
106}
107
108impl PersistedUriPermission {
109
110    pub fn uri(&self) -> &FileUri {
111        match self {
112            PersistedUriPermission::File { uri, .. } => uri,
113            PersistedUriPermission::Dir { uri, .. } => uri,
114        }
115    }
116
117    pub fn into_uri(self) -> FileUri {
118        match self {
119            PersistedUriPermission::File { uri, .. } => uri,
120            PersistedUriPermission::Dir { uri, .. } => uri,
121        }
122    }
123
124    pub fn can_read(&self) -> bool {
125        match self {
126            PersistedUriPermission::File { can_read, .. } => *can_read,
127            PersistedUriPermission::Dir { can_read, .. } => *can_read,
128        }
129    }
130
131    pub fn can_write(&self) -> bool {
132        match self {
133            PersistedUriPermission::File { can_write, .. } => *can_write,
134            PersistedUriPermission::Dir { can_write, .. } => *can_write,
135        }
136    }
137
138    pub fn is_file(&self) -> bool {
139        matches!(self, PersistedUriPermission::File { .. })
140    }
141
142    pub fn is_dir(&self) -> bool {
143        matches!(self, PersistedUriPermission::Dir { .. })
144    }
145}