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