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,
ObjView::Map(ref map_view) => map_view.obj_id,
ObjView::Text(ref text_view) => text_view.obj_id,
}
}
pub fn to_litl(&self) -> litl::Val {
match self {
ObjView::List(list_view) => list_view.val_to_litl(),
ObjView::Text(text_view) => text_view.val_to_litl(),
ObjView::Map(map_view) => map_view.val_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())),
}
}
}