roussillon_type_system/value/
concept.rs

1use std::cell::RefCell;
2use std::fmt::{Debug, Formatter};
3use std::rc::Rc;
4
5use crate::types::concept::Type;
6use crate::value::error::{CanTypeError, TypeError};
7
8pub trait DataValue {
9    fn data_type(&self) -> Type;
10
11    fn raw(&self) -> Vec<u8>;
12
13    fn set(&mut self, raw: &[u8]);
14
15    fn validate_type(&self, expected_type: &Type) -> CanTypeError {
16        if self.data_type().typename() == expected_type.typename() {
17            Ok(())
18        } else {
19            Err(TypeError::InvalidType { expected: expected_type.clone(), provided: self.data_type() })
20        }
21    }
22}
23
24impl Debug for dyn DataValue {
25    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26        write!(f, "{:?}{:x?}", self.data_type(), self.raw())
27    }
28}
29
30pub type ValueCell = Rc<RefCell<dyn DataValue>>;
31
32pub trait GetDataValue<T>: DataValue {
33    fn get(&self) -> T;
34    fn from_raw(raw: &[u8]) -> Self;
35}