@@ -25,18 +25,7 @@ lazy_static! {
2525 static ref GLOBALS : rustpython_vm:: scope:: Scope = create_globals( ) ;
2626}
2727
28- fn get_py_path ( ) -> PathBuf {
29- env:: current_dir ( ) . unwrap ( ) . join ( "src-python" )
30- }
31-
32- fn read_main_py < ' a > ( ) -> String {
33- let py_file_path = get_py_path ( ) . join ( "main.py" ) ;
34- std:: fs:: read_to_string ( py_file_path) . unwrap ( )
35- // include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
36- }
37-
38- pub fn init_python ( ) -> PyResult < ( ) > {
39- let code = read_main_py ( ) ;
28+ pub fn init_python ( code : String ) -> crate :: Result < ( ) > {
4029 rustpython_vm:: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
4130 let code_obj = vm
4231 . compile (
@@ -50,7 +39,7 @@ pub fn init_python() -> PyResult<()> {
5039 Ok ( ( ) )
5140}
5241
53- pub fn run_python ( payload : StringRequest ) -> PyResult < ( ) > {
42+ pub fn run_python ( payload : StringRequest ) -> crate :: Result < ( ) > {
5443 rustpython_vm:: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
5544 let code_obj = vm
5645 . compile (
@@ -63,36 +52,42 @@ pub fn run_python(payload: StringRequest) -> PyResult<()> {
6352 } ) ?;
6453 Ok ( ( ) )
6554}
66- pub fn register_function ( payload : RegisterRequest ) -> PyResult < ( ) > {
55+ pub fn register_function ( payload : RegisterRequest ) -> crate :: Result < ( ) > {
6756 register_function_str ( payload. python_function_call , payload. number_of_args )
6857}
6958
70- pub fn register_function_str ( fn_name : String , number_of_args : Option < u8 > ) -> PyResult < ( ) > {
59+ pub fn register_function_str ( fn_name : String , number_of_args : Option < u8 > ) -> crate :: Result < ( ) > {
7160 rustpython_vm:: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
7261 GLOBALS . globals . get_item ( & fn_name, vm) . unwrap ( ) ;
7362 FUNCTION_MAP . lock ( ) . unwrap ( ) . insert ( fn_name) ;
7463 Ok ( ( ) )
7564 } )
7665}
77- pub fn call_function ( payload : RunRequest ) -> PyResult < String > {
78- // TODO,
66+ pub fn call_function ( payload : RunRequest ) -> crate :: Result < String > {
67+ let function_name = payload. function_name ;
68+ if FUNCTION_MAP . lock ( ) . unwrap ( ) . get ( & function_name) . is_none ( ) {
69+ Err ( std:: io:: Error :: new (
70+ std:: io:: ErrorKind :: Other ,
71+ format ! ( "Function {function_name} has not been registered yet" ) ,
72+ ) ) ?;
73+ }
7974 rustpython_vm:: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
8075 let posargs: Vec < _ > = payload
8176 . args
8277 . into_iter ( )
83- . map ( |x | py_serde:: deserialize ( vm, x ) . unwrap ( ) )
78+ . map ( |value | py_serde:: deserialize ( vm, value ) . unwrap ( ) )
8479 . collect ( ) ;
8580 let res = GLOBALS
8681 . globals
87- . get_item ( & payload . function_name , vm) ?
82+ . get_item ( & function_name, vm) ?
8883 . call ( posargs, vm) ?
8984 . str ( vm) ?
9085 . to_string ( ) ;
9186 Ok ( res)
9287 } )
9388}
9489
95- pub fn read_variable ( payload : StringRequest ) -> PyResult < String > {
90+ pub fn read_variable ( payload : StringRequest ) -> crate :: Result < String > {
9691 rustpython_vm:: Interpreter :: without_stdlib ( Default :: default ( ) ) . enter ( |vm| {
9792 let res = GLOBALS
9893 . globals
0 commit comments