1use bitcoin::*;
2use jsonschema::JSONSchema;
3use sapio_base::plugin_args::ContextualArguments;
4use sapio_base::plugin_args::CreateArgs;
5use schemars::*;
6use serde::*;
7use serde_json::Value;
8use std::error::Error;
9pub trait SapioAPIHandle {
10 fn get_api(&self) -> serde_json::Value;
11}
12impl SapioAPIHandle for serde_json::Value {
13 fn get_api(&self) -> Self {
14 self.clone()
15 }
16}
17pub trait SapioJSONTrait: JsonSchema + Serialize + for<'a> Deserialize<'a> {
18 fn get_example_for_api_checking() -> Value;
19 fn check_trait_implemented_inner(api: &dyn SapioAPIHandle) -> Result<(), Box<dyn Error>> {
20 let tag = Self::get_example_for_api_checking();
21 let japi = api.get_api();
22 let compiled = JSONSchema::compile(&japi).map_err(|_| "Error Compiling Schema")?;
23 compiled
24 .validate(&serde_json::to_value(CreateArgs {
25 arguments: tag,
26 context: ContextualArguments {
27 amount: Amount::from_sat(0),
28 network: Network::Bitcoin,
29 effects: Default::default(),
30 },
31 })?)
32 .map_err(|e| {
33 let mut s = String::from("Validation Errors:");
34 for error in e {
35 s += &format!("\n - {}", error.to_string());
36 }
37 s
38 })?;
39 Ok(())
40 }
41 fn check_trait_implemented(api: &dyn SapioAPIHandle) -> bool {
42 Self::check_trait_implemented_inner(api).is_ok()
43 }
44}