add: proc macros

This commit is contained in:
2023-04-30 19:48:01 +08:00
parent 385e7fb7e5
commit 06840bcc65
Signed by: LuoRain
GPG key ID: 16B4D3D5372966A6
10 changed files with 112 additions and 33 deletions

11
Cargo.lock generated
View file

@ -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]]

View file

@ -1,15 +1,6 @@
[package]
name = "scs-rs"
version = "0.1.0"
edition = "2021"
authors = ["LuoRain <luotianyi@luotianyi.me>"]
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"
]

17
proc_macros/Cargo.toml Normal file
View file

@ -0,0 +1,17 @@
[package]
name = "scs-rs-proc-macros"
version = "0.1.0"
edition = "2021"
authors = ["LuoRain <luotianyi@luotianyi.me>"]
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"] }

44
proc_macros/src/lib.rs Normal file
View file

@ -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::<FnArg>("version: scs_rs::scs_u32_t").unwrap());
punc.push_punct(syn::parse_str::<Comma>(",").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))
}

16
scs_rs/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "scs-rs"
version = "0.2.0"
edition = "2021"
authors = ["LuoRain <luotianyi@luotianyi.me>"]
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" }

7
scs_rs/src/lib.rs Normal file
View file

@ -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;

13
scs_rs/wrapper.hpp Normal file
View file

@ -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"

View file

@ -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"));

View file

@ -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"