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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! RespoNode abstraction

pub(crate) mod component;
pub mod css;
pub(crate) mod dom_change;
pub(crate) mod element;
mod listener;

use std::boxed::Box;
use std::fmt::Display;
use std::rc::Rc;
use std::{collections::HashMap, fmt::Debug};

use cirru_parser::Cirru;
pub use listener::RespoEvent;
pub(crate) use listener::{RespoEventMark, RespoListenerFn};

pub use component::RespoComponent;
pub use element::RespoElement;

use crate::states_tree::{DynEq, RespoStateBranch, RespoUpdateState};

use css::RespoStyle;

pub(crate) use dom_change::RespoCoord;
pub(crate) use dom_change::{ChildDomOp, DomChange};

pub use component::effect::{RespoEffect, RespoEffectType};

/// an `Element` or a `Component`
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RespoNode<T>
where
  T: Debug + Clone,
{
  Component(RespoComponent<T>),
  /// corresponding to DOM elements
  Element(RespoElement<T>),
  Referenced(Rc<RespoNode<T>>),
}

impl<T> From<RespoNode<T>> for Cirru
where
  T: Debug + Clone,
{
  fn from(value: RespoNode<T>) -> Self {
    match value {
      RespoNode::Component(RespoComponent { name, tree, .. }) => {
        Cirru::List(vec![Cirru::Leaf("::Component".into()), Cirru::from(name.as_ref()), (*tree).into()])
      }
      RespoNode::Element(RespoElement { name, children, .. }) => {
        let mut xs = vec![Cirru::from(name.as_ref())];
        for (k, child) in children {
          xs.push(Cirru::List(vec![Cirru::Leaf(k.to_string().into()), child.to_owned().into()]));
        }
        Cirru::List(xs)
      }
      RespoNode::Referenced(cell) => (*cell).to_owned().into(),
    }
  }
}

impl<T> Display for RespoNode<T>
where
  T: Debug + Clone,
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{:?}", self)
  }
}

/// a key for referencing a child node, use a value that can be converted to string
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct RespoIndexKey(String);

impl From<usize> for RespoIndexKey {
  fn from(data: usize) -> Self {
    Self(data.to_string())
  }
}

impl From<&usize> for RespoIndexKey {
  fn from(data: &usize) -> Self {
    Self(data.to_string())
  }
}

impl From<String> for RespoIndexKey {
  fn from(s: String) -> Self {
    Self(s)
  }
}

impl From<&String> for RespoIndexKey {
  fn from(s: &String) -> Self {
    Self(s.to_owned())
  }
}

impl From<&str> for RespoIndexKey {
  fn from(s: &str) -> Self {
    Self(s.to_owned())
  }
}

impl Display for RespoIndexKey {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.0)
  }
}

impl From<RespoIndexKey> for Cirru {
  fn from(k: RespoIndexKey) -> Cirru {
    Cirru::from(k.to_string())
  }
}

impl<T> RespoNode<T>
where
  T: Debug + Clone,
{
  /// create an element node
  pub fn new_tag(name: &str) -> Self {
    Self::Element(RespoElement {
      name: name.into(),
      attrs: HashMap::new(),
      event: HashMap::new(),
      style: RespoStyle::default(),
      children: Vec::new(),
    })
  }
  /// create a new component
  pub fn new_component(name: &str, tree: RespoNode<T>) -> Self {
    Self::Component(RespoComponent {
      name: name.into(),
      effects: Vec::new(),
      tree: Box::new(tree),
    })
  }
  /// wrap with a `Rc<T>` to enable memory reuse and skipping in diff
  pub fn rc(&self) -> Self {
    Self::Referenced(Rc::new(self.to_owned()))
  }
}

pub(crate) type StrDict = HashMap<Rc<str>, String>;

pub(crate) fn str_dict_to_cirrus_dict(dict: &StrDict) -> Cirru {
  let mut xs = vec![];
  for (k, v) in dict {
    xs.push(vec![Cirru::from(k.as_ref()), Cirru::from(v)].into());
  }
  Cirru::List(xs)
}

/// dispatch function passed from root of renderer,
/// call it like `dispatch.run(op)`
#[derive(Clone)]
pub struct DispatchFn<T>(Rc<dyn Fn(T) -> Result<(), String>>)
where
  T: Debug + Clone;

impl<T> Debug for DispatchFn<T>
where
  T: Debug + Clone,
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str("[DispatchFn]")
  }
}

/// guide for actions to be dispatched
/// expecially for how you update states
pub trait RespoAction {
  type Intent: Debug + Clone + PartialEq + Eq;
  /// to provide syntax sugar to dispatch.run_state
  fn build_states_action(cursor: &[Rc<str>], a: Option<RespoStateBranch>) -> Self
  where
    Self: Sized,
  {
    // val is a backup value from DynEq to Json Value
    let val = match &a {
      None => None,
      Some(v) => v.0.as_ref().backup(),
    };
    Self::states_action(RespoUpdateState {
      cursor: cursor.to_vec(),
      data: a,
      backup: val,
    })
  }

  /// builder for intent actions
  fn build_intent_action(op: Self::Intent) -> Self
  where
    Self: Sized,
  {
    todo!("build_intent_action need to be implemented when intent({:?}) is used ", op)
  }

  /// a builder for states change
  fn states_action(a: RespoUpdateState) -> Self;

  /// handle intent seperately since it might contain effects
  fn detect_intent(&self) -> Option<Self::Intent> {
    None
  }
}

impl<T> DispatchFn<T>
where
  T: Debug + Clone + RespoAction,
{
  /// dispatch an action
  pub fn run(&self, op: T) -> Result<(), String> {
    (self.0)(op)
  }
  /// dispatch to update local state
  pub fn run_state<U>(&self, cursor: &[Rc<str>], data: U) -> Result<(), String>
  where
    U: DynEq + ToOwned + Clone + PartialEq + Eq + 'static,
  {
    let a = Rc::new(data);
    (self.0)(T::build_states_action(cursor, Some(RespoStateBranch::new(a))))
  }
  /// alias for dispatching intent
  pub fn run_intent(&self, op: T::Intent) -> Result<(), String> {
    (self.0)(T::build_intent_action(op))
  }
  /// reset state to empty
  pub fn run_empty_state(&self, cursor: &[Rc<str>]) -> Result<(), String> {
    (self.0)(T::build_states_action(cursor, None))
  }
  pub fn new<U>(f: U) -> Self
  where
    U: Fn(T) -> Result<(), String> + 'static,
  {
    Self(Rc::new(f))
  }
}

/// (internal) function to handle event marks at first phase of event handling
#[derive(Clone)]
pub(crate) struct RespoEventMarkFn(Rc<dyn Fn(RespoEventMark) -> Result<(), String>>);

impl Debug for RespoEventMarkFn {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str("[EventMarkFn ...]")
  }
}

impl RespoEventMarkFn {
  pub fn run(&self, e: RespoEventMark) -> Result<(), String> {
    (self.0)(e)
  }
  pub fn new<U>(f: U) -> Self
  where
    U: Fn(RespoEventMark) -> Result<(), String> + 'static,
  {
    Self(Rc::new(f))
  }
}

impl From<Rc<dyn Fn(RespoEventMark) -> Result<(), String>>> for RespoEventMarkFn {
  fn from(f: Rc<dyn Fn(RespoEventMark) -> Result<(), String>>) -> Self {
    Self(f)
  }
}