1pub mod jmbl_view;
2pub mod value;
3
4use std::convert::TryInto;
5
6use crate::{
7 list::list_view::ListView, map::map_view::{MapView}, object::AnyObjectState, ops::ObjID,
8 text::text_view::TextView,
9};
10
11use jmbl_view::JMBLViewRef;
12use value::ValueError;
13
14#[derive(Debug, PartialEq, Eq, Hash, Clone)]
15pub enum ObjView {
16 List(ListView),
17 Text(TextView),
18 Map(MapView),
19}
20
21impl ObjView {
22 pub(crate) fn new_for(obj: &AnyObjectState, obj_id: ObjID, ctx: JMBLViewRef) -> Self {
23 match obj {
24 AnyObjectState::List(_) => ObjView::List(ListView::new(obj_id, ctx)),
25 AnyObjectState::Text(_) => ObjView::Text(TextView::new(obj_id, ctx)),
26 AnyObjectState::Map(_) => ObjView::Map(MapView::new(obj_id, ctx)),
27 }
28 }
29
30 pub(crate) fn get_obj_id(&self) -> ObjID {
31 match self {
32 ObjView::List(ref list_view) => list_view.obj_id,
33 ObjView::Map(ref map_view) => map_view.obj_id,
34 ObjView::Text(ref text_view) => text_view.obj_id,
35 }
36 }
37
38 pub fn to_litl(&self) -> litl::Val {
39 match self {
40 ObjView::List(list_view) => list_view.val_to_litl(),
41 ObjView::Text(text_view) => text_view.val_to_litl(),
42 ObjView::Map(map_view) => map_view.val_to_litl(),
43 }
44 }
45
46 pub fn if_map(&self) -> Result<&MapView, ValueError> {
47 self.try_into()
48 }
49
50 pub fn if_list(&self) -> Result<&ListView, ValueError> {
51 self.try_into()
52 }
53
54 pub fn if_text(&self) -> Result<&TextView, ValueError> {
55 self.try_into()
56 }
57
58 pub fn if_map_mut(&mut self) -> Result<&mut MapView, ValueError> {
59 self.try_into()
60 }
61
62 pub fn if_list_mut(&mut self) -> Result<&mut ListView, ValueError> {
63 self.try_into()
64 }
65
66 pub fn if_text_mut(&mut self) -> Result<&mut TextView, ValueError> {
67 self.try_into()
68 }
69}
70
71impl TryInto<ListView> for ObjView {
72 type Error = ValueError;
73
74 fn try_into(self) -> Result<ListView, Self::Error> {
75 match self {
76 ObjView::List(list_view) => Ok(list_view),
77 _ => Err(ValueError::ExpectedList(self.clone())),
78 }
79 }
80}
81
82impl TryInto<MapView> for ObjView {
83 type Error = ValueError;
84
85 fn try_into(self) -> Result<MapView, Self::Error> {
86 match self {
87 ObjView::Map(map_view) => Ok(map_view),
88 _ => Err(ValueError::ExpectedMap(self.clone())),
89 }
90 }
91}
92
93impl TryInto<TextView> for ObjView {
94 type Error = ValueError;
95
96 fn try_into(self) -> Result<TextView, Self::Error> {
97 match self {
98 ObjView::Text(text_view) => Ok(text_view),
99 _ => Err(ValueError::ExpectedText(self.clone())),
100 }
101 }
102}
103
104impl<'a> TryInto<&'a ListView> for &'a ObjView {
105 type Error = ValueError;
106
107 fn try_into(self) -> Result<&'a ListView, Self::Error> {
108 match self {
109 ObjView::List(list_view) => Ok(list_view),
110 _ => Err(ValueError::ExpectedList(self.clone())),
111 }
112 }
113}
114
115impl<'a> TryInto<&'a MapView> for &'a ObjView {
116 type Error = ValueError;
117
118 fn try_into(self) -> Result<&'a MapView, Self::Error> {
119 match self {
120 ObjView::Map(map_view) => Ok(map_view),
121 _ => Err(ValueError::ExpectedMap(self.clone())),
122 }
123 }
124}
125
126impl<'a> TryInto<&'a TextView> for &'a ObjView {
127 type Error = ValueError;
128
129 fn try_into(self) -> Result<&'a TextView, Self::Error> {
130 match self {
131 ObjView::Text(text_view) => Ok(text_view),
132 _ => Err(ValueError::ExpectedText(self.clone())),
133 }
134 }
135}
136
137impl<'a> TryInto<&'a mut ListView> for &'a mut ObjView {
138 type Error = ValueError;
139
140 fn try_into(self) -> Result<&'a mut ListView, Self::Error> {
141 match self {
142 ObjView::List(list_view) => Ok(list_view),
143 _ => Err(ValueError::ExpectedList(self.clone())),
144 }
145 }
146}
147
148impl<'a> TryInto<&'a mut MapView> for &'a mut ObjView {
149 type Error = ValueError;
150
151 fn try_into(self) -> Result<&'a mut MapView, Self::Error> {
152 match self {
153 ObjView::Map(map_view) => Ok(map_view),
154 _ => Err(ValueError::ExpectedMap(self.clone())),
155 }
156 }
157}
158
159impl<'a> TryInto<&'a mut TextView> for &'a mut ObjView {
160 type Error = ValueError;
161
162 fn try_into(self) -> Result<&'a mut TextView, Self::Error> {
163 match self {
164 ObjView::Text(text_view) => Ok(text_view),
165 _ => Err(ValueError::ExpectedText(self.clone())),
166 }
167 }
168}