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    pub fn from_path(path: impl AsRef<std::path::Path>) -> Self {
46        Self { uri: format!("file://{}", path.as_ref().to_string_lossy()), document_top_tree_uri: None }
47    }
48
49    pub(crate) fn as_path(&self) -> Option<&std::path::Path> {
50        if self.uri.starts_with("file://") {
51            return Some(std::path::Path::new(self.uri.trim_start_matches("file://")))
52        }
53        None
54    }
55
56    pub(crate) fn is_content_scheme(&self) -> bool {
57        self.uri.starts_with("content://")
58    }
59}
60
61impl From<&std::path::Path> for FileUri {
62
63    fn from(path: &std::path::Path) -> Self {
64        Self::from_path(path)
65    }
66}
67
68impl From<&std::path::PathBuf> for FileUri {
69
70    fn from(path: &std::path::PathBuf) -> 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
82#[cfg(feature = "tauri-plugin-fs")]
83impl From<tauri_plugin_fs::FilePath> for FileUri {
84
85    fn from(value: tauri_plugin_fs::FilePath) -> Self {
86        match value {
87            tauri_plugin_fs::FilePath::Url(url) => Self { uri: url.to_string(), document_top_tree_uri: None },
88            tauri_plugin_fs::FilePath::Path(path_buf) => path_buf.into(),
89        }
90    }
91}
92
93#[cfg(feature = "tauri-plugin-fs")]
94impl From<FileUri> for tauri_plugin_fs::FilePath {
95
96    fn from(value: FileUri) -> Self {
97        type NeverErr<T> = std::result::Result::<T, std::convert::Infallible>;
98        NeverErr::unwrap(value.uri.parse())
99    }
100}