typed_openrpc/
registry.rs1use crate::rpc_method::{MethodDoc, RpcMethod};
2
3#[derive(Clone, Debug, Default)]
5pub struct Registry {
6 methods: Vec<MethodDoc>,
7}
8
9impl Registry {
10 pub fn new() -> Self {
12 Self::default()
13 }
14
15 pub fn methods(&self) -> &[MethodDoc] {
17 &self.methods
18 }
19
20 pub fn into_methods(self) -> Vec<MethodDoc> {
22 self.methods
23 }
24
25 pub fn register(&mut self, doc: MethodDoc) {
27 self.methods.push(doc);
28 }
29
30 pub fn register_method<M: RpcMethod>(&mut self) {
32 self.register(MethodDoc::from::<M>());
33 }
34
35 pub fn extend<I>(&mut self, docs: I)
37 where
38 I: IntoIterator<Item = MethodDoc>,
39 {
40 self.methods.extend(docs);
41 }
42
43 #[cfg(feature = "inventory")]
44 pub fn collect(&mut self) {
51 for entry in inventory::iter::<crate::MethodDocInventoryEntry> {
52 self.register((entry.constructor)());
53 }
54 }
55}
56
57#[cfg(feature = "inventory")]
59pub struct MethodDocInventoryEntry {
60 pub constructor: fn() -> MethodDoc,
61}
62
63#[cfg(feature = "inventory")]
64inventory::collect!(MethodDocInventoryEntry);