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
pub mod jmbl_view;
pub mod value;

use std::convert::TryInto;

use crate::{
    list::list_view::ListView, map::map_view::MapView, object::AnyObjectState, ops::ObjID,
    text::text_view::TextView,
};

use jmbl_view::JMBLViewRef;
use value::ValueError;

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ObjView {
    List(ListView),
    Text(TextView),
    Map(MapView),
}

impl ObjView {
    pub(crate) fn new_for(obj: &AnyObjectState, obj_id: ObjID, ctx: JMBLViewRef) -> Self {
        match obj {
            AnyObjectState::List(_) => ObjView::List(ListView::new(obj_id, ctx)),
            AnyObjectState::Text(_) => ObjView::Text(TextView::new(obj_id, ctx)),
            AnyObjectState::Map(_) => ObjView::Map(MapView::new(obj_id, ctx)),
        }
    }

    pub(crate) fn get_obj_id(&self) -> ObjID {
        match self {
            ObjView::List(ref list_view) => list_view.obj_id,
            _ => todo!("Not yet implemented"),
        }
    }

    pub fn to_litl(&self) -> litl::Val {
        match self {
            ObjView::List(list_view) => list_view.to_litl(),
            ObjView::Text(text_view) => text_view.to_litl(),
            ObjView::Map(map_view) => map_view.to_litl(),
        }
    }

    pub fn if_map(&self) -> Result<&MapView, ValueError> {
        self.try_into()
    }

    pub fn if_list(&self) -> Result<&ListView, ValueError> {
        self.try_into()
    }

    pub fn if_text(&self) -> Result<&TextView, ValueError> {
        self.try_into()
    }

    pub fn if_map_mut(&mut self) -> Result<&mut MapView, ValueError> {
        self.try_into()
    }

    pub fn if_list_mut(&mut self) -> Result<&mut ListView, ValueError> {
        self.try_into()
    }

    pub fn if_text_mut(&mut self) -> Result<&mut TextView, ValueError> {
        self.try_into()
    }
}

impl TryInto<ListView> for ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<ListView, Self::Error> {
        match self {
            ObjView::List(list_view) => Ok(list_view),
            _ => Err(ValueError::ExpectedList(self.clone())),
        }
    }
}

impl TryInto<MapView> for ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<MapView, Self::Error> {
        match self {
            ObjView::Map(map_view) => Ok(map_view),
            _ => Err(ValueError::ExpectedMap(self.clone())),
        }
    }
}

impl TryInto<TextView> for ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<TextView, Self::Error> {
        match self {
            ObjView::Text(text_view) => Ok(text_view),
            _ => Err(ValueError::ExpectedText(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a ListView> for &'a ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a ListView, Self::Error> {
        match self {
            ObjView::List(list_view) => Ok(list_view),
            _ => Err(ValueError::ExpectedList(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a MapView> for &'a ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a MapView, Self::Error> {
        match self {
            ObjView::Map(map_view) => Ok(map_view),
            _ => Err(ValueError::ExpectedMap(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a TextView> for &'a ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a TextView, Self::Error> {
        match self {
            ObjView::Text(text_view) => Ok(text_view),
            _ => Err(ValueError::ExpectedText(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a mut ListView> for &'a mut ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a mut ListView, Self::Error> {
        match self {
            ObjView::List(list_view) => Ok(list_view),
            _ => Err(ValueError::ExpectedList(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a mut MapView> for &'a mut ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a mut MapView, Self::Error> {
        match self {
            ObjView::Map(map_view) => Ok(map_view),
            _ => Err(ValueError::ExpectedMap(self.clone())),
        }
    }
}

impl<'a> TryInto<&'a mut TextView> for &'a mut ObjView {
    type Error = ValueError;

    fn try_into(self) -> Result<&'a mut TextView, Self::Error> {
        match self {
            ObjView::Text(text_view) => Ok(text_view),
            _ => Err(ValueError::ExpectedText(self.clone())),
        }
    }
}