bitcoin_rpc_types/
types.rs1use std::collections::BTreeMap;
7use std::path::Path;
8
9use serde::{Deserialize, Serialize};
10use thiserror::Error;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct BtcArgument {
15 pub names: Vec<String>,
17 pub description: String,
19 #[serde(default, rename = "oneline_description")]
21 pub oneline_description: String,
22 #[serde(default, rename = "also_positional")]
24 pub also_positional: bool,
25 #[serde(default, rename = "type_str")]
27 pub type_str: Option<Vec<String>>,
28 pub required: bool,
30 #[serde(default)]
32 pub hidden: bool,
33 #[serde(rename = "type")]
35 pub type_: String,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct BtcResult {
41 #[serde(rename = "type")]
43 pub type_: String,
44 #[serde(default, rename = "optional")]
46 pub optional: bool,
47 #[serde(skip)]
49 pub required: bool,
50 pub description: String,
52 #[serde(default, rename = "skip_type_check")]
54 pub skip_type_check: bool,
55 #[serde(default, rename = "key_name")]
57 pub key_name: String,
58 #[serde(default)]
60 pub condition: String,
61 #[serde(default)]
63 pub inner: Vec<BtcResult>,
64}
65
66impl Default for BtcResult {
67 fn default() -> Self {
69 Self {
70 type_: String::new(),
71 optional: false,
72 required: true,
73 description: String::new(),
74 skip_type_check: false,
75 key_name: String::new(),
76 condition: String::new(),
77 inner: Vec::new(),
78 }
79 }
80}
81
82impl BtcResult {
83 pub fn new(
85 type_: String,
86 optional: bool,
87 description: String,
88 skip_type_check: bool,
89 key_name: String,
90 condition: String,
91 inner: Vec<BtcResult>,
92 ) -> Self {
93 Self {
94 type_,
95 optional,
96 required: !optional,
97 description,
98 skip_type_check,
99 key_name,
100 condition,
101 inner,
102 }
103 }
104
105 pub fn post_process(&mut self) {
107 self.required = !self.optional;
108 for inner in &mut self.inner {
109 inner.post_process();
110 }
111 }
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct BtcMethod {
117 pub name: String,
119 pub description: String,
121 #[serde(default)]
123 pub examples: String,
124 #[serde(default, rename = "argument_names")]
126 pub argument_names: Vec<String>,
127 pub arguments: Vec<BtcArgument>,
129 pub results: Vec<BtcResult>,
131}
132
133#[derive(Debug, Default, Clone, Serialize, Deserialize)]
135pub struct ApiDefinition {
136 pub rpcs: BTreeMap<String, BtcMethod>,
138}
139
140impl ApiDefinition {
141 pub fn new() -> Self { Self { rpcs: BTreeMap::new() } }
143
144 pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
146 let content = std::fs::read_to_string(path)?;
147 let mut api_def: ApiDefinition = serde_json::from_str(&content)?;
148 for method in api_def.rpcs.values_mut() {
149 for result in &mut method.results {
150 result.post_process();
151 }
152 }
153 Ok(api_def)
154 }
155
156 pub fn get_method(&self, name: &str) -> Option<&BtcMethod> { self.rpcs.get(name) }
158}
159
160#[derive(Error, Debug)]
162pub enum SchemaError {
163 #[error("Failed to parse JSON: {0}")]
165 JsonParse(#[from] serde_json::Error),
166
167 #[error("IO error: {0}")]
169 Io(#[from] std::io::Error),
170}
171
172pub type Result<T> = std::result::Result<T, SchemaError>;