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
use std::cell::RefCell;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;

use crate::types::concept::Type;
use crate::value::error::{CanTypeError, TypeError};

pub trait DataValue {
    fn data_type(&self) -> Type;

    fn raw(&self) -> Vec<u8>;

    fn set(&mut self, raw: &[u8]);

    fn validate_type(&self, expected_type: &Type) -> CanTypeError {
        if self.data_type().typename() == expected_type.typename() {
            Ok(())
        } else {
            Err(TypeError::InvalidType { expected: expected_type.clone(), provided: self.data_type() })
        }
    }
}

impl Debug for dyn DataValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}{:x?}", self.data_type(), self.raw())
    }
}

pub type ValueCell = Rc<RefCell<dyn DataValue>>;