tauri_plugin_android_fs/models/
dir.rs

1use serde::{Deserialize, Serialize};
2
3
4/// The application specific directory.  
5#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
6#[non_exhaustive]
7pub enum PrivateDir {
8
9    /// The application specific persistent-data directory.  
10    /// 
11    /// Files stored in this directory are included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).  
12    /// 
13    /// The system prevents other apps and user from accessing these locations. 
14    /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.   
15    ///  
16    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.  
17    /// 
18    /// e.g. `/data/user/0/{app-package-name}/files`
19    /// 
20    /// <https://developer.android.com/reference/android/content/Context#getFilesDir()>
21    Data,
22
23    /// The application specific cache directory.  
24    /// 
25    /// Files stored in this directory are **not** included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).  
26    /// 
27    /// The system prevents other apps and user from accessing these locations. 
28    /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.   
29    /// 
30    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request. 
31    ///
32    /// In addition, the system will automatically delete files in this directory as disk space is needed elsewhere on the device. 
33    /// But you should not rely on this. The cache should be explicitly cleared by yourself.
34    /// 
35    /// e.g. `/data/user/0/{app-package-name}/cache`
36    /// 
37    /// <https://developer.android.com/reference/android/content/Context#getCacheDir()>
38    Cache,
39
40    /// The application specific persistent-data directory.  
41    /// 
42    /// This is similar to [`PrivateDir::Data`].
43    /// But files stored in this directory are **not** included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup).  
44    /// 
45    /// The system prevents other apps and user from accessing these locations. 
46    /// In cases where the device is rooted or the user has special permissions, the user may be able to access this.   
47    ///  
48    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.  
49    /// 
50    /// e.g. `/data/user/0/{app-package-name}/no_backup`
51    /// 
52    /// <https://developer.android.com/reference/android/content/Context#getNoBackupFilesDir()>
53    NoBackupData,
54}
55
56/// The application specific directory.  
57#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
58#[non_exhaustive]
59pub enum OutsidePrivateDir {
60
61    /// The application specific persistent-data directory.  
62    /// 
63    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.  
64    Data,
65    
66    /// The application specific cache directory.  
67    /// 
68    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request. 
69    Cache,
70}
71
72#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
73#[non_exhaustive]
74pub enum PublicDir {
75    
76    #[serde(untagged)]
77    Image(PublicImageDir),
78
79    #[serde(untagged)]
80    Video(PublicVideoDir),
81
82    #[serde(untagged)]
83    Audio(PublicAudioDir),
84
85    #[serde(untagged)]
86    GeneralPurpose(PublicGeneralPurposeDir),
87}
88
89/// Directory in which to place images that are available to other applications and users.  
90#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
91#[non_exhaustive]
92pub enum PublicImageDir {
93
94    /// e.g. `~/Pictures`
95    Pictures,
96
97    /// e.g. `~/DCIM`
98    DCIM,
99}
100
101/// Directory in which to place videos that are available to other applications and users.  
102#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
103#[non_exhaustive]
104pub enum PublicVideoDir {
105
106	/// e.g. `~/Movies`
107	Movies,
108
109	/// e.g. `~/DCIM`
110	DCIM,
111}
112
113/// Directory in which to place audios that are available to other applications and users.  
114#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
115#[non_exhaustive]
116pub enum PublicAudioDir {
117
118    /// e.g. `~/Music`
119    Music,
120
121    /// e.g. `~/Alarms`
122    Alarms,
123
124    /// This is not available on Android 9 (API level 28) and lower.  
125    /// 
126    /// e.g. `~/Audiobooks`  
127    Audiobooks,
128
129    /// e.g. `~/Notifications`
130    Notifications,
131
132    /// e.g. `~/Podcasts`
133    Podcasts,
134
135    /// e.g. `~/Ringtones`
136    Ringtones,
137
138    /// This is not available on Android 11 (API level 30) and lower.  
139    /// 
140    /// e.g. `~/Recordings`
141    Recordings,
142}
143
144/// Directory in which to place files that are available to other applications and users.  
145#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
146#[non_exhaustive]
147pub enum PublicGeneralPurposeDir {
148
149    /// e.g. `~/Documents`
150    Documents,
151
152    /// e.g. `~/Download`  
153    ///
154    /// This is not the plural "Downloads", but the singular "Download".
155    /// <https://developer.android.com/reference/android/os/Environment#DIRECTORY_DOWNLOADS>
156    Download,
157}
158
159impl std::fmt::Display for PublicImageDir {
160    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161        match self {
162            PublicImageDir::Pictures => write!(f, "Pictures"),
163            PublicImageDir::DCIM => write!(f, "DCIM"),
164        }
165    }
166}
167
168impl std::fmt::Display for PublicVideoDir {
169    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170        match self {
171            PublicVideoDir::Movies => write!(f, "Movies"),
172            PublicVideoDir::DCIM => write!(f, "DCIM"),
173        }
174    }
175}
176
177impl std::fmt::Display for PublicAudioDir {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        match self {
180            PublicAudioDir::Music => write!(f, "Music"),
181            PublicAudioDir::Alarms => write!(f, "Alarms"),
182            PublicAudioDir::Audiobooks => write!(f, "Audiobooks"),
183            PublicAudioDir::Notifications => write!(f, "Notifications"),
184            PublicAudioDir::Podcasts => write!(f, "Podcasts"),
185            PublicAudioDir::Ringtones => write!(f, "Ringtones"),
186            PublicAudioDir::Recordings => write!(f, "Recordings"),
187        }
188    }
189}
190
191impl std::fmt::Display for PublicGeneralPurposeDir {
192    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
193        match self {
194            PublicGeneralPurposeDir::Documents => write!(f, "Documents"),
195            PublicGeneralPurposeDir::Download => write!(f, "Download"),
196        }
197    }
198}
199
200impl std::fmt::Display for PublicDir {
201    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
202        match self {
203            PublicDir::Image(p) => p.fmt(f),
204            PublicDir::Video(p) => p.fmt(f),
205            PublicDir::Audio(p) => p.fmt(f),
206            PublicDir::GeneralPurpose(p) => p.fmt(f),
207        }
208    }
209}
210
211macro_rules! impl_into_pubdir {
212    ($target: ident, $wrapper: ident) => {
213        impl From<$target> for PublicDir {
214            fn from(value: $target) -> Self {
215                Self::$wrapper(value)
216            }
217        }
218    };
219}
220impl_into_pubdir!(PublicImageDir, Image);
221impl_into_pubdir!(PublicVideoDir, Video);
222impl_into_pubdir!(PublicAudioDir, Audio);
223impl_into_pubdir!(PublicGeneralPurposeDir, GeneralPurpose);