Skip to content

Commit 129a40f

Browse files
committed
refactoring
1 parent 2875b8e commit 129a40f

5 files changed

Lines changed: 29 additions & 59 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tauri-plugin-python"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = [ "Marco Mengelkoch" ]
55
description = ""
66
edition = "2021"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tauri-plugin-python-api",
3-
"version": "0.1.0",
3+
"version": "0.2.1",
44
"author": "Marco Mengelkoch",
55
"description": "",
66
"type": "module",

src/desktop.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,14 @@
66
use serde::de::DeserializeOwned;
77
use tauri::{plugin::PluginApi, AppHandle, Runtime};
88

9-
use crate::models::*;
109
use crate::py_lib;
1110

1211
/// Access to the python plugin APIs.
13-
pub struct Python<R: Runtime>(AppHandle<R>);
1412
1513
pub fn init<R: Runtime, C: DeserializeOwned>(
1614
app: &AppHandle<R>,
1715
_api: PluginApi<R, C>,
18-
) -> crate::Result<Python<R>> {
16+
) -> crate::Result<crate::Python<R>> {
1917
py_lib::init_python()?;
20-
Ok(Python(app.clone()))
21-
}
22-
23-
impl<R: Runtime> Python<R> {
24-
pub fn run_python(&self, payload: StringRequest) -> crate::Result<StringResponse> {
25-
py_lib::run_python(payload)?;
26-
Ok(StringResponse { value: "Ok".into() })
27-
}
28-
pub fn register_function(&self, payload: RegisterRequest) -> crate::Result<StringResponse> {
29-
py_lib::register_function(payload)?;
30-
Ok(StringResponse { value: "Ok".into() })
31-
}
32-
pub fn call_function(&self, payload: RunRequest) -> crate::Result<StringResponse> {
33-
let py_res = py_lib::call_function(payload)?;
34-
Ok(StringResponse {
35-
value: py_res.to_string(),
36-
})
37-
}
38-
pub fn read_variable(&self, payload: StringRequest) -> crate::Result<StringResponse> {
39-
let py_res = py_lib::read_variable(payload)?;
40-
Ok(StringResponse { value: py_res })
41-
}
18+
Ok(crate::Python(app.clone()))
4219
}

src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

66
use tauri::{
7-
plugin::{Builder, TauriPlugin},
8-
Manager, Runtime,
7+
plugin::{Builder, TauriPlugin}, AppHandle, Manager, Runtime
98
};
109

1110
pub use models::*;
@@ -22,10 +21,7 @@ mod py_lib;
2221

2322
pub use error::{Error, Result};
2423

25-
#[cfg(desktop)]
26-
use desktop::Python;
27-
#[cfg(mobile)]
28-
use mobile::Python;
24+
pub struct Python<R: Runtime>(AppHandle<R>);
2925

3026
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the python APIs.
3127
pub trait PythonExt<R: Runtime> {
@@ -60,3 +56,24 @@ pub fn init<R: Runtime>(python_functions: Vec<&'static str>) -> TauriPlugin<R> {
6056
})
6157
.build()
6258
}
59+
60+
impl<R: Runtime> Python<R> {
61+
pub fn run_python(&self, payload: StringRequest) -> crate::Result<StringResponse> {
62+
py_lib::run_python(payload)?;
63+
Ok(StringResponse { value: "Ok".into() })
64+
}
65+
pub fn register_function(&self, payload: RegisterRequest) -> crate::Result<StringResponse> {
66+
py_lib::register_function(payload)?;
67+
Ok(StringResponse { value: "Ok".into() })
68+
}
69+
pub fn call_function(&self, payload: RunRequest) -> crate::Result<StringResponse> {
70+
let py_res = py_lib::call_function(payload)?;
71+
Ok(StringResponse {
72+
value: py_res.to_string(),
73+
})
74+
}
75+
pub fn read_variable(&self, payload: StringRequest) -> crate::Result<StringResponse> {
76+
let py_res = py_lib::read_variable(payload)?;
77+
Ok(StringResponse { value: py_res })
78+
}
79+
}

src/mobile.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,15 @@ use crate::py_lib;
1515
#[cfg(target_os = "ios")]
1616
tauri::ios_plugin_binding!(init_plugin_python);
1717

18-
/// Access to the python plugin APIs.
19-
pub struct Python<R: Runtime>(PluginHandle<R>);
20-
2118
// initializes the Kotlin or Swift plugin classes
2219
pub fn init<R: Runtime, C: DeserializeOwned>(
2320
_app: &AppHandle<R>,
2421
api: PluginApi<R, C>,
25-
) -> crate::Result<Python<R>> {
22+
) -> crate::Result<crate::Python<R>> {
2623
#[cfg(target_os = "android")]
2724
let handle = api.register_android_plugin("com.plugin.python.application", "ExamplePlugin")?;
2825
#[cfg(target_os = "ios")]
2926
let handle = api.register_ios_plugin(init_plugin_python)?;
3027
py_lib::init_python()?;
31-
Ok(Python(handle))
32-
}
33-
34-
impl<R: Runtime> Python<R> {
35-
pub fn run_python(&self, payload: StringRequest) -> crate::Result<StringResponse> {
36-
py_lib::run_python(payload)?;
37-
Ok(StringResponse { value: "Ok".into() })
38-
}
39-
pub fn register_function(&self, payload: RegisterRequest) -> crate::Result<StringResponse> {
40-
py_lib::register_function(payload)?;
41-
Ok(StringResponse { value: "Ok".into() })
42-
}
43-
pub fn call_function(&self, payload: RunRequest) -> crate::Result<StringResponse> {
44-
let py_res = py_lib::call_function(payload)?;
45-
Ok(StringResponse {
46-
value: py_res.to_string(),
47-
})
48-
}
49-
pub fn read_variable(&self, payload: StringRequest) -> crate::Result<StringResponse> {
50-
let py_res = py_lib::read_variable(payload)?;
51-
Ok(StringResponse { value: py_res })
52-
}
28+
Ok(crate::Python(handle))
5329
}

0 commit comments

Comments
 (0)