use crate::rpc_method::{MethodDoc, RpcMethod};
#[derive(Clone, Debug, Default)]
pub struct Registry {
methods: Vec<MethodDoc>,
}
impl Registry {
pub fn new() -> Self {
Self::default()
}
pub fn methods(&self) -> &[MethodDoc] {
&self.methods
}
pub fn into_methods(self) -> Vec<MethodDoc> {
self.methods
}
pub fn register(&mut self, doc: MethodDoc) {
self.methods.push(doc);
}
pub fn register_method<M: RpcMethod>(&mut self) {
self.register(MethodDoc::from::<M>());
}
pub fn extend<I>(&mut self, docs: I)
where
I: IntoIterator<Item = MethodDoc>,
{
self.methods.extend(docs);
}
#[cfg(feature = "inventory")]
pub fn collect(&mut self) {
for entry in inventory::iter::<crate::MethodDocInventoryEntry> {
self.register((entry.constructor)());
}
}
}
#[cfg(feature = "inventory")]
pub struct MethodDocInventoryEntry {
pub constructor: fn() -> MethodDoc,
}
#[cfg(feature = "inventory")]
inventory::collect!(MethodDocInventoryEntry);