pytauri_core/plugins/window_state/
mod.rs1use std::{
2 error::Error,
3 fmt::{Debug, Display, Formatter},
4};
5
6use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict};
7use pyo3_utils::from_py_dict::{derive_from_py_dict, FromPyDict as _};
8use tauri_plugin_window_state::{self as plugin};
9
10use crate::{ext_mod::plugin::Plugin, utils::TauriError};
11
12#[derive(Debug)]
13struct PluginError(plugin::Error);
14
15impl Display for PluginError {
16 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17 Display::fmt(&self.0, f)
18 }
19}
20
21impl Error for PluginError {}
22
23impl From<PluginError> for PyErr {
24 fn from(value: PluginError) -> Self {
25 match value.0 {
26 plugin::Error::Tauri(e) => TauriError::from(e).into(),
27 plugin::Error::Io(e) => e.into(),
28 e @ plugin::Error::SerdeJson(_) => PyRuntimeError::new_err(e.to_string()),
29 }
30 }
31}
32
33impl From<plugin::Error> for PluginError {
34 fn from(value: plugin::Error) -> Self {
35 Self(value)
36 }
37}
38
39#[non_exhaustive]
41pub struct BuilderArgs {}
42
43derive_from_py_dict!(BuilderArgs {});
44
45impl BuilderArgs {
46 fn from_kwargs(kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Option<Self>> {
47 kwargs.map(Self::from_py_dict).transpose()
48 }
49
50 fn apply_to_builder(self, builder: plugin::Builder) -> plugin::Builder {
51 let Self {} = self;
52 builder
53 }
54}
55
56#[pyclass(frozen)]
58#[non_exhaustive]
59pub struct Builder;
60
61#[pymethods]
62impl Builder {
63 #[staticmethod]
64 #[pyo3(signature = (**kwargs))]
65 fn build(kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Plugin> {
66 let args = BuilderArgs::from_kwargs(kwargs)?;
67
68 let mut builder = plugin::Builder::new();
69 if let Some(args) = args {
70 builder = args.apply_to_builder(builder);
71 }
72
73 let plugin = Plugin::new(Box::new(move || Box::new(builder.build())));
74 Ok(plugin)
75 }
76}
77
78#[pymodule(submodule, gil_used = false)]
80pub mod window_state {
81 #[pymodule_export]
82 pub use super::Builder;
83
84 pub use super::BuilderArgs;
85}