tendermint_testgen/generator.rs
1use std::str::FromStr;
2
3use serde::Serialize;
4use simple_error::*;
5
6/// A trait that allows to generate complex objects from simple companion objects.
7/// A companion type should have a simple API, leaving most fields optional.
8pub trait Generator<Output: Serialize>: FromStr<Err = SimpleError> + Clone {
9 /// Merge this companion with the another, default one.
10 /// The options present in this object will override those in the default one.
11 fn merge_with_default(self, default: Self) -> Self;
12
13 /// Generate the complex object from this companion object.
14 fn generate(&self) -> Result<Output, SimpleError>;
15
16 /// Generate and serialize the complex object
17 fn encode(&self) -> Result<String, SimpleError> {
18 let res = self.generate()?;
19 Ok(try_with!(
20 serde_json::to_string_pretty(&res),
21 "failed to serialize into JSON"
22 ))
23 }
24}