diff options
author | Stuart Stock <stuart@int08h.com> | 2018-09-29 23:48:01 -0500 |
---|---|---|
committer | Stuart Stock <stuart@int08h.com> | 2018-09-29 23:48:01 -0500 |
commit | 146ed83d361ffd44d09e3879dde809150bb07a7a (patch) | |
tree | c5d4defd34d1f58feb36a9b5fa5b55e490683749 /src/message.rs | |
parent | 606b144dc40ed418253d26bb56bcbe9469d04cf1 (diff) | |
download | roughenough-146ed83d361ffd44d09e3879dde809150bb07a7a.zip |
Major refeactoring for better code structure
* Extract distinct types for online and long-term keys
* Extract and create separate configuration trait and implementation(s)
* Clean-ups, renames, tidying
Diffstat (limited to 'src/message.rs')
-rw-r--r-- | src/message.rs | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/src/message.rs b/src/message.rs index 2927295..bd11bf7 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,4 +1,4 @@ -// Copyright 2017 int08h LLC +// Copyright 2017-2018 int08h LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io::{Cursor, Read, Write}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -use std::iter::once; use std::collections::HashMap; +use std::io::{Cursor, Read, Write}; +use std::iter::once; -use tag::Tag; use error::Error; +use tag::Tag; /// /// A Roughtime protocol message; a map of u32 tags to arbitrary byte-strings. @@ -64,7 +64,7 @@ impl RtMessage { match num_tags { 0 => Ok(RtMessage::new(0)), 1 => RtMessage::single_tag_message(bytes, &mut msg), - 2 ... 1024 => RtMessage::multi_tag_message(num_tags, bytes, &mut msg), + 2...1024 => RtMessage::multi_tag_message(num_tags, bytes, &mut msg), _ => Err(Error::InvalidNumTags(num_tags)), } } @@ -180,6 +180,21 @@ impl RtMessage { Ok(()) } + /// Retrieve the value associated with `tag`, if present. + /// + /// ## Arguments + /// + /// * `tag` - The [`Tag`](enum.Tag.html) to try and retrieve. + /// + pub fn get_field(&self, tag: Tag) -> Option<&[u8]> { + for (i, self_tag) in self.tags.iter().enumerate() { + if tag == *self_tag { + return Some(&self.values[i]); + } + } + return None; + } + /// Returns the number of tag/value pairs in the message pub fn num_fields(&self) -> u32 { self.tags.len() as u32 @@ -272,9 +287,9 @@ impl RtMessage { #[cfg(test)] mod test { - use std::io::{Cursor, Read}; use byteorder::{LittleEndian, ReadBytesExt}; use message::*; + use std::io::{Cursor, Read}; use tag::Tag; #[test] @@ -410,6 +425,20 @@ mod test { } #[test] + fn retrieve_message_values() { + let val1 = b"aabbccddeeffgg"; + let val2 = b"0987654321"; + + let mut msg = RtMessage::new(2); + msg.add_field(Tag::NONC, val1).unwrap(); + msg.add_field(Tag::MAXT, val2).unwrap(); + + assert_eq!(msg.get_field(Tag::NONC), Some(val1.as_ref())); + assert_eq!(msg.get_field(Tag::MAXT), Some(val2.as_ref())); + assert_eq!(msg.get_field(Tag::CERT), None); + } + + #[test] #[should_panic(expected = "InvalidAlignment")] fn from_bytes_offset_past_end_of_message() { let mut msg = RtMessage::new(2); |