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    pub(crate) uri: String,
31    pub(crate) document_top_tree_uri: Option<String>,
32}
33
34#[allow(unused)]
35impl FileUri {
36
37    pub fn to_json_string(&self) -> Result<String> {
38        serde_json::to_string(self).map_err(Into::into)
39    }
40
41    pub fn from_json_str(json: impl AsRef<str>) -> Result<Self> {
42        serde_json::from_str(json.as_ref()).map_err(Into::into)
43    }
44
45    /// Constructs a URI from the absolute path of a file or directory.   
46    /// Even if the path is invalid, it will not cause an error or panic; an invalid URI will be returned.   
47    /// 
48    /// # Note
49    /// There are a few points to note regarding this.
50    /// - This URI cannot be passed to functions of [`FileOpener`](crate::api::api_async::FileOpener).
51    /// - Operations using this URI may fall back to [`std::fs`] instead of Kotlin API.
52    pub fn from_path(path: impl AsRef<std::path::Path>) -> Self {
53        Self { uri: format!("file://{}", path.as_ref().to_string_lossy()), document_top_tree_uri: None }
54    }
55
56    pub(crate) fn as_path(&self) -> Option<&std::path::Path> {
57        if self.uri.starts_with("file://") {
58            return Some(std::path::Path::new(self.uri.trim_start_matches("file://")))
59        }
60        None
61    }
62
63    pub(crate) fn is_content_scheme(&self) -> bool {
64        self.uri.starts_with("content://")
65    }
66}
67
68impl From<&std::path::Path> for FileUri {
69
70    fn from(path: &std::path::Path) -> Self {
71        Self::from_path(path)
72    }
73}
74
75impl From<&std::path::PathBuf> for FileUri {
76
77    fn from(path: &std::path::PathBuf) -> Self {
78        Self::from_path(path)
79    }
80}
81
82impl From<std::path::PathBuf> for FileUri {
83
84    fn from(path: std::path::PathBuf) -> Self {
85        Self::from_path(path)
86    }
87}
88
89#[cfg(feature = "tauri-plugin-fs")]
90impl From<tauri_plugin_fs::FilePath> for FileUri {
91
92    fn from(value: tauri_plugin_fs::FilePath) -> Self {
93        match value {
94            tauri_plugin_fs::FilePath::Url(url) => Self { uri: url.to_string(), document_top_tree_uri: None },
95            tauri_plugin_fs::FilePath::Path(path_buf) => path_buf.into(),
96        }
97    }
98}
99
100#[cfg(feature = "tauri-plugin-fs")]
101impl From<FileUri> for tauri_plugin_fs::FilePath {
102
103    fn from(value: FileUri) -> Self {
104        type NeverErr<T> = std::result::Result::<T, std::convert::Infallible>;
105        NeverErr::unwrap(value.uri.parse())
106    }
107}