From 06840bcc65db2dd67e492ffd0a3c80f8b42e29af Mon Sep 17 00:00:00 2001 From: YUCLing Date: Sun, 30 Apr 2023 19:48:01 +0800 Subject: [PATCH] add: proc macros --- Cargo.lock | 11 +++++++++- Cargo.toml | 19 +++++----------- proc_macros/Cargo.toml | 17 ++++++++++++++ proc_macros/src/lib.rs | 44 +++++++++++++++++++++++++++++++++++++ scs_rs/Cargo.toml | 16 ++++++++++++++ build.rs => scs_rs/build.rs | 0 scs_rs/src/lib.rs | 7 ++++++ scs_rs/wrapper.hpp | 13 +++++++++++ src/lib.rs | 5 ----- wrapper.hpp | 13 ----------- 10 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 proc_macros/Cargo.toml create mode 100644 proc_macros/src/lib.rs create mode 100644 scs_rs/Cargo.toml rename build.rs => scs_rs/build.rs (100%) create mode 100644 scs_rs/src/lib.rs create mode 100644 scs_rs/wrapper.hpp delete mode 100644 src/lib.rs delete mode 100644 wrapper.hpp diff --git a/Cargo.lock b/Cargo.lock index 71b9c36..f36454a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,9 +191,18 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "scs-rs" -version = "0.1.0" +version = "0.2.0" dependencies = [ "bindgen", + "scs-rs-proc-macros", +] + +[[package]] +name = "scs-rs-proc-macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6f29ff5..22e3467 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,6 @@ -[package] -name = "scs-rs" -version = "0.1.0" -edition = "2021" -authors = ["LuoRain "] -description = "Bindings for SCS Software SDK" -repository = "https://git.rainplus.org/LuoRain/scs-rs" -license = "MIT" +[workspace] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[build-dependencies] -bindgen = "0.65.1" - -[dependencies] +members = [ + "scs_rs", + "proc_macros" +] \ No newline at end of file diff --git a/proc_macros/Cargo.toml b/proc_macros/Cargo.toml new file mode 100644 index 0000000..266d344 --- /dev/null +++ b/proc_macros/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "scs-rs-proc-macros" +version = "0.1.0" +edition = "2021" +authors = ["LuoRain "] +description = "Procedural macros for scs-rs crate" +repository = "https://git.rainplus.org/LuoRain/scs-rs" +license = "MIT" + +[lib] +proc-macro = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +quote = "1.0.26" +syn = { version = "2.0.15", features = ["full"] } diff --git a/proc_macros/src/lib.rs b/proc_macros/src/lib.rs new file mode 100644 index 0000000..401ecb8 --- /dev/null +++ b/proc_macros/src/lib.rs @@ -0,0 +1,44 @@ +use std::str::FromStr; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, ItemFn, punctuated::Punctuated, FnArg, token::Comma}; + +#[proc_macro_attribute] +pub fn scs_api(args: TokenStream, input: TokenStream) -> TokenStream { + let api_type = if args.is_empty() { + "void".to_string() + } else { + args.to_string() + }; + + if api_type != "void" && api_type != "result" { + panic!("Expected API type `void` or `result`, but got `{}`", api_type); + } + + let mut input = parse_macro_input!(input as ItemFn); + + input.sig.abi = syn::parse_str("extern \"C\"").unwrap(); + + if api_type == "result" { + input.sig.output = syn::parse_str("-> scs_rs::scs_result_t").unwrap(); + } + + TokenStream::from(quote!(#input)) +} + +#[proc_macro_attribute] +pub fn scs_telemetry_init(_args: TokenStream, input: TokenStream) -> TokenStream { + let input = scs_api(TokenStream::from_str("result").unwrap(), input); + let mut input = parse_macro_input!(input as ItemFn); + + let mut punc = Punctuated::new(); + punc.push_value(syn::parse_str::("version: scs_rs::scs_u32_t").unwrap()); + punc.push_punct(syn::parse_str::(",").unwrap()); + punc.push_value(syn::parse_str("params: *const scs_rs::scs_telemetry_init_params_t").unwrap()); + + input.sig.inputs = punc; + input.sig.ident = syn::parse_str("scs_telemetry_init").unwrap(); + + TokenStream::from(quote!(#input)) +} \ No newline at end of file diff --git a/scs_rs/Cargo.toml b/scs_rs/Cargo.toml new file mode 100644 index 0000000..afe28f4 --- /dev/null +++ b/scs_rs/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "scs-rs" +version = "0.2.0" +edition = "2021" +authors = ["LuoRain "] +description = "Bindings for SCS Software SDK" +repository = "https://git.rainplus.org/LuoRain/scs-rs" +license = "MIT" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +bindgen = "0.65.1" + +[dependencies] +scs-rs-proc-macros = { path = "../proc_macros", version = "0.1" } diff --git a/build.rs b/scs_rs/build.rs similarity index 100% rename from build.rs rename to scs_rs/build.rs diff --git a/scs_rs/src/lib.rs b/scs_rs/src/lib.rs new file mode 100644 index 0000000..3a1db20 --- /dev/null +++ b/scs_rs/src/lib.rs @@ -0,0 +1,7 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + +pub use scs_rs_proc_macros as proc_macros; \ No newline at end of file diff --git a/scs_rs/wrapper.hpp b/scs_rs/wrapper.hpp new file mode 100644 index 0000000..1734d15 --- /dev/null +++ b/scs_rs/wrapper.hpp @@ -0,0 +1,13 @@ +/* America Truck Simulator */ +#include "../sdk/include/amtrucks/scssdk_ats.h" +#include "../sdk/include/amtrucks/scssdk_input_ats.h" +#include "../sdk/include/amtrucks/scssdk_telemetry_ats.h" + +/* Euro Truck Simulator 2 */ +#include "../sdk/include/eurotrucks2/scssdk_eut2.h" +#include "../sdk/include/eurotrucks2/scssdk_input_eut2.h" +#include "../sdk/include/eurotrucks2/scssdk_telemetry_eut2.h" + +/* SDK */ +#include "../sdk/include/scssdk_input.h" +#include "../sdk/include/scssdk_telemetry.h" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 428a22c..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] - -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file diff --git a/wrapper.hpp b/wrapper.hpp deleted file mode 100644 index f6f76b4..0000000 --- a/wrapper.hpp +++ /dev/null @@ -1,13 +0,0 @@ -/* America Truck Simulator */ -#include "./sdk/include/amtrucks/scssdk_ats.h" -#include "./sdk/include/amtrucks/scssdk_input_ats.h" -#include "./sdk/include/amtrucks/scssdk_telemetry_ats.h" - -/* Euro Truck Simulator 2 */ -#include "./sdk/include/eurotrucks2/scssdk_eut2.h" -#include "./sdk/include/eurotrucks2/scssdk_input_eut2.h" -#include "./sdk/include/eurotrucks2/scssdk_telemetry_eut2.h" - -/* SDK */ -#include "./sdk/include/scssdk_input.h" -#include "./sdk/include/scssdk_telemetry.h" \ No newline at end of file