forked from RGB-WG/rgb-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstash.rs
192 lines (178 loc) · 5.94 KB
/
stash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// RGB standard library
// Written in 2020 by
// Dr. Maxim Orlovsky <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use lnpbp::rgb::{ContractId, SchemaId, ToBech32};
use crate::api::Reply;
use crate::cli::{Error, OutputFormat, Runtime};
#[derive(Clap, Clone, Debug, Display)]
#[display(Debug)]
pub enum SchemaCommand {
/// Lists all known schemata
List {
/// Format for information output
#[clap(short, long, arg_enum, default_value = "yaml")]
format: OutputFormat,
},
/// Export schema data
Export {
/// Format for information output
#[clap(short, long, arg_enum, default_value = "yaml")]
format: OutputFormat,
#[clap()]
schema_id: SchemaId,
},
}
#[derive(Clap, Clone, Debug, Display)]
#[display(Debug)]
pub enum GenesisCommand {
/// Lists all known contract ids
List {
/// Format for information output
#[clap(short, long, arg_enum, default_value = "yaml")]
format: OutputFormat,
},
/// Export schema data
Export {
/// Format for information output
#[clap(short, long, arg_enum, default_value = "yaml")]
format: OutputFormat,
#[clap()]
contract_id: ContractId,
},
}
impl SchemaCommand {
pub fn exec(self, runtime: Runtime) -> Result<(), Error> {
match self {
SchemaCommand::List { format } => self.exec_list(runtime, format),
SchemaCommand::Export { format, schema_id } => {
self.exec_export(runtime, format, schema_id)
}
}
}
fn exec_list(
&self,
mut runtime: Runtime,
format: OutputFormat,
) -> Result<(), Error> {
match &*runtime.list_schemata()? {
Reply::Failure(failure) => {
eprintln!("Server returned error: {}", failure);
}
Reply::SchemaIds(ids) => {
let output = match format {
OutputFormat::Yaml => serde_yaml::to_string(&ids)?,
OutputFormat::Json => serde_json::to_string(&ids)?,
OutputFormat::Toml => toml::to_string(&ids)?,
_ => Err(Error::FormatNotSupported)?,
};
println!("{}", output);
}
_ => {
eprintln!(
"Unexpected server error; probably you connecting with outdated client version"
);
}
}
Ok(())
}
fn exec_export(
&self,
mut runtime: Runtime,
format: OutputFormat,
schema_id: SchemaId,
) -> Result<(), Error> {
match &*runtime.schema(schema_id)? {
Reply::Failure(failure) => {
eprintln!("Server returned error: {}", failure);
}
Reply::Schema(schema) => {
let output = match format {
OutputFormat::Yaml => serde_yaml::to_string(&schema)?,
OutputFormat::Json => serde_json::to_string(&schema)?,
OutputFormat::Bech32 => schema.to_bech32_string(),
_ => Err(Error::FormatNotSupported)?,
};
println!("{}", output);
}
_ => {
eprintln!(
"Unexpected server error; probably you connecting with outdated client version"
);
}
}
Ok(())
}
}
impl GenesisCommand {
pub fn exec(self, runtime: Runtime) -> Result<(), Error> {
match self {
GenesisCommand::List { format } => self.exec_list(runtime, format),
GenesisCommand::Export {
format,
contract_id,
} => self.exec_export(runtime, format, contract_id),
}
}
fn exec_list(
&self,
mut runtime: Runtime,
format: OutputFormat,
) -> Result<(), Error> {
match &*runtime.list_geneses()? {
Reply::Failure(failure) => {
eprintln!("Server returned error: {}", failure);
}
Reply::ContractIds(ids) => {
let output = match format {
OutputFormat::Yaml => serde_yaml::to_string(&ids)?,
OutputFormat::Json => serde_json::to_string(&ids)?,
OutputFormat::Toml => toml::to_string(&ids)?,
_ => Err(Error::FormatNotSupported)?,
};
println!("{}", output);
}
_ => {
eprintln!(
"Unexpected server error; probably you connecting with outdated client version"
);
}
}
Ok(())
}
fn exec_export(
&self,
mut runtime: Runtime,
format: OutputFormat,
contract_id: ContractId,
) -> Result<(), Error> {
match &*runtime.genesis(contract_id)? {
Reply::Failure(failure) => {
eprintln!("Server returned error: {}", failure);
}
Reply::Genesis(genesis) => {
let output = match format {
OutputFormat::Yaml => serde_yaml::to_string(&genesis)?,
OutputFormat::Json => serde_json::to_string(&genesis)?,
OutputFormat::Bech32 => genesis.to_bech32_string(),
_ => Err(Error::FormatNotSupported)?,
};
println!("{}", output);
}
_ => {
eprintln!(
"Unexpected server error; probably you connecting with outdated client version"
);
}
}
Ok(())
}
}