tauri_plugin_android_fs/models/
file_uri.rs1use serde::{Deserialize, Serialize};
2use crate::*;
3
4
5#[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 {
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}