@@ -7,9 +7,10 @@ use tokio::runtime::Runtime;
7
7
8
8
use clightningrpc_common:: json_utils;
9
9
use clightningrpc_plugin:: commands:: RPCCommand ;
10
+ use clightningrpc_plugin:: error;
10
11
use clightningrpc_plugin:: plugin:: { debug, info} ;
11
- use clightningrpc_plugin:: { add_rpc, error} ;
12
12
use clightningrpc_plugin:: { errors:: PluginError , plugin:: Plugin } ;
13
+ use clightningrpc_plugin_macros:: { plugin, rpc_method} ;
13
14
14
15
use coffee_core:: coffee:: CoffeeManager ;
15
16
use coffee_lib:: errors:: CoffeeError ;
@@ -21,10 +22,18 @@ use super::state::PluginArgs;
21
22
use crate :: plugin:: State ;
22
23
23
24
pub fn build_plugin ( ) -> Result < Plugin < State > , PluginError > {
24
- let mut plugin = Plugin :: < State > :: new ( State :: new ( ) , /* dynamic */ true ) . on_init ( on_init) ;
25
- add_rpc ! ( plugin, CoffeeInstall ) ;
26
- add_rpc ! ( plugin, CoffeeList ) ;
27
- add_rpc ! ( plugin, CoffeeRemote ) ;
25
+ let mut plugin = plugin ! {
26
+ state: State :: new( ) ,
27
+ dynamic: true ,
28
+ notification: [ ] ,
29
+ methods: [
30
+ coffee_install,
31
+ coffee_list,
32
+ coffee_remote,
33
+ ] ,
34
+ hooks: [ ] ,
35
+ } ;
36
+ plugin. on_init ( on_init) ;
28
37
Ok ( plugin)
29
38
}
30
39
@@ -70,104 +79,48 @@ fn from<T: Display>(err: T) -> PluginError {
70
79
error ! ( "{err}" )
71
80
}
72
81
73
- #[ derive( Clone ) ]
74
- struct CoffeeInstall {
75
- name : String ,
76
- usage : String ,
77
- description : String ,
82
+ #[ rpc_method(
83
+ rpc_name = "coffee_install" ,
84
+ description = "install a plugin from one of the repository choosed"
85
+ ) ]
86
+ fn coffee_install ( plugin : & mut Plugin < State > , request : Value ) -> Result < Value , PluginError > {
87
+ let coffee = plugin. state . coffee ( ) ;
88
+ let mut coffee = coffee. lock ( ) . unwrap ( ) ;
89
+ let rt = Runtime :: new ( ) . unwrap ( ) ;
90
+
91
+ let request: InstallReq = serde_json:: from_value ( request) ?;
92
+ rt. block_on ( coffee. install ( & request. name , false , true ) )
93
+ . map_err ( from) ?;
94
+ Ok ( json ! ( { } ) )
78
95
}
79
96
80
- impl CoffeeInstall {
81
- fn new ( ) -> Self {
82
- CoffeeInstall {
83
- name : "coffee_install" . to_string ( ) ,
84
- usage : String :: new ( ) ,
85
- description : String :: from ( "install a plugin from one of the repository choosed" ) ,
86
- }
87
- }
88
- }
89
-
90
- impl RPCCommand < State > for CoffeeInstall {
91
- fn call < ' c > (
92
- & self ,
93
- plugin : & mut Plugin < State > ,
94
- request : serde_json:: Value ,
95
- ) -> Result < serde_json:: Value , PluginError > {
96
- let coffee = plugin. state . coffee ( ) ;
97
- let mut coffee = coffee. lock ( ) . unwrap ( ) ;
98
- let rt = Runtime :: new ( ) . unwrap ( ) ;
99
-
100
- let request: InstallReq = serde_json:: from_value ( request) ?;
101
- rt. block_on ( coffee. install ( & request. name , false , true ) )
102
- . map_err ( from) ?;
103
- Ok ( json ! ( { } ) )
104
- }
105
- }
106
-
107
- #[ derive( Clone ) ]
108
- struct CoffeeList {
109
- name : String ,
110
- usage : String ,
111
- description : String ,
112
- }
113
-
114
- impl CoffeeList {
115
- fn new ( ) -> Self {
116
- CoffeeList { name : "coffee_list" . to_owned ( ) , usage : String :: new ( ) , description : "show all the plugin installed and if {remotes} is specified show also the one available" . to_owned ( ) }
117
- }
118
- }
119
-
120
- impl RPCCommand < State > for CoffeeList {
121
- fn call < ' c > (
122
- & self ,
123
- plugin : & mut Plugin < State > ,
124
- _: serde_json:: Value ,
125
- ) -> Result < serde_json:: Value , PluginError > {
126
- let runtime = Runtime :: new ( ) . unwrap ( ) ;
127
- let coffee = plugin. state . coffee ( ) ;
128
- let mut coffee = coffee. lock ( ) . unwrap ( ) ;
129
- let result = runtime. block_on ( coffee. list ( ) ) . map_err ( from) ?;
130
- Ok ( serde_json:: to_value ( result) ?)
131
- }
132
- }
133
-
134
- #[ derive( Clone ) ]
135
- struct CoffeeRemote {
136
- name : String ,
137
- usage : String ,
138
- description : String ,
139
- }
140
-
141
- impl CoffeeRemote {
142
- fn new ( ) -> Self {
143
- CoffeeRemote {
144
- name : "coffee_remote" . to_owned ( ) ,
145
- usage : String :: new ( ) ,
146
- description : "manage a remote" . to_owned ( ) ,
147
- }
148
- }
97
+ #[ rpc_method(
98
+ rpc_name = "coffee_list" ,
99
+ description = "show all the plugin installed and if {remotes} is specified show also the one available"
100
+ ) ]
101
+ fn coffee_list ( plugin : & mut Plugin < State > , _: Value ) -> Result < Value , PluginError > {
102
+ let runtime = Runtime :: new ( ) . unwrap ( ) ;
103
+ let coffee = plugin. state . coffee ( ) ;
104
+ let mut coffee = coffee. lock ( ) . unwrap ( ) ;
105
+ let result = runtime. block_on ( coffee. list ( ) ) . map_err ( from) ?;
106
+ Ok ( serde_json:: to_value ( result) ?)
149
107
}
150
108
151
- impl RPCCommand < State > for CoffeeRemote {
152
- fn call < ' c > (
153
- & self ,
154
- plugin : & mut Plugin < State > ,
155
- request : serde_json:: Value ,
156
- ) -> Result < serde_json:: Value , PluginError > {
157
- let request: RemoteReq = serde_json:: from_value ( request) ?;
158
- let runtime = Runtime :: new ( ) . unwrap ( ) ;
159
- let coffee = plugin. state . coffee ( ) ;
160
-
161
- runtime
162
- . block_on ( async {
163
- let mut coffee = coffee. lock ( ) . unwrap ( ) ;
164
- let cmd = request. cmd ( ) . unwrap ( ) ;
165
- match cmd {
166
- RemoteCmd :: Add => coffee. add_remote ( & request. name , & request. url ( ) ) . await ,
167
- RemoteCmd :: Rm => coffee. rm_remote ( & request. name ) . await ,
168
- }
169
- } )
170
- . map_err ( from) ?;
171
- Ok ( json ! ( { } ) )
172
- }
109
+ #[ rpc_method( rpc_name = "coffee_remote" , description = "manage a remote" ) ]
110
+ fn coffee_remote ( plugin : & mut Plugin < State > , request : Value ) -> Result < Value , PluginError > {
111
+ let request: RemoteReq = serde_json:: from_value ( request) ?;
112
+ let runtime = Runtime :: new ( ) . unwrap ( ) ;
113
+ let coffee = plugin. state . coffee ( ) ;
114
+
115
+ runtime
116
+ . block_on ( async {
117
+ let mut coffee = coffee. lock ( ) . unwrap ( ) ;
118
+ let cmd = request. cmd ( ) . unwrap ( ) ;
119
+ match cmd {
120
+ RemoteCmd :: Add => coffee. add_remote ( & request. name , & request. url ( ) ) . await ,
121
+ RemoteCmd :: Rm => coffee. rm_remote ( & request. name ) . await ,
122
+ }
123
+ } )
124
+ . map_err ( from) ?;
125
+ Ok ( json ! ( { } ) )
173
126
}
0 commit comments