Skip to content

Commit 39d202b

Browse files
feat(plugin): include the macro to write in a clear way the plugins
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 69014d2 commit 39d202b

File tree

3 files changed

+81
-102
lines changed

3 files changed

+81
-102
lines changed

Cargo.lock

+26-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coffee_plugin/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
tokio = { version = "1.22.0", features = ["rt"] }
88
clightningrpc-common = "0.3.0-beta.3"
99
clightningrpc-plugin = { version = "0.3.0-beta.8", features = ["log"] }
10+
clightningrpc-plugin-macros = "0.3.0-beta.4"
1011
coffee_core = { path = "../coffee_core" }
1112
coffee_lib = { path = "../coffee_lib" }
1213
serde_json = "1"

coffee_plugin/src/plugin/plugin_mod.rs

+54-101
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use tokio::runtime::Runtime;
77

88
use clightningrpc_common::json_utils;
99
use clightningrpc_plugin::commands::RPCCommand;
10+
use clightningrpc_plugin::error;
1011
use clightningrpc_plugin::plugin::{debug, info};
11-
use clightningrpc_plugin::{add_rpc, error};
1212
use clightningrpc_plugin::{errors::PluginError, plugin::Plugin};
13+
use clightningrpc_plugin_macros::{plugin, rpc_method};
1314

1415
use coffee_core::coffee::CoffeeManager;
1516
use coffee_lib::errors::CoffeeError;
@@ -21,10 +22,18 @@ use super::state::PluginArgs;
2122
use crate::plugin::State;
2223

2324
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);
2837
Ok(plugin)
2938
}
3039

@@ -70,104 +79,48 @@ fn from<T: Display>(err: T) -> PluginError {
7079
error!("{err}")
7180
}
7281

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!({}))
7895
}
7996

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)?)
149107
}
150108

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!({}))
173126
}

0 commit comments

Comments
 (0)