Skip to content

Commit

Permalink
cln-plugin: Allow setting usage of RPC-method
Browse files Browse the repository at this point in the history
The `cln-plugin` can be used to create a plugin that registers
additional rpc-methods. However, it doesn't allow to specify the `usage`
of the command.

This change makes it possible to specify the `usage`. It should not
contain any breaking changes.

I've opted to add a new method called `rpcmethod_from_builder` to the
`Builder` struct. This approach allows us to add new parameters in the
future.
  • Loading branch information
ErikDeSmedt authored and Christian Decker committed Jan 16, 2024
1 parent 43e302c commit e3e3a3d
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,19 @@ where
RpcMethod {
name: name.to_string(),
description: description.to_string(),
usage: String::default(),
callback: Box::new(move |p, r| Box::pin(callback(p, r))),
},
);
self
}

pub fn rpcmethod_from_builder(mut self, rpc_method: RpcMethodBuilder<S>) -> Builder<S, I, O> {
self.rpcmethods
.insert(rpc_method.name.to_string(), rpc_method.build());
self
}

/// Send true value for "dynamic" field in "getmanifest" response
pub fn dynamic(mut self) -> Builder<S, I, O> {
self.dynamic = true;
Expand Down Expand Up @@ -335,7 +342,7 @@ where
.map(|v| messages::RpcMethod {
name: v.name.clone(),
description: v.description.clone(),
usage: String::new(),
usage: v.usage.clone(),
})
.collect();

Expand Down Expand Up @@ -383,6 +390,44 @@ where
}
}

impl<S> RpcMethodBuilder<S>
where
S: Send + Clone,
{
pub fn new<C, F>(name: &str, callback: C) -> Self
where
C: Send + Sync + 'static,
C: Fn(Plugin<S>, Request) -> F + 'static,
F: Future<Output = Response> + Send + 'static,
{
Self {
name: name.to_string(),
callback: Box::new(move |p, r| Box::pin(callback(p, r))),
usage: None,
description: None,
}
}

pub fn description(mut self, description: &str) -> Self {
self.description = Some(description.to_string());
self
}

pub fn usage(mut self, usage: &str) -> Self {
self.usage = Some(usage.to_string());
self
}

fn build(self) -> RpcMethod<S> {
RpcMethod {
callback: self.callback,
name: self.name,
description: self.description.unwrap_or_default(),
usage: self.usage.unwrap_or_default(),
}
}
}

// Just some type aliases so we don't get confused in a lisp-like sea
// of parentheses.
type Request = serde_json::Value;
Expand All @@ -405,6 +450,17 @@ where
callback: AsyncCallback<S>,
description: String,
name: String,
usage: String,
}

pub struct RpcMethodBuilder<S>
where
S: Clone + Send,
{
callback: AsyncCallback<S>,
name: String,
description: Option<String>,
usage: Option<String>,
}

struct Subscription<S>
Expand Down

0 comments on commit e3e3a3d

Please sign in to comment.