use crate::{
aux::Owned,
ext::{Gtin, Money, Natural},
};
#[derive(Owned!)]
pub struct Script(pub Vec<Stmt>);
#[derive(Owned!)]
pub enum Stmt {
Create(Create),
Transfer(Transfer),
Analyze(Analyze),
}
#[derive(Owned!)]
pub struct Create {
pub who: Actor,
}
#[derive(Owned!)]
pub enum Transfer {
Pay(Pay),
Deliver(Deliver),
}
#[derive(Owned!)]
pub enum Analyze {
Balance(Balance),
}
#[derive(Owned!)]
pub struct Pay {
pub amount: Money,
pub who: Dir,
}
#[derive(Owned!)]
pub struct Deliver {
pub what: Product,
pub who: Dir,
pub price: Option<Money>,
pub split: Option<Ratio>,
}
#[derive(Owned!)]
pub struct Balance {
pub between: Dir,
}
#[derive(Owned!)]
pub enum Actor {
Entity(Entity),
Object(Object),
Concept(Concept),
}
#[derive(Owned!)]
pub struct Entity {
pub name: Ident,
}
#[derive(Owned!)]
pub struct Object {
pub name: Ident,
pub parent: Option<Ident>,
}
#[derive(Owned!)]
pub struct Concept {
pub name: Ident,
pub default_price: Option<Money>,
pub gtin: Option<Gtin>,
}
#[derive(Owned!)]
pub enum Product {
Name(Ident),
Id(Gtin),
}
#[derive(Owned!)]
pub struct Dir {
pub source: Ident,
pub target: Ident,
}
#[derive(Owned!)]
pub struct Ratio {
pub left: Natural,
pub right: Natural,
}
#[derive(Owned!)]
pub struct Ident(String);
impl Ident {
pub(super) fn new(ident: impl AsRef<str>) -> Self {
Self(ident.as_ref().to_string())
}
pub fn get(&self) -> &str {
&self.0
}
}
impl From<Ident> for String {
fn from(id: Ident) -> Self {
id.0
}
}
impl AsRef<str> for Ident {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}