[WIP] Version Parsing
This commit is contained in:
parent
8337fa3d8f
commit
907b9f04db
9 changed files with 133 additions and 40 deletions
|
|
@ -4,7 +4,7 @@ use std::io::{Cursor, Read};
|
|||
|
||||
use crate::data_value::DataValue;
|
||||
use crate::rand_u32;
|
||||
use mtp_type_map::{CommunicationTypeId, DataTypeId};
|
||||
use mtp_type_map::{CommunicationType, CommunicationTypeId, DataType, DataTypeId, TypeMap};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CommunicationValue {
|
||||
|
|
@ -13,6 +13,7 @@ pub struct CommunicationValue {
|
|||
sender: u64,
|
||||
receiver: u64,
|
||||
data: BTreeMap<DataTypeId, DataValue>,
|
||||
type_map: Option<TypeMap>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -24,9 +25,27 @@ impl CommunicationValue {
|
|||
sender: 0,
|
||||
receiver: 0,
|
||||
data: BTreeMap::new(),
|
||||
type_map: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_comm(comm_type: CommunicationType, tm: &TypeMap) -> Self {
|
||||
let id = comm_type.to_id(tm);
|
||||
Self {
|
||||
id: rand_u32(),
|
||||
comm_type: id,
|
||||
sender: 0,
|
||||
receiver: 0,
|
||||
data: BTreeMap::new(),
|
||||
type_map: Some(tm.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_comm_default(comm_type: CommunicationType) -> Self {
|
||||
let tm = TypeMap::latest();
|
||||
Self::from_comm(comm_type, &tm)
|
||||
}
|
||||
|
||||
pub fn with_id(mut self, p0: u32) -> Self {
|
||||
self.id = p0;
|
||||
self
|
||||
|
|
@ -67,6 +86,20 @@ impl CommunicationValue {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn add_typed(mut self, data: DataType, tm: &TypeMap, value: DataValue) -> Self {
|
||||
self.data.insert(data.to_id(tm), value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_typed_default(mut self, data: DataType, value: DataValue) -> Self {
|
||||
let tm = self
|
||||
.type_map
|
||||
.clone()
|
||||
.unwrap_or_else(TypeMap::latest);
|
||||
self.data.insert(data.to_id(&tm), value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn get_data(&self, data_type: DataTypeId) -> &DataValue {
|
||||
self.data.get(&data_type).unwrap_or(&DataValue::Null)
|
||||
}
|
||||
|
|
@ -193,15 +226,16 @@ impl CommunicationValue {
|
|||
sender,
|
||||
receiver,
|
||||
data,
|
||||
type_map: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================ TESTS ================================ */
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::data_value::DataValue;
|
||||
use mtp_type_map::{CommunicationTypeId, DataTypeId};
|
||||
|
||||
fn roundtrip(cv: CommunicationValue) -> CommunicationValue {
|
||||
let bytes = cv.to_bytes();
|
||||
|
|
|
|||
Loading…
Reference in a new issue