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