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
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::sync::Arc;

/// The Value to be serialized.
#[derive(Debug, PartialEq)]
pub enum KVal {
    Null,
    Bit(bool),
    Bool(bool),
    Byte(i8),
    Str16(String),
    Int(i64),
    Float(f32),
    Str256(String),
    Double(f64),
    Decimal(Decimal),
    BigStr(String),
    Binary(Vec<u8>),
    BinaryArc(Arc<Vec<u8>>),
    Binary8(Vec<u64>),
    Binary4(Vec<u32>),
    Binary2(Vec<u16>),
    Undefined(u8),
}


/// Decimal represents 40 digits with dot.
/// It's supposed to be used for decimal strings which can be converted to without errors.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Decimal{
    /// The integer part.
    pub int : i128,
    /// The number is an integer when dot == 0. When dot == 1, the dot slides to left.
    /// If int == 128, it's 12.8. If int == 100, it's 10.0
    /// When dot == 2, if int == 128, it's 1.28. If int == 100, it's 1.00
    /// ...
    pub dot : u8,
}

impl Decimal{
    /// get the nearest f64
    pub fn to_f64(&self) -> f64 {
        crate::enc_dec::decimal_lib::to_f64(self.int, self.dot)
    }
    /// get the edecimal string
    pub fn to_string(&self) -> String{
        crate::enc_dec::decimal_lib::to_string(self.int, self.dot)
    }
    /// create the value
    pub fn new(int : i128, dot : u8) -> Decimal{
        Decimal{ int, dot }
    }
}

impl KVal {
    ///bit,byte,int,float,double can be converted to f64
    pub fn as_f64(&self) -> Option<f64>{
        use KVal::*;
        match self{
            Bit(b) => Some(*b as isize as f64),
            Byte(b) => Some(*b as f64),
            Int(i) => Some(*i as f64),
            Float(f) => Some(*f as f64),
            Double(f) => Some(*f),
            _ => None,
        }
    }

    ///bit,byte,int,float,double can be converted to f32
    pub fn as_f32(&self) -> Option<f32>{
        use KVal::*;
        match self{
            Bit(b) => Some(*b as isize as f32),
            Byte(b) => Some(*b as f32),
            Int(i) => Some(*i as f32),
            Float(f) => Some(*f),
            Double(f) => Some(*f as f32), //???
            _ => None,
        }
    }

    ///bit, byte, int can be converted to i64
    pub fn as_i64(&self) -> Option<i64>{
        use KVal::*;
        match self {
            Bit(b) => Some(*b as i64),
            Byte(b) => Some(*b as i64),
            Int(i) => Some(*i as i64),
            _ => None,
        }
    }

    ///str16, str256, BigStr, Bit <- Empty String
    pub fn as_string(&self) -> Option<String>{
        use KVal::*;
        match self{
            Bit(_b) => Some(String::new()),
            Str16(s) => Some(s.to_string()),
            Str256(s) => Some(s.to_string()),
            BigStr(s) => Some(s.to_string()),
            _ => None,
        }
    }

    /// Bool, Bit can be converted to bool
    pub fn as_bool(&self) -> Option<bool>{
        match self{
            KVal::Bool(b) => Some(*b),
            KVal::Bit(b) => Some(*b),
            _ => None,
        }
    }

    /// return Some if it's null
    pub fn as_null(&self) -> Option<()>{
        match self{
            KVal::Null => Some(()),
            _ => None,
        }
    }

    /// if it's == undefined0, return Some(0).
    /// if it's == undefined1, return Some(1).
    /// ...
    pub fn as_undefined(&self) -> Option<u8>{
        match self{
            KVal::Undefined(u) => Some(*u),
            _ => None,
        }
    }

    /// return Some(Decimal) if it's Decimal
    pub fn as_decimal(&self) -> Option<Decimal>{
        match self{
            KVal::Decimal(d) => Some(*d),
            _ => None,
        }
    }

    /// Get the string expression of this value.
    pub fn to_string(&self) -> String{
        crate::string_compaction::to_string(self)
    }
}