1use std::fmt::Display;
2
3pub use async_trait::async_trait;
4pub use chrono::{DateTime, Utc};
5
6use crate::value::Map;
7use crate::value::Value;
8
9pub type Error = Box<dyn std::error::Error + Sync + Send>;
10
11pub mod prelude {
12 pub use chrono::{DateTime, Utc};
13
14 pub use crate::value::Deserialize;
15 pub use crate::value::from_reader;
16 pub use crate::value::from_slice;
17 pub use crate::value::from_str;
18 pub use crate::value::from_value;
19 pub use crate::value::json;
20 pub use crate::value::Map;
21 pub use crate::value::Number;
22 pub use crate::value::Serialize;
23 pub use crate::value::to_string;
24 pub use crate::value::to_string_pretty;
25 pub use crate::value::Value;
26
27 pub use super::Action;
28 pub use super::Arg;
29 pub use super::Asset;
30 pub use super::async_trait;
31 pub use super::Chord;
32 pub use super::Context;
33 pub use super::Creator;
34 pub use super::Data;
35 pub use super::Error;
36 pub use super::Id;
37}
38
39pub trait Id: Sync + Send + Display {
40 fn clone(&self) -> Box<dyn Id>;
41}
42
43pub trait Context: Sync + Send {
44 fn data(&self) -> ⤅
45
46 fn data_mut(&mut self) -> &mut Map;
47
48 fn clone(&self) -> Box<dyn Context>;
49}
50
51
52pub trait Data: Sync + Send {
53 fn to_value(&self) -> Value;
54}
55
56impl Data for Value {
57 fn to_value(&self) -> Value {
58 self.clone()
59 }
60}
61
62
63pub trait Frame: Data {
64 fn id(&self) -> &str;
65
66 fn start(&self) -> DateTime<Utc>;
67
68 fn end(&self) -> DateTime<Utc>;
69}
70
71
72pub enum Asset {
73 Value(Value),
74 Data(Box<dyn Data>),
75 Frames(Vec<Box<dyn Frame>>),
76}
77
78impl Asset {
79 pub fn to_value(&self) -> Value {
80 match self {
81 Asset::Value(v) => {
82 v.clone()
83 }
84 Asset::Data(d) => {
85 d.to_value()
86 }
87 Asset::Frames(fs) => {
88 Value::Array(fs.iter().map(|f| f.to_value()).collect())
89 }
90 }
91 }
92}
93
94pub trait Chord: Sync + Send {
95 fn creator(&self, action: &str) -> Option<&dyn Creator>;
96
97 fn render(&self, context: &dyn Context, raw: &Value) -> Result<Value, Error>;
98
99 fn clone(&self) -> Box<dyn Chord>;
100}
101
102pub trait Arg: Sync + Send {
103 fn id(&self) -> &dyn Id;
104
105 fn args(&self) -> Result<Value, Error>;
106
107 fn args_raw(&self) -> &Value;
108
109 fn args_init(&self) -> Option<&Value>;
110
111 fn context(&self) -> &dyn Context;
112
113 fn context_mut(&mut self) -> &mut dyn Context;
114}
115
116#[async_trait]
117pub trait Action: Sync + Send {
118 async fn execute(&self, chord: &dyn Chord, arg: &mut dyn Arg) -> Result<Asset, Error>;
119
120 async fn explain(&self, _chord: &dyn Chord, arg: &dyn Arg) -> Result<Value, Error> {
121 arg.args()
122 }
123}
124
125#[async_trait]
126pub trait Creator: Sync + Send {
127 async fn create(&self, chord: &dyn Chord, arg: &dyn Arg) -> Result<Box<dyn Action>, Error>;
128}