File tree Expand file tree Collapse file tree 5 files changed +107
-1
lines changed
Expand file tree Collapse file tree 5 files changed +107
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ pub struct CustomEvent {
99 inner_type : Type ,
1010}
1111
12- const CUSTOM_EVENT_MSG : & str = "`custom_event` attribute can only be applied to NewType structs" ;
12+ const CUSTOM_EVENT_MSG : & str =
13+ "custom_event attribute macro can only be applied to NewType structs" ;
1314
1415impl Parse for CustomEvent {
1516 fn parse ( input : syn:: parse:: ParseStream ) -> syn:: Result < Self > {
Original file line number Diff line number Diff line change 1+ mod attribute {
2+ use yew:: custom_event;
3+
4+ #[ custom_event]
5+ struct NoAttributeGiven ( Event ) ;
6+
7+ #[ custom_event( ident = not a literal string value) ]
8+ struct AttributeValueNotALitStr ( Event ) ;
9+
10+ #[ custom_event( "string without ident" ) ]
11+ struct StringWithoutIdent ( Event ) ;
12+ }
13+
14+ mod structs {
15+ use yew:: custom_event;
16+
17+ #[ custom_event( unit_struct) ]
18+ struct UnitStruct ;
19+
20+ #[ custom_event( tuple_struct) ]
21+ struct TupleStruct ( Event , Event ) ;
22+
23+ #[ custom_event( normal_struct) ]
24+ struct PlainOldStruct {
25+ event : Event ,
26+ }
27+ }
28+
29+ fn main ( ) { }
Original file line number Diff line number Diff line change 1+ error: unexpected end of input, expected identifier
2+ --> $DIR/custom_event_fail.rs:4:5
3+ |
4+ 4 | #[custom_event]
5+ | ^^^^^^^^^^^^^^^
6+ |
7+ = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+ error: expected string literal
10+ --> $DIR/custom_event_fail.rs:7:28
11+ |
12+ 7 | #[custom_event(ident = not a literal string value)]
13+ | ^^^
14+
15+ error: expected identifier
16+ --> $DIR/custom_event_fail.rs:10:20
17+ |
18+ 10 | #[custom_event("string without ident")]
19+ | ^^^^^^^^^^^^^^^^^^^^^^
20+
21+ error: custom_event attribute macro can only be applied to NewType structs
22+ --> $DIR/custom_event_fail.rs:18:5
23+ |
24+ 18 | struct UnitStruct;
25+ | ^^^^^^^^^^^^^^^^^^
26+
27+ error: custom_event attribute macro can only be applied to NewType structs
28+ --> $DIR/custom_event_fail.rs:21:5
29+ |
30+ 21 | struct TupleStruct(Event, Event);
31+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+ error: custom_event attribute macro can only be applied to NewType structs
34+ --> $DIR/custom_event_fail.rs:24:5
35+ |
36+ 24 | / struct PlainOldStruct {
37+ 25 | | event: Event,
38+ 26 | | }
39+ | |_____^
Original file line number Diff line number Diff line change 1+ // Inside a module as public custom events can be used outside the module they are defined in.
2+ mod events {
3+ use yew:: { custom_event, web_sys:: Event } ;
4+
5+ // NewType with matching event type and ident - i.e. "custard" can be custard
6+ #[ custom_event( custard) ]
7+ pub struct CustardEvent ( Event ) ;
8+
9+ #[ custom_event( pbj = "peanut butter jelly" ) ]
10+ pub struct PeanutButterJellyEvent ( Event ) ;
11+ }
12+
13+ use yew:: custom_event;
14+
15+ // Expand on a custom event
16+ #[ custom_event( dessert) ]
17+ struct DessertEvent ( events:: CustardEvent ) ;
18+
19+ fn main ( ) {
20+ use events:: * ;
21+ use yew:: StaticEvent ;
22+ assert_eq ! ( "custard" , custard:: event_name( ) ) ;
23+ assert_eq ! ( "custard" , CustardEvent :: event_name( ) ) ;
24+
25+ assert_eq ! ( "peanut butter jelly" , pbj:: event_name( ) ) ;
26+ assert_eq ! ( "peanut butter jelly" , PeanutButterJellyEvent :: event_name( ) ) ;
27+
28+ assert_eq ! ( "dessert" , dessert:: event_name( ) ) ;
29+ assert_eq ! ( "dessert" , DessertEvent :: event_name( ) ) ;
30+ }
Original file line number Diff line number Diff line change 1+ #[ allow( dead_code) ]
2+ #[ rustversion:: attr( stable( 1.51 ) , test) ]
3+ fn tests ( ) {
4+ let t = trybuild:: TestCases :: new ( ) ;
5+ t. pass ( "tests/custom_event/custom_event_pass.rs" ) ;
6+ t. compile_fail ( "tests/custom_event/custom_event_fail.rs" ) ;
7+ }
You can’t perform that action at this time.
0 commit comments