Skip to content

Commit 8fdcb8b

Browse files
committed
wip
1 parent c697b0d commit 8fdcb8b

File tree

16 files changed

+347
-145
lines changed

16 files changed

+347
-145
lines changed

Cargo.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/api/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
2-
name = "probe"
2+
name = "rosttasse"
33
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
77
jni = "0.21.1"
8-
probe_macros = { path = "../macros/" }
8+
rosttasse_macros = { path = "../macros/" }

rust/api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod primitives;
44

55
use class::Instance;
66
use conversion::{FromJValue, IntoJValue};
7-
pub use probe_macros::*;
7+
pub use rosttasse_macros::*;
88

99
pub trait JSignature {
1010
const CLASS: &'static str;

rust/macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "probe_macros"
2+
name = "rosttasse_macros"
33
version = "0.1.0"
44
edition = "2021"
55

rust/macros/src/class/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
mod parse;
2+
3+
pub use parse::{ClassBody, ClassInput};
4+
use proc_macro2::TokenStream;
5+
use quote::{quote, quote_spanned};
6+
7+
pub fn main(input: ClassInput, body: ClassBody) -> syn::Result<TokenStream> {
8+
let struct_name = &body.ident;
9+
10+
let signature = input.package.value();
11+
let signature = quote_spanned! {input.package.span() =>
12+
impl ::probe::JSignature for #struct_name {
13+
const CLASS: &str = #signature;
14+
}
15+
};
16+
17+
Ok(quote! {
18+
#signature
19+
})
20+
}

rust/macros/src/class/parse.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use syn::{
2+
parse::{Parse, ParseStream},
3+
punctuated::Punctuated,
4+
token, FieldsNamed, Ident, LitStr, Token, Type, Visibility,
5+
};
6+
7+
mod kw {
8+
use syn::custom_keyword;
9+
10+
custom_keyword!(extends);
11+
pub type Extends = extends;
12+
13+
custom_keyword!(implements);
14+
pub type Implements = implements;
15+
}
16+
17+
pub use kw::{Extends, Implements};
18+
19+
pub struct ClassInput {
20+
pub package: LitStr,
21+
pub extends: Option<(Extends, Type)>,
22+
pub implements_token: Option<Implements>,
23+
pub implements: Punctuated<Type, Token![,]>,
24+
}
25+
26+
impl Parse for ClassInput {
27+
fn parse(input: ParseStream) -> syn::Result<Self> {
28+
let package = input.parse()?;
29+
let extends = if let Ok(extends) = input.parse() {
30+
let ty = input.parse()?;
31+
Some((extends, ty))
32+
} else {
33+
None
34+
};
35+
36+
let implements_token = input.parse().ok();
37+
let implements = if implements_token.is_some() {
38+
input.parse_terminated(Type::parse, Token![,])?
39+
} else {
40+
Punctuated::new()
41+
};
42+
43+
Ok(Self {
44+
package,
45+
extends,
46+
implements_token,
47+
implements,
48+
})
49+
}
50+
}
51+
52+
pub struct ClassBody {
53+
pub vis: Visibility,
54+
pub struct_token: Token![struct],
55+
pub ident: Ident,
56+
pub fields: Option<FieldsNamed>,
57+
}
58+
59+
impl Parse for ClassBody {
60+
fn parse(input: ParseStream) -> syn::Result<Self> {
61+
let vis = input.parse()?;
62+
let struct_token = input.parse()?;
63+
let ident = input.parse()?;
64+
let fields = if input.peek(token::Brace) {
65+
Some(input.parse()?)
66+
} else {
67+
input.parse::<Token![;]>()?;
68+
None
69+
};
70+
71+
Ok(Self {
72+
vis,
73+
struct_token,
74+
ident,
75+
fields,
76+
})
77+
}
78+
}

rust/macros/src/java_class.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use syn::{Attribute, Error, Expr, ExprLit, Lit, LitStr, Meta, MetaList, MetaName
99
pub use enum_::main_enum;
1010
pub use struct_::main_struct;
1111

12-
pub fn get_string_attr(span: Span, attr_name: &impl AsRef<str>, attrs: &Vec<Attribute>) -> Result<Option<String>, Error> {
12+
pub fn get_string_attr(
13+
span: Span,
14+
attr_name: &impl AsRef<str>,
15+
attrs: &Vec<Attribute>,
16+
) -> Result<Option<String>, Error> {
1317
let attr = get_attribute(attr_name, &attrs).map(|attr| match &attr.meta {
1418
Meta::List(MetaList { tokens, .. }) => {
1519
Ok(syn::parse2::<LitStr>(tokens.to_token_stream())?.value())
@@ -20,7 +24,10 @@ pub fn get_string_attr(span: Span, attr_name: &impl AsRef<str>, attrs: &Vec<Attr
2024
}),
2125
..
2226
}) => Ok(p.value()),
23-
_ => Err(Error::new(span, format!("Usage: #[{} = \"OtherName\"]", attr_name.as_ref()))),
27+
_ => Err(Error::new(
28+
span,
29+
format!("Usage: #[{} = \"OtherName\"]", attr_name.as_ref()),
30+
)),
2431
});
2532

2633
if let Some(attr) = attr {

rust/macros/src/java_class/enum_.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ pub fn main_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream, Er
6060
let variant_name = get_string_attr(variant.span(), &"variant", &variant.attrs)?;
6161

6262
let name = &variant.ident;
63-
let name = rename
64-
.map(|n| n)
65-
.unwrap_or(name.to_string());
63+
let name = rename.map(|n| n).unwrap_or(name.to_string());
6664

6765
let variant_sig = if !is_itself {
6866
quote!(#struct_class::sig())

rust/macros/src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
extern crate proc_macro;
22

3+
mod class;
34
mod import;
45
mod java_class;
56

7+
use quote::quote;
68
use syn::DeriveInput;
79

8-
#[proc_macro_derive(JavaClass, attributes(package, rename, variant, instance, field, enum_of))]
10+
#[proc_macro_derive(
11+
JavaClass,
12+
attributes(package, rename, variant, instance, field, enum_of)
13+
)]
914
pub fn java_class(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
1015
let input = syn::parse_macro_input!(input as DeriveInput);
1116

@@ -32,3 +37,29 @@ pub fn import(
3237

3338
data.unwrap_or_else(|e| e.into_compile_error()).into()
3439
}
40+
41+
#[proc_macro_attribute]
42+
pub fn class(
43+
input: proc_macro::TokenStream,
44+
body: proc_macro::TokenStream,
45+
) -> proc_macro::TokenStream {
46+
let input = syn::parse_macro_input!(input as class::ClassInput);
47+
let body = syn::parse_macro_input!(body as class::ClassBody);
48+
49+
let data = class::main(input, body);
50+
51+
data.unwrap_or_else(|e| e.into_compile_error()).into()
52+
}
53+
54+
#[proc_macro]
55+
pub fn test(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
56+
let input = match syn::parse::<syn::LitStr>(input) {
57+
Ok(o) => o,
58+
Err(e) => return e.to_compile_error().into()
59+
};
60+
let v = std::env::var("MINE").unwrap_or_default() + &input.value();
61+
println!("{}", v);
62+
std::env::set_var("MINE", v);
63+
quote! {}.into()
64+
}
65+

rust/mod/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ crate-type = ["cdylib"]
88

99
[dependencies]
1010
jni = "0.21.1"
11-
probe = { path = "../api/" }
11+
rosttasse = { path = "../api/" }
12+
13+
[build-dependencies]
14+
rosttasse = { path = "../api/" }

0 commit comments

Comments
 (0)