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
use std::fmt::Display;

pub use async_trait::async_trait;
pub use chrono::{DateTime, Utc};

use crate::value::Map;
use crate::value::Value;

pub type Error = Box<dyn std::error::Error + Sync + Send>;

pub mod prelude {
    pub use chrono::{DateTime, Utc};

    pub use crate::value::Deserialize;
    pub use crate::value::from_reader;
    pub use crate::value::from_slice;
    pub use crate::value::from_str;
    pub use crate::value::from_value;
    pub use crate::value::json;
    pub use crate::value::Map;
    pub use crate::value::Number;
    pub use crate::value::Serialize;
    pub use crate::value::to_string;
    pub use crate::value::to_string_pretty;
    pub use crate::value::Value;

    pub use super::Action;
    pub use super::Arg;
    pub use super::Asset;
    pub use super::async_trait;
    pub use super::Chord;
    pub use super::Context;
    pub use super::Creator;
    pub use super::Data;
    pub use super::Error;
    pub use super::Id;
}

pub trait Id: Sync + Send + Display {
    fn clone(&self) -> Box<dyn Id>;
}

pub trait Context: Sync + Send {
    fn data(&self) -> &Map;

    fn data_mut(&mut self) -> &mut Map;

    fn clone(&self) -> Box<dyn Context>;
}


pub trait Data: Sync + Send {
    fn to_value(&self) -> Value;
}

impl Data for Value {
    fn to_value(&self) -> Value {
        self.clone()
    }
}


pub trait Frame: Data {
    fn id(&self) -> &str;

    fn start(&self) -> DateTime<Utc>;

    fn end(&self) -> DateTime<Utc>;
}


pub enum Asset {
    Value(Value),
    Data(Box<dyn Data>),
    Frames(Vec<Box<dyn Frame>>),
}

impl Asset {
    pub fn to_value(&self) -> Value {
        match self {
            Asset::Value(v) => {
                v.clone()
            }
            Asset::Data(d) => {
                d.to_value()
            }
            Asset::Frames(fs) => {
                Value::Array(fs.iter().map(|f| f.to_value()).collect())
            }
        }
    }
}

pub trait Chord: Sync + Send {
    fn creator(&self, action: &str) -> Option<&dyn Creator>;

    fn render(&self, context: &dyn Context, raw: &Value) -> Result<Value, Error>;

    fn clone(&self) -> Box<dyn Chord>;
}

pub trait Arg: Sync + Send {
    fn id(&self) -> &dyn Id;

    fn args(&self) -> Result<Value, Error>;

    fn args_raw(&self) -> &Value;

    fn args_init(&self) -> Option<&Value>;

    fn context(&self) -> &dyn Context;

    fn context_mut(&mut self) -> &mut dyn Context;
}

#[async_trait]
pub trait Action: Sync + Send {
    async fn execute(&self, chord: &dyn Chord, arg: &mut dyn Arg) -> Result<Asset, Error>;

    async fn explain(&self, _chord: &dyn Chord, arg: &dyn Arg) -> Result<Value, Error> {
        arg.args()
    }
}

#[async_trait]
pub trait Creator: Sync + Send {
    async fn create(&self, chord: &dyn Chord, arg: &dyn Arg) -> Result<Box<dyn Action>, Error>;
}