vertigo/driver_module/js_value/
js_json_context.rs1use std::rc::Rc;
2
3#[derive(Debug)]
4struct JsJsonContextInner {
5 parent: Option<Rc<JsJsonContextInner>>,
6 current: String,
7}
8
9#[derive(Clone, Debug)]
10pub struct JsJsonContext {
11 inner: Rc<JsJsonContextInner>,
12}
13
14impl JsJsonContext {
15 pub fn new(current: impl Into<String>) -> JsJsonContext {
16 Self {
17 inner: Rc::new(JsJsonContextInner {
18 parent: None,
19 current: current.into(),
20 }),
21 }
22 }
23
24 pub fn add(&self, child: impl ToString) -> JsJsonContext {
25 Self {
26 inner: Rc::new(JsJsonContextInner {
27 parent: Some(self.inner.clone()),
28 current: child.to_string(),
29 }),
30 }
31 }
32
33 pub fn convert_to_string(&self) -> String {
34 let mut path = Vec::new();
35 let mut current = self.inner.clone();
36
37 loop {
38 path.push(current.current.clone());
39
40 let Some(parent) = current.parent.clone() else {
41 return path.into_iter().rev().collect::<Vec<_>>().join(" -> ");
42 };
43
44 current = parent;
45 }
46 }
47}
48
49impl std::fmt::Display for JsJsonContext {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 f.write_str(&self.convert_to_string())
52 }
53}
54
55impl std::error::Error for JsJsonContext {}