small_db/
field.rs

1use std::fmt::{self, Debug};
2
3use crate::{
4    io::{Condensable, SmallReader, Vaporizable},
5    Op,
6};
7
8#[derive(Copy, Clone, PartialEq, Debug)]
9pub enum Type {
10    INT,
11}
12
13pub fn get_type_length(t: Type) -> usize {
14    match t {
15        Type::INT => 4,
16    }
17}
18
19#[derive(PartialEq, Debug, Clone)]
20pub struct FieldItem {
21    pub field_type: Type,
22    pub field_name: String,
23}
24
25#[derive(Copy, Clone, PartialEq, Eq, Ord, Debug, PartialOrd)]
26pub struct IntField {
27    pub value: i32,
28}
29
30impl IntField {
31    pub fn new(v: i32) -> IntField {
32        IntField { value: v }
33    }
34
35    pub fn len(&self) -> usize {
36        4
37    }
38
39    pub fn compare(&self, op: Op, field: IntField) -> bool {
40        match op {
41            crate::Op::Equals => self.value == field.value,
42            crate::Op::GreaterThan => self.value > field.value,
43            crate::Op::LessThan => self.value < field.value,
44            crate::Op::LessThanOrEq => self.value <= field.value,
45            crate::Op::GreaterThanOrEq => self.value >= field.value,
46            crate::Op::Like => todo!(),
47            crate::Op::NotEquals => self.value != field.value,
48        }
49    }
50}
51
52impl Condensable for IntField {
53    fn to_bytes(&self) -> Vec<u8> {
54        self.value.to_be_bytes().to_vec()
55    }
56}
57
58impl Vaporizable for IntField {
59    fn read_from(reader: &mut SmallReader) -> Self {
60        let data = reader.read_exact(4);
61        IntField {
62            value: i32::from_be_bytes([
63                data[0], data[1], data[2], data[3],
64            ]),
65        }
66    }
67}
68
69impl fmt::Display for IntField {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        write!(f, "{}", self.value)
72    }
73}