Skip to content

Commit

Permalink
fix(plugin): fix the option conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 14, 2024
1 parent 8e347cb commit 08336e0
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion plugin/examples/foo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() {
.add_opt(
"foo",
"flag",
None,
Some("false".to_owned()),
"An example of command line option",
false,
)
Expand Down
18 changes: 8 additions & 10 deletions plugin/src/commands/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
use std::collections::HashMap;
use std::sync::Arc;

use crate::commands::{
types::{RPCHookInfo, RPCMethodInfo},
RPCCommand,
};
use serde_json::Value;

use clightningrpc_common::json_utils::{add_bool, add_vec, init_payload};

use crate::commands::types::{InitConf, RPCHookInfo, RPCMethodInfo};
use crate::commands::RPCCommand;
use crate::errors::PluginError;
use crate::plugin::Plugin;
use crate::types::RpcOption;
use clightningrpc_common::json_utils::{add_bool, add_vec, init_payload};
use serde_json::Value;

use super::types::InitConf;

#[derive(Clone)]
/// Type to define the manifest method and its attributes, used during plugin initialization
#[derive(Clone)]
pub struct ManifestRPC {}

impl<T: Clone> RPCCommand<T> for ManifestRPC {
fn call<'c>(&self, plugin: &mut Plugin<T>, _request: Value) -> Result<Value, PluginError> {
fn call<'c>(&self, plugin: &mut Plugin<T>, _: Value) -> Result<Value, PluginError> {
let mut response = init_payload();
add_vec::<RpcOption>(
&mut response,
Expand Down
8 changes: 8 additions & 0 deletions plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ impl<'a, T: 'a + Clone> Plugin<T> {
description: &str,
deprecated: bool,
) -> &mut Self {
let def_val = match opt_type {
"flag" | "bool" => {
def_val.and_then(|val| Some(serde_json::json!(val.parse::<bool>().unwrap())))

Check warning on line 142 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`

warning: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)` --> plugin/src/plugin.rs:142:17 | 142 | def_val.and_then(|val| Some(serde_json::json!(val.parse::<bool>().unwrap()))) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `def_val.map(|val| serde_json::json!(val.parse::<bool>().unwrap()))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map = note: `#[warn(clippy::bind_instead_of_map)]` on by default
}
"int" => def_val.and_then(|val| Some(serde_json::json!(val.parse::<i64>().unwrap()))),

Check warning on line 144 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`

warning: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)` --> plugin/src/plugin.rs:144:22 | 144 | "int" => def_val.and_then(|val| Some(serde_json::json!(val.parse::<i64>().unwrap()))), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `def_val.map(|val| serde_json::json!(val.parse::<i64>().unwrap()))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
"string" => def_val.and_then(|val| Some(serde_json::json!(val))),

Check warning on line 145 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`

warning: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)` --> plugin/src/plugin.rs:145:25 | 145 | "string" => def_val.and_then(|val| Some(serde_json::json!(val))), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `def_val.map(|val| serde_json::json!(val))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
_ => unreachable!("{opt_type} not supported"),
};
self.option.insert(
name.to_owned(),
RpcOption {
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct RpcOption {
pub opt_typ: String,
/// default value, that can be specified only as string
/// and core lightning will convert it for you :) smart right?
pub default: Option<String>,
pub default: Option<serde_json::Value>,
/// description of the option that is shows to the user
/// when lightningd --help is typed
pub description: String,
Expand Down
6 changes: 3 additions & 3 deletions plugin_macros/examples/macros_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub fn foo_rpc(plugin: &mut Plugin<State>, request: Value) -> Result<Value, Plug
Ok(response)
}

#[notification(on = "rpc_command")]
fn on_rpc(plugin: &mut Plugin<State>, request: &Value) {
#[notification(on = "warning")]
fn on_warning(plugin: &mut Plugin<State>, request: &Value) {
use clightningrpc_plugin::types::LogLevel;
plugin.log(LogLevel::Info, "received an RPC notification");
}
Expand All @@ -40,7 +40,7 @@ fn main() {
state: State::new(),
dynamic: true,
notification: [
on_rpc,
on_warning,
],
methods: [
foo_rpc,
Expand Down
2 changes: 1 addition & 1 deletion testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ log = "^0.4"
tempfile = "3.6.0"
port-selector = "0.1.6"
anyhow = "1.0.71"
tokio = { version = "1.22.0", features = ["process", "time", "fs"] }
tokio = { version = "1.22.0", features = ["process", "time", "fs", "io-util"] }
8 changes: 5 additions & 3 deletions testing/src/cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub mod macros {
macro_rules! lightningd {
($dir:expr, $port:expr, $($opt_args:tt)*) => {
async {
use std::process::Stdio;

use tokio::process::Command;

let opt_args = format!($($opt_args)*);
Expand All @@ -31,9 +29,11 @@ pub mod macros {
.arg(format!("--addr=127.0.0.1:{}", $port))
.arg(format!("--bind-addr=127.0.0.1:{}", $port + 1))
.arg(format!("--lightning-dir={path}"))
.arg("--developer")
.arg("--dev-fast-gossip")
.arg("--funding-confirms=1")
.stdout(Stdio::null())
.arg(format!("--log-file={path}/log.log"))
.stdout(std::process::Stdio::null())
.spawn()
}.await
};
Expand Down Expand Up @@ -117,6 +117,8 @@ impl Node {
self.inner.clone()
}

// FIXME: add a method to print the log file

pub fn btc(&self) -> Arc<BtcNode> {
self.bitcoin.clone()
}
Expand Down
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
clightning-testing = { path = "../testing" }
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] }
log = "*"
env_logger = "0.11.1"
serde_json = "1.0.1"
anyhow = "1.0.71"
Expand Down
3 changes: 1 addition & 2 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ macro_rules! async_run {

pub mod fixtures {

use rstest::*;

use clightning_testing::cln;
use rstest::*;

use super::{async_run, init};

Expand Down
1 change: 0 additions & 1 deletion tests/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ fn generate_invoice_with_description_hash(lightningd: cln::Node) {
Some(true),
)
.unwrap();
println!("{:?}", invoice);
let decode = lightningd.decodepay(&invoice.bolt11, None).unwrap();
assert_eq!(decode.amount_msat, Some(MSat(1)));
assert_eq!(
Expand Down

0 comments on commit 08336e0

Please sign in to comment.