tauri_plugin_android_fs/models/
file_uri.rs

1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5/// Path to represent a file or directory.
6/// 
7/// # Note
8/// For compatibility, an interconversion to [`tauri_plugin_fs::FilePath`] is implemented, such as follwing.  
9/// This is lossy and also not guaranteed to work properly with other plugins.  
10/// However, reading and writing files by official [`tauri_plugin_fs`] etc. should work well.  
11/// ```ignore
12/// use tauri_plugin_android_fs::FileUri;
13/// use tauri_plugin_fs::FilePath;
14/// 
15/// let uri: FileUri = unimplemented!();
16/// let path: FilePath = uri.into();
17/// let uri: FileUri = path.into();
18/// ```
19/// 
20/// # Typescript type
21/// ```typescript
22/// type FileUri = {
23///     uri: string, // This can use as path for official tauri_plugin_fs
24///     documentTopTreeUri: string | null
25/// }
26/// ```
27#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
28#[serde(rename_all = "camelCase")]
29pub struct FileUri {
30    /// `file://` or `content://` URI of file or directory.
31    pub uri: String,
32
33    /// Only files/directories under the directory obtained by `FilePicker::pick_dir` will own this.
34    pub document_top_tree_uri: Option<String>,
35}
36
37#[allow(unused)]
38impl FileUri {
39
40    pub fn to_json_string(&self) -> Result<String> {
41        serde_json::to_string(self).map_err(Into::into)
42    }
43
44    pub fn from_json_str(json: impl AsRef<str>) -> Result<Self> {
45        serde_json::from_str(json.as_ref()).map_err(Into::into)
46    }
47
48    pub fn to_bytes(&self) -> Result<Vec<u8>> {
49        serde_json::to_vec(self).map_err(Into::into)
50    }
51
52    pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self> {
53        serde_json::from_slice(bytes.as_ref()).map_err(Into::into)
54    }
55
56    pub fn from_path(path: impl AsRef<std::path::Path>) -> Self {
57        Self { uri: format!("file://{}", path.as_ref().to_string_lossy()), document_top_tree_uri: None }
58    }
59
60    pub(crate) fn as_path(&self) -> Option<&std::path::Path> {
61        if self.uri.starts_with("file://") {
62            return Some(std::path::Path::new(self.uri.trim_start_matches("file://")))
63        }
64        None
65    }
66
67    pub(crate) fn is_content_scheme(&self) -> bool {
68        self.uri.starts_with("content://")
69    }
70}
71
72impl From<&std::path::Path> for FileUri {
73
74    fn from(path: &std::path::Path) -> Self {
75        Self::from_path(path)
76    }
77}
78
79impl From<&std::path::PathBuf> for FileUri {
80
81    fn from(path: &std::path::PathBuf) -> Self {
82        Self::from_path(path)
83    }
84}
85
86impl From<std::path::PathBuf> for FileUri {
87
88    fn from(path: std::path::PathBuf) -> Self {
89        Self::from_path(path)
90    }
91}
92
93#[cfg(feature = "tauri-plugin-fs")]
94impl From<tauri_plugin_fs::FilePath> for FileUri {
95
96    fn from(value: tauri_plugin_fs::FilePath) -> Self {
97        match value {
98            tauri_plugin_fs::FilePath::Url(url) => Self { uri: url.to_string(), document_top_tree_uri: None },
99            tauri_plugin_fs::FilePath::Path(path_buf) => path_buf.into(),
100        }
101    }
102}
103
104#[cfg(feature = "tauri-plugin-fs")]
105impl From<FileUri> for tauri_plugin_fs::FilePath {
106
107    fn from(value: FileUri) -> Self {
108        type NeverErr<T> = std::result::Result::<T, std::convert::Infallible>;
109        NeverErr::unwrap(value.uri.parse())
110    }
111}