pytauri_core/plugins/opener/
mod.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display, Formatter},
4};
5
6use pyo3::{exceptions::PyRuntimeError, prelude::*};
7use tauri_plugin_opener::{self as plugin};
8
9use crate::{ext_mod::plugin::Plugin, tauri_runtime::Runtime, utils::TauriError};
10
11#[derive(Debug)]
12struct 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::UnknownProgramName(_)
30                | plugin::Error::ForbiddenPath { .. }
31                | plugin::Error::ForbiddenUrl { .. }
32                | plugin::Error::UnsupportedPlatform
33                | plugin::Error::NoParent(_)
34                | plugin::Error::FailedToConvertPathToFileUrl
35            ) => PyRuntimeError::new_err(e.to_string()),
36            #[cfg(any(
37                target_os = "linux",
38                target_os = "dragonfly",
39                target_os = "freebsd",
40                target_os = "netbsd",
41                target_os = "openbsd"
42            ))]
43            e @ plugin::Error::Zbus(_)  => PyRuntimeError::new_err(e.to_string()),
44            non_exhaustive => PyRuntimeError::new_err(format!(
45                "Unimplemented plugin error, please report this to the pytauri developers: {non_exhaustive}"
46            )),
47        }
48    }
49}
50
51impl From<plugin::Error> for PluginError {
52    fn from(value: plugin::Error) -> Self {
53        Self(value)
54    }
55}
56
57/// See also: [tauri_plugin_opener::init]
58#[pyfunction]
59pub fn init() -> Plugin {
60    Plugin::new(Box::new(|| Box::new(plugin::init::<Runtime>())))
61}
62
63/// See also: [tauri_plugin_opener]
64#[pymodule(submodule, gil_used = false)]
65pub mod opener {
66    #[pymodule_export]
67    pub use super::init;
68}