pytauri_core/plugins/fs/
mod.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display, Formatter},
4};
5
6use pyo3::{exceptions::PyRuntimeError, prelude::*};
7use tauri_plugin_fs::{self as plugin};
8
9use crate::{ext_mod::plugin::Plugin, tauri_runtime::Runtime, utils::TauriError};
10
11#[derive(Debug)]
12pub(crate) struct PluginError(plugin::Error);
13
14impl Display for PluginError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        Display::fmt(&self.0, f)
17    }
18}
19
20impl Error for PluginError {}
21
22impl From<PluginError> for PyErr {
23    fn from(value: PluginError) -> Self {
24        match value.0 {
25            plugin::Error::Tauri(e) => TauriError::from(e).into(),
26            plugin::Error::Io(e) => e.into(),
27            e @ (
28                plugin::Error::Json(_)
29                | plugin::Error::PathForbidden(_)
30                | plugin::Error::GlobPattern(_)
31                | plugin::Error::InvalidPathUrl
32                | plugin::Error::UnsafePathBuf(_)
33            ) => PyRuntimeError::new_err(e.to_string()),
34            non_exhaustive => PyRuntimeError::new_err(format!(
35                "Unimplemented plugin error, please report this to the pytauri developers: {non_exhaustive}"
36            )),
37        }
38    }
39}
40
41impl From<plugin::Error> for PluginError {
42    fn from(value: plugin::Error) -> Self {
43        Self(value)
44    }
45}
46
47/// See also: [tauri_plugin_fs::init]
48#[pyfunction]
49pub fn init() -> Plugin {
50    Plugin::new(Box::new(|| Box::new(plugin::init::<Runtime>())))
51}
52
53/// See also: [tauri_plugin_fs]
54#[pymodule(submodule, gil_used = false)]
55pub mod fs {
56    #[pymodule_export]
57    pub use super::init;
58
59    pub(crate) use super::PluginError;
60}