@@ -16,52 +16,67 @@ pub(super) struct Args {
1616
1717#[ derive( Subcommand ) ]
1818pub ( super ) enum Command {
19- /// Publish a message to remote instance(s) .
19+ /// Publish a message to the current instance.
2020 Pub ( CommandPub ) ,
21- /// Manage packages .
22- Pack ( CommandPack ) ,
21+ /// Publish a message to the specified instance .
22+ PubTo ( CommandPubTo ) ,
2323 /// Subscribe to messages from all remote instances.
2424 Sub ( CommandSub ) ,
25+ /// Manage packages.
26+ Pack ( CommandPack ) ,
2527}
2628
2729#[ derive( clap:: Args ) ]
2830pub ( super ) struct CommandPub {
2931 /// The kind of message.
3032 #[ arg( index = 1 ) ]
31- pub ( super ) kind : String ,
32- /// The receiver ID.
33- #[ arg( index = 2 ) ]
34- pub ( super ) receiver : Option < u64 > ,
33+ pub ( super ) kind : String ,
3534 /// Send the message with a string body.
3635 #[ arg( long) ]
37- pub ( super ) str : Option < String > ,
36+ pub ( super ) str : Option < String > ,
3837 /// Send the message with a JSON body.
3938 #[ arg( long) ]
40- pub ( super ) json : Option < String > ,
39+ pub ( super ) json : Option < String > ,
40+ /// Send the message as string of list.
41+ #[ arg( long, num_args = 0 ..) ]
42+ pub ( super ) list : Vec < String > ,
4143}
4244
4345impl CommandPub {
4446 #[ allow( dead_code) ]
4547 pub ( super ) fn receiver ( & self ) -> Result < u64 > {
46- if let Some ( receiver) = self . receiver {
47- Ok ( receiver)
48- } else if let Some ( s) = std:: env:: var ( "YAZI_PID" ) . ok ( ) . filter ( |s| !s. is_empty ( ) ) {
48+ if let Some ( s) = std:: env:: var ( "YAZI_PID" ) . ok ( ) . filter ( |s| !s. is_empty ( ) ) {
4949 Ok ( s. parse ( ) ?)
5050 } else {
51- bail ! ( "No receiver ID provided, neither YAZI_ID environment variable found." )
51+ bail ! ( "No ` YAZI_ID` environment variable found." )
5252 }
5353 }
54+ }
5455
55- #[ allow( dead_code) ]
56- pub ( super ) fn body ( & self ) -> Result < Cow < str > > {
57- if let Some ( json) = & self . json {
58- Ok ( json. into ( ) )
59- } else if let Some ( str) = & self . str {
60- Ok ( serde_json:: to_string ( str) ?. into ( ) )
61- } else {
62- Ok ( "" . into ( ) )
63- }
64- }
56+ #[ derive( clap:: Args ) ]
57+ pub ( super ) struct CommandPubTo {
58+ /// The receiver ID.
59+ #[ arg( index = 1 ) ]
60+ pub ( super ) receiver : u64 ,
61+ /// The kind of message.
62+ #[ arg( index = 2 ) ]
63+ pub ( super ) kind : String ,
64+ /// Send the message with a string body.
65+ #[ arg( long) ]
66+ pub ( super ) str : Option < String > ,
67+ /// Send the message with a JSON body.
68+ #[ arg( long) ]
69+ pub ( super ) json : Option < String > ,
70+ /// Send the message as string of list.
71+ #[ arg( long, num_args = 0 ..) ]
72+ pub ( super ) list : Vec < String > ,
73+ }
74+
75+ #[ derive( clap:: Args ) ]
76+ pub ( super ) struct CommandSub {
77+ /// The kind of messages to subscribe to, separated by commas if multiple.
78+ #[ arg( index = 1 ) ]
79+ pub ( super ) kinds : String ,
6580}
6681
6782#[ derive( clap:: Args ) ]
@@ -81,9 +96,25 @@ pub(super) struct CommandPack {
8196 pub ( super ) upgrade : bool ,
8297}
8398
84- #[ derive( clap:: Args ) ]
85- pub ( super ) struct CommandSub {
86- /// The kind of messages to subscribe to, separated by commas if multiple.
87- #[ arg( index = 1 ) ]
88- pub ( super ) kinds : String ,
99+ // --- Macros
100+ macro_rules! impl_body {
101+ ( $name: ident) => {
102+ impl $name {
103+ #[ allow( dead_code) ]
104+ pub ( super ) fn body( & self ) -> Result <Cow <str >> {
105+ if let Some ( json) = & self . json {
106+ Ok ( json. into( ) )
107+ } else if let Some ( str ) = & self . str {
108+ Ok ( serde_json:: to_string( str ) ?. into( ) )
109+ } else if !self . list. is_empty( ) {
110+ Ok ( serde_json:: to_string( & self . list) ?. into( ) )
111+ } else {
112+ Ok ( "" . into( ) )
113+ }
114+ }
115+ }
116+ } ;
89117}
118+
119+ impl_body ! ( CommandPub ) ;
120+ impl_body ! ( CommandPubTo ) ;
0 commit comments