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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use self::rpc::RPCDefinitionData;
use nt_packet::*;
use bytes::{Buf, BufMut, BytesMut};
use leb128::{LEB128Read, LEB128Write};

use std::convert::AsRef;

pub mod rpc;

/// Struct containing the data associated with an entry.
/// Used interally to store entries
#[derive(Clone, Debug, PartialEq)]
pub struct EntryData {
    /// The name associated with this entry
    pub name: String,
    /// The flags associated with this entry
    pub flags: u8,
    /// The value associated with this entry
    pub value: EntryValue,
    /// The most recent sequence number associated with this entry
    pub seqnum: u16,
}

impl EntryData {
    /// Creates a new [`EntryData`] with the given parameters, and a sequence number of 1
    pub fn new(name: String, flags: u8, value: EntryValue) -> EntryData {
        Self::new_with_seqnum(name, flags, value, 1)
    }

    #[doc(hidden)]
    pub(crate) fn new_with_seqnum(name: String, flags: u8, value: EntryValue, seqnum: u16) -> EntryData {
        EntryData {
            name,
            flags,
            value,
            seqnum
        }
    }
}

/// Corresponds to the type tag that NetworkTables presents prior to the corresponding [`EntryValue`]
#[derive(Debug, PartialEq, Clone)]
pub enum EntryType {
    /// Represents a boolean entry value
    Boolean,
    /// Represents a double entry value
    Double,
    /// Represents a string entry value
    String,
    /// Represents a raw data entry value
    RawData,
    /// Represents a boolean array entry value
    BooleanArray,
    /// Represents a double array entry value
    DoubleArray,
    /// Represents a string array entry value
    StringArray,
    /// Represents an RPC definition entry value
    RPCDef,
}

/// Enum representing the different values that NetworkTables supports
#[derive(Debug, Clone, PartialEq)]
pub enum EntryValue {
    /// An entry value containing a single boolean
    Boolean(bool),
    /// An entry value containing a single double
    Double(f64),
    /// An entry value containing a single string
    String(String),
    /// An entry value containing raw data
    RawData(Vec<u8>),
    /// An entry value containing a boolean array
    BooleanArray(Vec<bool>),
    /// An entry value containing a double array
    DoubleArray(Vec<f64>),
    /// An entry value containing a string array
    StringArray(Vec<String>),
    /// An entry value containing definition data for a RPC
    RPCDef(RPCDefinitionData),
}

impl ClientMessage for EntryType {
    fn encode(&self, buf: &mut BytesMut) {
        let byte = match self {
            &EntryType::Boolean => 0x00,
            &EntryType::Double => 0x01,
            &EntryType::String => 0x02,
            &EntryType::RawData => 0x03,
            &EntryType::BooleanArray => 0x10,
            &EntryType::DoubleArray => 0x11,
            &EntryType::StringArray => 0x12,
            _ => panic!()
        };

        buf.put_u8(byte);
    }
}

impl ServerMessage for EntryType {
    fn decode(buf: &mut Buf) -> (Option<Self>, usize) {
        let byte = buf.get_u8();
        let this = match byte {
            0x00 => Some(EntryType::Boolean),
            0x01 => Some(EntryType::Double),
            0x02 => Some(EntryType::String),
            0x03 => Some(EntryType::RawData),
            0x10 => Some(EntryType::BooleanArray),
            0x11 => Some(EntryType::DoubleArray),
            0x12 => Some(EntryType::StringArray),
            0x20 => Some(EntryType::RPCDef),
            _ => None
        };
        (this, 1)
    }
}

impl EntryType {
    /// Deserializes an [`EntryValue`] of type `self` from the given `buf`
    pub fn get_entry(&self, mut buf: &mut Buf) -> (EntryValue, usize) {
        match self {
            &EntryType::Boolean => (EntryValue::Boolean(buf.get_u8() == 1), 1),
            &EntryType::Double => (EntryValue::Double(buf.get_f64_be()), 8),
            &EntryType::String => {
                let (value, bytes_read) = String::decode(buf);
                (EntryValue::String(value.unwrap()), bytes_read)
            }
            &EntryType::RawData => {
                let (len, size) = buf.read_unsigned().unwrap();
                let len = len as usize;
                let mut data = vec![0u8; len];
                buf.copy_to_slice(&mut data[..]);
                (EntryValue::RawData(data), len + size)
            }
            &EntryType::BooleanArray => {
                let mut bytes_read = 0;
                let len = buf.get_u8() as usize;
                bytes_read += 1;
                let mut arr = vec![false; len];

                for i in 0..len {
                    let byte = buf.get_u8();
                    bytes_read += 1;
                    arr[i] = byte == 1;
                }

                (EntryValue::BooleanArray(arr), bytes_read)
            }
            &EntryType::DoubleArray => {
                let mut bytes_read = 0;
                let len = buf.get_u8() as usize;
                bytes_read += 1;
                let mut arr = vec![0f64; len];

                for i in 0..len {
                    let val = buf.get_f64_be();
                    bytes_read += 8;
                    arr[i] = val;
                }

                (EntryValue::DoubleArray(arr), bytes_read)
            }
            &EntryType::StringArray => {
                let mut bytes_read = 0;

                let len = buf.get_u8() as usize;
                bytes_read += 1;
                let mut arr = Vec::with_capacity(len);

                for i in 0..len {
                    let (val, bytes) = String::decode(buf);
                    bytes_read += bytes;
                    arr[i] = val.unwrap();
                }

                (EntryValue::StringArray(arr), bytes_read)
            }
            &EntryType::RPCDef => {
                let (def, bytes) = RPCDefinitionData::decode(buf);
                (EntryValue::RPCDef(def.unwrap()), bytes)
            }
        }
    }
}

impl ClientMessage for EntryValue {
    fn encode(&self, buf: &mut BytesMut) {
        match &self {
            &EntryValue::Boolean(val) => buf.put_u8(*val as u8),
            &EntryValue::Double(val) => buf.put_f64_be(*val),
            &EntryValue::String(val) => val.encode(buf),
            &EntryValue::RawData(val) => {
                buf.write_unsigned(val.len() as u64).unwrap();
                buf.put_slice(&val[..]);
            }
            &EntryValue::BooleanArray(val) => {
                buf.write_unsigned(val.len() as u64).unwrap();

                for b in val {
                    buf.put_u8(*b as u8)
                }
            }
            &EntryValue::DoubleArray(val) => {
                buf.write_unsigned(val.len() as u64).unwrap();

                for d in val {
                    buf.put_f64_be(*d);
                }
            }
            &EntryValue::StringArray(val) => {
                buf.write_unsigned(val.len() as u64).unwrap();

                for s in val {
                    s.encode(buf);
                }
            }
            _ => panic!()
        }
    }
}

impl EntryValue {
    /// Returns the [`EntryType`] corresponding to the variant of [`self`]
    pub fn entry_type(&self) -> EntryType {
        match self {
            EntryValue::Boolean(_) => EntryType::Boolean,
            EntryValue::Double(_) => EntryType::Double,
            EntryValue::String(_) => EntryType::String,
            EntryValue::RawData(_) => EntryType::RawData,
            EntryValue::BooleanArray(_) => EntryType::BooleanArray,
            EntryValue::DoubleArray(_) => EntryType::DoubleArray,
            EntryValue::StringArray(_) => EntryType::StringArray,
            EntryValue::RPCDef(_) => EntryType::RPCDef
        }
    }
}

impl<T> From<T> for EntryType
    where T: AsRef<str>
{
    fn from(val: T) -> Self {
        info!("{}", val.as_ref());
        match val.as_ref().to_lowercase().as_str() {
            "string" => EntryType::String,
            "bool" => EntryType::Boolean,
            "double" => EntryType::Double,
            _ => unimplemented!()
        }
    }
}