pancake_db_core/primitives/
bools.rs1use pancake_db_idl::dml::field_value::Value;
2use pancake_db_idl::dtype::DataType;
3
4use crate::compression::Codec;
5use crate::compression::q_codec::BoolQCodec;
6use crate::compression::Q_COMPRESS;
7use crate::errors::{CoreError, CoreResult};
8use crate::primitives::{Atom, Primitive};
9
10impl Atom for bool {
11 const BYTE_SIZE: usize = 1;
12
13 fn to_bytes(&self) -> Vec<u8> {
14 vec![*self as u8]
15 }
16
17 fn try_from_bytes(bytes: &[u8]) -> CoreResult<bool> {
18 let byte = bytes[0];
19 if byte == 0 {
20 Ok(false)
21 } else if byte == 1 {
22 Ok(true)
23 } else {
24 Err(CoreError::corrupt(&format!("unable to decode bit from byte {}", byte)))
25 }
26 }
27}
28
29impl Primitive for bool {
30 type A = Self;
31 const DTYPE: DataType = DataType::Bool;
32
33 const IS_ATOMIC: bool = true;
34
35 fn to_value(&self) -> Value {
36 Value::BoolVal(*self)
37 }
38
39 fn try_from_value(v: &Value) -> CoreResult<bool> {
40 match v {
41 Value::BoolVal(res) => Ok(*res),
42 _ => Err(CoreError::invalid("cannot read bool from value")),
43 }
44 }
45
46 fn to_atoms(&self) -> Vec<Self> {
47 vec![*self]
48 }
49
50 fn try_from_atoms(atoms: &[Self]) -> CoreResult<Self> {
51 Ok(atoms[0])
52 }
53
54 fn new_codec(codec: &str) -> Option<Box<dyn Codec<P=Self>>> {
55 if codec == Q_COMPRESS {
56 Some(Box::new(BoolQCodec {}))
57 } else {
58 None
59 }
60 }
61}
62