use serde::Serialize;
use crate::sdk::ABI_VERSION;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum FieldType {
I64,
F64,
Utf8,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Field {
pub name: String,
#[serde(rename = "type")]
pub ty: FieldType,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub amount: bool,
}
impl Field {
pub fn int(name: &str) -> Self {
Field {
name: name.into(),
ty: FieldType::I64,
amount: false,
}
}
pub fn float(name: &str) -> Self {
Field {
name: name.into(),
ty: FieldType::F64,
amount: false,
}
}
pub fn text(name: &str) -> Self {
Field {
name: name.into(),
ty: FieldType::Utf8,
amount: false,
}
}
pub fn amount(mut self) -> Self {
self.amount = true;
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Domain {
pub id: String,
pub version: String,
}
impl Domain {
pub fn new(id: &str, version: &str) -> Self {
Domain {
id: id.into(),
version: version.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DescribeDoc {
pub abi_version: u32,
pub domain: Domain,
pub input: Vec<Field>,
pub report_schema: u32,
}
impl DescribeDoc {
pub fn new(id: &str, version: &str) -> Self {
DescribeDoc {
abi_version: ABI_VERSION,
domain: Domain {
id: id.into(),
version: version.into(),
},
input: Vec::new(),
report_schema: 1,
}
}
pub fn input(mut self, fields: Vec<Field>) -> Self {
self.input = fields;
self
}
pub fn amount_field(&self) -> Option<&str> {
self.input
.iter()
.find(|f| f.amount)
.map(|f| f.name.as_str())
}
}