tablestg 0.4.6

Storage for database tables
Documentation
use crate::*;

#[derive(Clone, Hash, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)]
/// Generic value.
pub enum Value {
    Empty,
    Int(i64),
    String(LString),
    Binary(LVec<u8>),
    List(LVec<Value>),
    Enum(usize, LBox<Value>),
    VarVal(u64, u64),
    IList(LVec<i64>),
}

impl Value {
    // Get mut reference to List LVec ( Value must be List )
    pub fn list_mut(&mut self) -> &mut LVec<Value> {
        match self {
            Value::List(list) => list,
            _ => panic!(),
        }
    }

    // Get reference to List LVec ( Value must be List )
    pub fn list(&self) -> &LVec<Value> {
        match self {
            Value::List(list) => list,
            _ => panic!(),
        }
    }

    pub fn en(&self) -> (&usize, &Value) {
        match self {
            Value::Enum(tag, bx) => (tag, &**bx),
            _ => panic!(),
        }
    }

    // Get reference to IList LVec ( Value must be IList )
    pub fn ilist(&self) -> &LVec<i64> {
        match self {
            Value::IList(list) => list,
            _ => panic!(),
        }
    }

    // Get mut reference to IList LVec ( Value must be IList )
    pub fn ilist_mut(&mut self) -> &mut LVec<i64> {
        match self {
            Value::IList(list) => list,
            _ => panic!(),
        }
    }

    // Get reference to LString ( Value must be String )
    pub fn string(&self) -> &LString {
        match self {
            Value::String(s) => s,
            _ => panic!(),
        }
    }

    // Get reference to LVec ( Value must be Binary )
    pub fn binary(&self) -> &LVec<u8> {
        match self {
            Value::Binary(b) => b,
            _ => panic!(),
        }
    }

    pub fn int(&self) -> i64 {
        match self {
            Value::Int(x) => *x,
            _ => panic!(),
        }
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        postcard::to_stdvec(self).unwrap()
    }
    pub fn from_bytes(b: &[u8]) -> Self {
        postcard::from_bytes(b).unwrap()
    }
}

#[test]
fn test_value() {
    /*
        use pstd::veca;
        let v1 = Value::List(veca![
            Value::String(LString::from("George Barwood")),
            Value::String(LString::from("george.barwood@gmail.com")),
            Value::String(LString::from("33 Sandpipe Close, GL2 4LZ")),
            Value::Int(-47),
            Value::Int(99),
            Value::VarVal(100, 20),
            Value::IList(veca![1, 2, 3, 4]),
        ]);
        let bytes = v1.to_bytes();
        println!("len={} bytes={:?}", bytes.len(), bytes);

        let v2 = Value::from_bytes(&bytes);
        println!("v2={:?}", v2);
    */
}