rusteal_macros/lib.rs
1// rusteal-macros: proc macros for #[uclass], #[ustruct], #[ufunction], #[uproperty].
2
3mod prop_type;
4mod uclass;
5mod uclass_impl;
6mod ustruct;
7
8/// Attribute macro for defining a Rust struct as a UE class.
9///
10/// # Example
11/// ```ignore
12/// #[uclass(parent = Actor)]
13/// pub struct MyEnemy {
14/// #[uproperty(BlueprintReadWrite, default = 100.0)]
15/// health: f32,
16///
17/// // Rust-private field (no #[uproperty])
18/// chase_timer: f64,
19/// }
20/// ```
21#[proc_macro_attribute]
22pub fn uclass(
23 attr: proc_macro::TokenStream,
24 item: proc_macro::TokenStream,
25) -> proc_macro::TokenStream {
26 match uclass::expand_uclass(attr.into(), item.into()) {
27 Ok(tokens) => tokens.into(),
28 Err(err) => err.to_compile_error().into(),
29 }
30}
31
32/// Attribute macro for an impl block on a `#[uclass]` struct.
33/// Methods marked `#[ufunction(...)]` become UE-callable functions.
34///
35/// # Example
36/// ```ignore
37/// #[uclass_impl]
38/// impl MyEnemy {
39/// #[ufunction(BlueprintCallable)]
40/// fn take_damage(&mut self, amount: f32) -> bool {
41/// let h = self.health() - amount;
42/// self.set_health(h.max(0.0));
43/// h <= 0.0
44/// }
45///
46/// fn helper(&self) -> f32 { self.health() * 2.0 } // plain Rust method
47/// }
48/// ```
49#[proc_macro_attribute]
50pub fn uclass_impl(
51 attr: proc_macro::TokenStream,
52 item: proc_macro::TokenStream,
53) -> proc_macro::TokenStream {
54 match uclass_impl::expand_uclass_impl(attr.into(), item.into()) {
55 Ok(tokens) => tokens.into(),
56 Err(err) => err.to_compile_error().into(),
57 }
58}
59
60/// Attribute macro for defining a Rust struct as a UE struct: a data table's
61/// row, a property's value. Every field is a `#[uproperty]`; the struct's
62/// memory is UE's, read and written through the generated `<Name>Ext` trait
63/// on `UStructRef<Name>` and `OwnedStruct<Name>`.
64///
65/// # Example
66/// ```ignore
67/// #[ustruct]
68/// pub struct WeaponTableRow {
69/// #[uproperty(EditAnywhere)]
70/// static_mesh: SoftObjectRef<StaticMesh>,
71/// #[uproperty(EditAnywhere)]
72/// weapon_to_spawn: SubclassOf<ShooterWeapon>,
73/// }
74/// ```
75#[proc_macro_attribute]
76pub fn ustruct(
77 attr: proc_macro::TokenStream,
78 item: proc_macro::TokenStream,
79) -> proc_macro::TokenStream {
80 match ustruct::expand_ustruct(attr.into(), item.into()) {
81 Ok(tokens) => tokens.into(),
82 Err(err) => err.to_compile_error().into(),
83 }
84}