ag_grid_derive/
lib.rs

1use syn::DeriveInput;
2
3mod field_setter;
4mod from_interface;
5mod to_js_value;
6
7#[proc_macro_derive(FieldSetter, attributes(field_setter))]
8pub fn field_setter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
9    let input = syn::parse::<DeriveInput>(input).unwrap();
10    field_setter::field_setter_impl(input)
11}
12
13/// Automatically derive the `ToJsValue` trait to enable the annotated type to
14/// be serialized to a `wasm_bindgen::JsValue`.
15///
16/// The macro can be applied either to structs with named fields, or fieldless
17/// enums.
18///
19/// # Examples
20///
21/// ## Structs with named fields
22///
23/// The macro can be derived for any struct with named fields where all field
24/// types also implement `ToJsValue`. Most primitive types have a `ToJsValue`
25/// implemntation already. Given the following struct,
26///
27/// ```rust
28/// #[derive(ToJsValue)]
29/// struct Data {
30///     first_value: String,
31///     second_value: bool
32/// }
33/// ```
34///
35/// the following equivalent implementation would be generated:
36///
37/// ```rust
38/// impl ToJsValue for Data {
39///     fn to_js_value(&self) -> JsValue {
40///         let obj = js_sys::Object::new();
41///         // We actually use a custom implementation of Object which has a helper `set` method.
42///         obj.set("firstValue", self.first_value.to_js_value());
43///         obj.set("secondValue", self.second_value.to_js_value());
44///         obj.into()
45///     }
46/// }
47/// ```
48///
49/// Note that the field names are converted to camelCase by default.
50///
51/// ### Supported attributes
52///
53/// At the the container-level:
54/// * `#[js_value(skip_serializing_none)]` - any top-level `Option<T>` which has
55///   a value of `None` will be omitted from the serialized object. When this
56///   flag is absent, i.e. by default, `None` values are serialized to
57///   `JsValue::null()`.
58///
59/// At the field-level:
60/// * `#[js_value(rename = "...")]` - override the default camelCase name for
61///   the serialized field.
62///
63/// ## Fieldless enums
64///
65/// A fieldless enum, such as
66///
67/// ```rust
68/// derive(ToJsValue)
69/// enum MoonPhase {
70///     New,
71///     FirstQuarter,
72///     ThirdQuarter,
73///     Full     
74/// }
75/// ```
76///
77/// would produce an implementation equivalent to:
78///
79/// ```rust
80/// impl ToJsValue for MoonPhase {
81///     fn to_js_value(&self) -> JsValue{
82///         Self::New => JsValue::from_str("new"),
83///         Self::New => JsValue::from_str("firstQuarter"),
84///         Self::New => JsValue::from_str("ThirdQuarter"),
85///         Self::New => JsValue::from_str("full"),
86///     }
87/// }
88/// ```
89///
90/// As with structs the default behaviour is to serialize the variant to
91/// camelCase.
92///
93/// ### Supported attributes
94///
95/// At the variant-level:
96/// * `#[js_value(rename = "...")]` - override the default camelCase name for
97///   the serialized variant.
98/// * `#[js_value(serialize_as = "{placeholder}")` - , where `{placeholder}` can
99///   be one of : `null`, `undefined`, `true` or `false`. Instead of the tagged
100///   variant being serialized to a string, it is instead serialized to one of
101///   the chosen literal JavaScript Values. It is useful if, for example, you
102///   have an enum with a `Null` variant and want it to be serialized to a a
103///   primitive `null` rather than the string `"null"`.
104#[proc_macro_derive(ToJsValue, attributes(js_value))]
105pub fn to_js_value(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
106    let input = syn::parse::<DeriveInput>(input).unwrap();
107    to_js_value::to_js_value_impl(input)
108}
109
110#[proc_macro_derive(FromInterface)]
111pub fn from_interface(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
112    let input = syn::parse::<DeriveInput>(input).unwrap();
113    from_interface::from_interface_impl(input)
114}