diff --git a/tests/attribute_doc_comments/expected.md b/tests/attribute_doc_comments/expected.md index 9ef4304..7777b75 100644 --- a/tests/attribute_doc_comments/expected.md +++ b/tests/attribute_doc_comments/expected.md @@ -19,15 +19,29 @@ pub mod trinitrix { } } /* C API */ -pub mod trinitrix_c {} +pub mod trinitrix_c { + #[allow(unused_imports)] + use crate::callback_function; +} #[no_mangle] pub extern "C" fn trinitrix_hi( output: *mut trixy::types::String, - name: String, + name: trixy::types::String, ) -> core::ffi::c_int { let output_val: trixy::types::String = { let (tx, rx) = trixy::oneshot::channel(); - callback_function(trinitrix_hi); + callback_function( + Commands::Trinitrix(crate::trinitrix::Trinitrix::hi { + trixy_output: tx, + name: match name.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ); let recv = rx .recv() .expect("The channel should not be closed until this value is received"); diff --git a/tests/doc_comments/expected.md b/tests/doc_comments/expected.md index ec849a5..ff803d5 100644 --- a/tests/doc_comments/expected.md +++ b/tests/doc_comments/expected.md @@ -19,15 +19,29 @@ pub mod trinitrix { } } /* C API */ -pub mod trinitrix_c {} +pub mod trinitrix_c { + #[allow(unused_imports)] + use crate::callback_function; +} #[no_mangle] pub extern "C" fn trinitrix_hi( output: *mut trixy::types::String, - name: String, + name: trixy::types::String, ) -> core::ffi::c_int { let output_val: trixy::types::String = { let (tx, rx) = trixy::oneshot::channel(); - callback_function(trinitrix_hi); + callback_function( + Commands::Trinitrix(crate::trinitrix::Trinitrix::hi { + trixy_output: tx, + name: match name.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ); let recv = rx .recv() .expect("The channel should not be closed until this value is received"); diff --git a/tests/empty_doc_comment/expected.md b/tests/empty_doc_comment/expected.md index 767bc77..14e86f2 100644 --- a/tests/empty_doc_comment/expected.md +++ b/tests/empty_doc_comment/expected.md @@ -17,7 +17,10 @@ pub mod test { pub enum Test {} } /* C API */ -pub mod test_c {} +pub mod test_c { + #[allow(unused_imports)] + use crate::callback_function; +} // vim: filetype=rust ``` diff --git a/tests/functions/expected.md b/tests/functions/expected.md index 96c0e2f..30b7ac4 100644 --- a/tests/functions/expected.md +++ b/tests/functions/expected.md @@ -9,7 +9,9 @@ File path: `out/dir/api.rs` pub enum Commands { /// Call out an outstanding person #[allow(non_camel_case_types)] - call_me_back_outstanding { callback: fn(String) -> String }, + call_me_back_outstanding { + callback: extern "C" fn(trixy::types::String) -> trixy::types::String, + }, One(one::One), } pub mod one { @@ -17,21 +19,44 @@ pub mod one { pub enum One { /// Call out a person #[allow(non_camel_case_types)] - call_me_back { callback: fn(u32) }, + call_me_back { callback: extern "C" fn(trixy::types::u32) }, } } /* C API */ #[no_mangle] pub extern "C" fn call_me_back_outstanding( - callback: fn(String) -> String, + callback: extern "C" fn(trixy::types::String) -> trixy::types::String, ) -> core::ffi::c_int { - callback_function(call_me_back_outstanding); + callback_function(crate::Commands::call_me_back_outstanding { + callback: match callback.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }); return 1; } -pub mod one_c {} +pub mod one_c { + #[allow(unused_imports)] + use crate::callback_function; +} #[no_mangle] -pub extern "C" fn one_call_me_back(callback: fn(u32)) -> core::ffi::c_int { - callback_function(one_call_me_back); +pub extern "C" fn one_call_me_back( + callback: extern "C" fn(trixy::types::u32), +) -> core::ffi::c_int { + callback_function( + crate::Commands::One(crate::one::One::call_me_back { + callback: match callback.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ); return 1; } // vim: filetype=rust @@ -61,7 +86,7 @@ extern int one_call_me_back (void (*callback) (uint32_t)); struct one { - int (*call_me_back) (); + int (*call_me_back) (void (*callback) (uint32_t)); }; const struct one one = { diff --git a/tests/multi_line_doc_comment/expected.md b/tests/multi_line_doc_comment/expected.md index 42386a6..3544702 100644 --- a/tests/multi_line_doc_comment/expected.md +++ b/tests/multi_line_doc_comment/expected.md @@ -22,15 +22,29 @@ pub mod trinitrix { } } /* C API */ -pub mod trinitrix_c {} +pub mod trinitrix_c { + #[allow(unused_imports)] + use crate::callback_function; +} #[no_mangle] pub extern "C" fn trinitrix_hi( output: *mut trixy::types::String, - name: String, + name: trixy::types::String, ) -> core::ffi::c_int { let output_val: trixy::types::String = { let (tx, rx) = trixy::oneshot::channel(); - callback_function(trinitrix_hi); + callback_function( + Commands::Trinitrix(crate::trinitrix::Trinitrix::hi { + trixy_output: tx, + name: match name.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ); let recv = rx .recv() .expect("The channel should not be closed until this value is received"); diff --git a/tests/trinitrix_api/expected.md b/tests/trinitrix_api/expected.md index cc6af7d..5d50051 100644 --- a/tests/trinitrix_api/expected.md +++ b/tests/trinitrix_api/expected.md @@ -12,7 +12,7 @@ pub enum Commands { pub mod trinitrix { #[derive(Debug)] pub enum Trinitrix { - Std(std::Std), + Stdi(stdi::Stdi), Api(api::Api), } /// Language specific functions, which mirror the `trinitrix.api` namespace. @@ -20,9 +20,9 @@ pub mod trinitrix { /// one as it will most likely be more high-level and easier to use (as it isn't abstracted /// over multiple languages). Feel free to drop down to the lower level api, if you feel /// like that more, it should be as stable and user-oriented as the `std` functions - pub mod std { + pub mod stdi { #[derive(Debug)] - pub enum Std {} + pub enum Stdi {} } /// General API to change stuff in Trinitrix pub mod api { @@ -54,9 +54,9 @@ pub mod trinitrix { impl From for Mode { fn from(value: crate::trinitrix_c::api_c::ui_c::Mode_c) -> Self { match value { - Mode_c::Normal => Self::Normal, - Mode_c::Insert => Self::Insert, - Mode_c::Command => Self::Command, + crate::trinitrix_c::api_c::ui_c::Mode_c::Normal => Self::Normal, + crate::trinitrix_c::api_c::ui_c::Mode_c::Insert => Self::Insert, + crate::trinitrix_c::api_c::ui_c::Mode_c::Command => Self::Command, } } } @@ -105,7 +105,7 @@ pub mod trinitrix { pub enum Keymaps { /// Add a new keymapping #[allow(non_camel_case_types)] - add { mode: String, key: String, callback: fn() }, + add { mode: String, key: String, callback: extern "C" fn() }, /// Remove a keymapping /// /// Does nothing, if the keymapping doesn't exists yet @@ -147,104 +147,279 @@ pub mod trinitrix { } } /* C API */ -pub mod trinitrix_c {} -pub mod std_c {} -pub mod api_c {} -#[no_mangle] -pub extern "C" fn trinitrix_api_exit() -> core::ffi::c_int { - callback_function(trinitrix_api_exit); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_room_message_send(message: String) -> core::ffi::c_int { - callback_function(trinitrix_api_room_message_send); - return 1; -} -pub mod ui_c { - #[allow(non_camel_case_types)] - #[repr(C)] - #[derive(Debug)] - pub enum Mode_c { - /// Default mode (navigation mode) - Normal, - /// Allows you to insert things - Insert, - /// actives the command line - Command, +pub mod trinitrix_c { + #[allow(unused_imports)] + use crate::callback_function; + pub mod stdi_c { + #[allow(unused_imports)] + use crate::callback_function; } - impl From for Mode_c { - fn from(value: crate::trinitrix::api::ui::Mode) -> Self { - match value { - Mode::Normal => Self::Normal, - Mode::Insert => Self::Insert, - Mode::Command => Self::Command, + pub mod api_c { + #[allow(unused_imports)] + use crate::callback_function; + pub mod ui_c { + #[allow(unused_imports)] + use crate::callback_function; + #[allow(non_camel_case_types)] + #[repr(C)] + #[derive(Debug)] + pub enum Mode_c { + /// Default mode (navigation mode) + Normal, + /// Allows you to insert things + Insert, + /// actives the command line + Command, + } + impl From for Mode_c { + fn from(value: crate::trinitrix::api::ui::Mode) -> Self { + match value { + crate::trinitrix::api::ui::Mode::Normal => Self::Normal, + crate::trinitrix::api::ui::Mode::Insert => Self::Insert, + crate::trinitrix::api::ui::Mode::Command => Self::Command, + } + } } } + #[no_mangle] + pub extern "C" fn trinitrix_api_ui_set_mode( + mode: crate::trinitrix_c::api_c::ui_c::Mode_c, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Ui(crate::trinitrix::api::ui::Ui::set_mode { + mode: match mode.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_ui_cycle_planes() -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Ui( + crate::trinitrix::api::ui::Ui::cycle_planes, + ), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_ui_cycle_planes_rev() -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Ui( + crate::trinitrix::api::ui::Ui::cycle_planes_rev, + ), + ), + ), + ); + return 1; + } + pub mod keymaps_c { + #[allow(unused_imports)] + use crate::callback_function; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_keymaps_add( + mode: trixy::types::String, + key: trixy::types::String, + callback: extern "C" fn(), + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Keymaps(crate::trinitrix::api::keymaps::Keymaps::add { + mode: match mode.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + key: match key.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + callback: match callback.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_keymaps_remove( + mode: trixy::types::String, + key: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Keymaps(crate::trinitrix::api::keymaps::Keymaps::remove { + mode: match mode.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + key: match key.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_keymaps_get( + mode: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Keymaps(crate::trinitrix::api::keymaps::Keymaps::get { + mode: match mode.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + pub mod raw_c { + #[allow(unused_imports)] + use crate::callback_function; + pub mod __private_c { + #[allow(unused_imports)] + use crate::callback_function; + } + } + #[no_mangle] + pub extern "C" fn trinitrix_api_raw_raise_error( + error_message: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Raw(crate::trinitrix::api::raw::Raw::raise_error { + error_message: match error_message.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_raw_display_output( + output_message: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Raw(crate::trinitrix::api::raw::Raw::display_output { + output_message: match output_message.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_raw_send_input_unprocessed( + input: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api( + crate::trinitrix::api::Api::Raw(crate::trinitrix::api::raw::Raw::send_input_unprocessed { + input: match input.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ), + ); + return 1; + } + } + #[no_mangle] + pub extern "C" fn trinitrix_api_exit() -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api(crate::trinitrix::api::Api::exit), + ), + ); + return 1; + } + #[no_mangle] + pub extern "C" fn trinitrix_api_room_message_send( + message: trixy::types::String, + ) -> core::ffi::c_int { + callback_function( + crate::Commands::Trinitrix( + crate::trinitrix::Trinitrix::Api(crate::trinitrix::api::Api::room_message_send { + message: match message.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ), + ); + return 1; } } -#[no_mangle] -pub extern "C" fn trinitrix_api_ui_set_mode( - mode: crate::trinitrix::api::ui::Mode, -) -> core::ffi::c_int { - callback_function(trinitrix_api_ui_set_mode); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_ui_cycle_planes() -> core::ffi::c_int { - callback_function(trinitrix_api_ui_cycle_planes); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_ui_cycle_planes_rev() -> core::ffi::c_int { - callback_function(trinitrix_api_ui_cycle_planes_rev); - return 1; -} -pub mod keymaps_c {} -#[no_mangle] -pub extern "C" fn trinitrix_api_keymaps_add( - mode: String, - key: String, - callback: fn(), -) -> core::ffi::c_int { - callback_function(trinitrix_api_keymaps_add); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_keymaps_remove( - mode: String, - key: String, -) -> core::ffi::c_int { - callback_function(trinitrix_api_keymaps_remove); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_keymaps_get(mode: String) -> core::ffi::c_int { - callback_function(trinitrix_api_keymaps_get); - return 1; -} -pub mod raw_c {} -#[no_mangle] -pub extern "C" fn trinitrix_api_raw_raise_error( - error_message: String, -) -> core::ffi::c_int { - callback_function(trinitrix_api_raw_raise_error); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_raw_display_output( - output_message: String, -) -> core::ffi::c_int { - callback_function(trinitrix_api_raw_display_output); - return 1; -} -#[no_mangle] -pub extern "C" fn trinitrix_api_raw_send_input_unprocessed( - input: String, -) -> core::ffi::c_int { - callback_function(trinitrix_api_raw_send_input_unprocessed); - return 1; -} -pub mod __private_c {} // vim: filetype=rust ``` @@ -347,7 +522,7 @@ extern int trinitrix_api_raw_send_input_unprocessed (const char *input); * the lower level api, if you feel like that more, it should be as stable and * user-oriented as the `std` functions */ -struct std +struct stdi { }; @@ -356,7 +531,7 @@ struct std */ struct ui { - int (*set_mode) (); + int (*set_mode) (enum Mode); int (*cycle_planes) (void); int (*cycle_planes_rev) (void); }; @@ -392,9 +567,9 @@ struct ui */ struct keymaps { - int (*add) (); - int (*remove) (); - int (*get) (); + int (*add) (const char *, const char *, void (*callback) ()); + int (*remove) (const char *, const char *); + int (*get) (const char *); }; /** @@ -413,9 +588,9 @@ struct __private */ struct raw { - int (*raise_error) (); - int (*display_output) (); - int (*send_input_unprocessed) (); + int (*raise_error) (const char *); + int (*display_output) (const char *); + int (*send_input_unprocessed) (const char *); struct __private __private; }; @@ -425,7 +600,7 @@ struct raw struct api { int (*exit) (void); - int (*room_message_send) (); + int (*room_message_send) (const char *); struct ui ui; struct keymaps keymaps; struct raw raw; @@ -433,11 +608,11 @@ struct api struct trinitrix { - struct std std; + struct stdi stdi; struct api api; }; -const struct std std = {}; +const struct stdi stdi = {}; const struct ui ui = { .set_mode = trinitrix_api_ui_set_mode, .cycle_planes = trinitrix_api_ui_cycle_planes, @@ -463,7 +638,7 @@ const struct api api = { .raw = raw, }; const struct trinitrix trinitrix = { - .std = std, + .stdi = stdi, .api = api, }; diff --git a/tests/trinitrix_api/input.tri b/tests/trinitrix_api/input.tri index ea33579..8d7f6de 100644 --- a/tests/trinitrix_api/input.tri +++ b/tests/trinitrix_api/input.tri @@ -33,7 +33,7 @@ mod trinitrix { /// one as it will most likely be more high-level and easier to use (as it isn't abstracted /// over multiple languages). Feel free to drop down to the lower level api, if you feel /// like that more, it should be as stable and user-oriented as the `std` functions - mod std {} + mod stdi {} /// General API to change stuff in Trinitrix mod api { diff --git a/tests/types/expected.md b/tests/types/expected.md index cdb3f0a..3e56d91 100644 --- a/tests/types/expected.md +++ b/tests/types/expected.md @@ -15,7 +15,7 @@ pub mod test { #[derive(Debug)] pub struct Callback { /// Very important field - pub func: fn(String) -> String, + pub func: extern "C" fn(trixy::types::String) -> trixy::types::String, /// Very important field for keeping time constant pub timeout: u32, } @@ -58,9 +58,9 @@ pub mod test { impl From for CallbackPriority { fn from(value: crate::test_c::CallbackPriority_c) -> Self { match value { - CallbackPriority_c::High => Self::High, - CallbackPriority_c::Medium => Self::Medium, - CallbackPriority_c::Low => Self::Low, + crate::test_c::CallbackPriority_c::High => Self::High, + crate::test_c::CallbackPriority_c::Medium => Self::Medium, + crate::test_c::CallbackPriority_c::Low => Self::Low, } } } @@ -75,6 +75,8 @@ pub mod test { } /* C API */ pub mod test_c { + #[allow(unused_imports)] + use crate::callback_function; /// Same thing as above (and a enum doc comment test) #[allow(non_camel_case_types)] #[repr(C)] @@ -92,9 +94,9 @@ pub mod test_c { impl From for CallbackPriority_c { fn from(value: crate::test::CallbackPriority) -> Self { match value { - CallbackPriority::High => Self::High, - CallbackPriority::Medium => Self::Medium, - CallbackPriority::Low => Self::Low, + crate::test::CallbackPriority::High => Self::High, + crate::test::CallbackPriority::Medium => Self::Medium, + crate::test::CallbackPriority::Low => Self::Low, } } } @@ -104,17 +106,43 @@ pub mod test_c { #[derive(Debug)] pub struct Callback_c { /// Very important field - pub func: fn(String) -> String, + pub func: extern "C" fn(trixy::types::String) -> trixy::types::String, /// Very important field for keeping time constant pub timeout: trixy::types::u32, } + impl TryFrom for Callback_c { + type Error = trixy::types::error::TypeConversionError; + fn try_from(value: crate::test::Callback) -> Result { + Ok(Self { + func: value.func.try_into()?, + timeout: value.timeout.try_into()?, + }) + } + } } #[no_mangle] pub extern "C" fn test_execute_callback( - callback: crate::test::Callback, - priority: crate::test::CallbackPriority, + callback: crate::test_c::Callback_c, + priority: crate::test_c::CallbackPriority_c, ) -> core::ffi::c_int { - callback_function(test_execute_callback); + callback_function( + crate::Commands::Test(crate::test::Test::execute_callback { + callback: match callback.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + priority: match priority.try_into() { + Ok(ok) => ok, + Err(err) => { + trixy::types::traits::errno::set(err.into()); + return 0; + } + }, + }), + ); return 1; } // vim: filetype=rust @@ -173,7 +201,7 @@ extern int test_execute_callback (struct Callback callback, struct test { - int (*execute_callback) (); + int (*execute_callback) (struct Callback, enum CallbackPriority); }; const struct test test = { diff --git a/tests/utf8_in_doc_comment/expected.md b/tests/utf8_in_doc_comment/expected.md index 2c02e9f..1c80c5d 100644 --- a/tests/utf8_in_doc_comment/expected.md +++ b/tests/utf8_in_doc_comment/expected.md @@ -17,7 +17,10 @@ pub mod trinitrix { pub enum Trinitrix {} } /* C API */ -pub mod trinitrix_c {} +pub mod trinitrix_c { + #[allow(unused_imports)] + use crate::callback_function; +} // vim: filetype=rust ```