pub const FORMAT1_PROPERTY_HEADER_SIZE: usize = 2;
pub type PropertyCode = u8;
pub struct Property {
code: PropertyCode,
data: Vec<u8>,
}
impl Property {
pub fn new() -> Property {
Property {
code: 0,
data: Vec::new(),
}
}
pub fn from(code: PropertyCode, data: Vec<u8>) -> Property {
Property {
code: code,
data: data,
}
}
pub fn set_code(&mut self, code: PropertyCode) -> &mut Self {
self.code = code;
self
}
pub fn code(&self) -> PropertyCode {
self.code
}
pub fn set_data(&mut self, data: Vec<u8>) -> &mut Self {
self.data = data.clone();
self
}
pub fn data(&self) -> &Vec<u8> {
&self.data
}
pub fn size(&self) -> usize {
self.data.len()
}
pub fn parse(&mut self, msg: &[u8]) -> bool {
let msg_len = msg.len();
if msg_len < FORMAT1_PROPERTY_HEADER_SIZE {
return false;
}
self.code = msg[0];
let data_size = msg[1] as usize;
self.data.clear();
if data_size == 0 {
return true;
}
if msg_len < (FORMAT1_PROPERTY_HEADER_SIZE + data_size) {
return false;
}
let prop_data = &(*msg)[2..];
for n in 0..data_size {
self.data.push(prop_data[n]);
}
true
}
pub fn equals(&self, prop: &Property) -> bool {
if self.code() != prop.code() {
return false;
}
if self.size() != prop.size() {
return false;
}
for n in 0..self.size() {
if self.data[n] != prop.data[n] {
return false;
}
}
true
}
}
impl Clone for Property {
fn clone(&self) -> Property {
Property {
code: self.code(),
data: self.data.clone(),
}
}
}