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 { msg: format!("Illegal mode: {mode}").into() })
77        }
78    }
79}
80
81/// Access mode
82#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
83pub enum PersistableAccessMode {
84
85    /// Read access.
86    Read,
87
88    /// Write access.
89    Write,
90
91    /// Read-write access.
92    ReadAndWrite,
93}
94
95#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
96pub enum PersistedUriPermission {
97    File {
98        uri: FileUri,
99        can_read: bool,
100        can_write: bool,
101    },
102    Dir {
103        uri: FileUri,
104        can_read: bool,
105        can_write: bool,
106    }
107}
108
109impl PersistedUriPermission {
110
111    pub fn uri(&self) -> &FileUri {
112        match self {
113            PersistedUriPermission::File { uri, .. } => uri,
114            PersistedUriPermission::Dir { uri, .. } => uri,
115        }
116    }
117
118    pub fn into_uri(self) -> FileUri {
119        match self {
120            PersistedUriPermission::File { uri, .. } => uri,
121            PersistedUriPermission::Dir { uri, .. } => uri,
122        }
123    }
124
125    pub fn can_read(&self) -> bool {
126        match self {
127            PersistedUriPermission::File { can_read, .. } => *can_read,
128            PersistedUriPermission::Dir { can_read, .. } => *can_read,
129        }
130    }
131
132    pub fn can_write(&self) -> bool {
133        match self {
134            PersistedUriPermission::File { can_write, .. } => *can_write,
135            PersistedUriPermission::Dir { can_write, .. } => *can_write,
136        }
137    }
138
139    pub fn is_file(&self) -> bool {
140        matches!(self, PersistedUriPermission::File { .. })
141    }
142
143    pub fn is_dir(&self) -> bool {
144        matches!(self, PersistedUriPermission::Dir { .. })
145    }
146}