event_args

Macro event_args 

Source
macro_rules! event_args {
    ($(
        $(#[$outer:meta])*
        $vis:vis struct $Args:ident {
            $($(#[$arg_outer:meta])* $arg_vis:vis $arg:ident : $arg_ty:ty,)*
            ..
            $(#[$is_in_target_outer:meta])*
            fn is_in_target(&$self:ident, $is_in_target_id:ident: WidgetId) -> bool { $($is_in_target:tt)* }

            $(
                $(#[$validate_outer:meta])*
                fn validate(&$self_v:ident) -> Result<(), $ValidationError:path> { $($validate:tt)+ }
            )?
        }
    )+) => { ... };
}
Expand description

Declares new EventArgs types.

The macro syntax is similar to struct declaration, but after the args struct members you must add .. and then the fn is_in_target(&self, widget: WidgetId) -> bool { } method that matches the widget target.

After the is_in_target method you can also optionally add a fn validate(&self) -> Result<(), Txt> { } method that validates the arguments.

The macro expansion implements the EventArgs trait for the new structs, it generates a public timestamp member and a new and now associated functions. The new function instantiates args with custom timestamp and propagation handle, the now function provides the timestamp and propagation handle and is the primary way to instantiate args.

ยงExamples

event_args! {
    /// My event arguments.
    pub struct MyEventArgs {
        /// My argument.
        pub arg: String,
        /// My event target.
        pub target: WidgetPath,

        ..

        fn is_in_target(&self, widget: WidgetId) -> bool {
            self.target.contains(widget)
        }

        /// Optional validation, if defined the generated `new` and `now` functions call it and unwrap the result.
        ///
        /// The error type can be any type that implement `Debug`.
        fn validate(&self) -> Result<(), Txt> {
            if self.arg.contains("error") {
                return Err(formatx!("invalid arg `{}`", self.arg));
            }
            Ok(())
        }
    }

    // multiple structs can be declared in the same call.
    // pub struct MyOtherEventArgs { /**/ }
}