Skip to content

Commit

Permalink
Merge pull request #27 from mjhouse/add_with_methods
Browse files Browse the repository at this point in the history
Add with methods
  • Loading branch information
mjhouse authored Mar 4, 2024
2 parents ade3cdd + e78798f commit 4bb83d5
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 114 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ or military projects that can't have virally licensed dependencies.

let mut message = Message::new();

let mut command = CommandWord::new(0b0001100001100010);
assert_eq!(command.count(),2);
message.add_command(CommandWord::new()
.with_subaddress(12)
.with_subaddress(5)
.with_word_count(2)
.build()
.unwrap()
).unwrap();

message.add_command(command);
assert_eq!(message.data_count(),0);
assert_eq!(message.data_expected(),2);

// add two data words
message.add_data(DataWord::new(0b0110100001101001)).unwrap();
message.add_data(DataWord::new(0b0110100001101001)).unwrap();
message.add_data(DataWord::new()).unwrap();
message.add_data(DataWord::new()).unwrap();

assert!(message.is_full());
assert_eq!(message.word_count(),3);
assert_eq!(message.data_count(),2);
assert_eq!(message.data_expected(),2);
```

## Words
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub enum Error {
/// A word was found to be invalid while building a message
WordIsInvalid,

/// A byte array could not be converted to a string
StringIsInvalid,

/// An invalid StatusWord was given while building a message
InvalidStatusWord,

Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,22 @@ pub mod message;
pub mod word;

pub use message::Message;
pub use word::{CommandWord,StatusWord,DataWord};

pub use word::{
CommandWord,
StatusWord,
DataWord
};

pub use flags::{
ModeCode,
TransmitReceive,
Address,
SubAddress,
Instrumentation,
ServiceRequest,
Reserved,
BroadcastCommand,
TerminalBusy,
BusControlAccept,
};
18 changes: 9 additions & 9 deletions src/message/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mod tests {
fn test_message_command_add() {
let mut message = Message::new();

let word = Word::Command(CommandWord::new(0b0001100001100010));
let word = Word::Command(CommandWord::from(0b0001100001100010));
let result = message.add(word.clone());

assert_eq!(result, Ok(1));
Expand All @@ -195,7 +195,7 @@ mod tests {
fn test_message_command_data() {
let mut message = Message::new();

let word = Word::Command(CommandWord::new(0b0001100001100010));
let word = Word::Command(CommandWord::from(0b0001100001100010));
message.add(word.clone()).unwrap();

assert_eq!(message.word_count(), 1);
Expand All @@ -207,10 +207,10 @@ mod tests {
fn test_message_command_add_data() {
let mut message = Message::new();

let word = Word::Command(CommandWord::new(0b0001100001100010));
let word = Word::Command(CommandWord::from(0b0001100001100010));
message.add(word.clone()).unwrap();

let data = Word::Data(DataWord::new(0b0110100001101001));
let data = Word::Data(DataWord::from(0b0110100001101001));
message.add(data.clone()).unwrap();

assert_eq!(message.word_count(), 2);
Expand All @@ -221,7 +221,7 @@ mod tests {
fn test_message_status_add() {
let mut message = Message::new();

let word = Word::Status(StatusWord::new(0b0001100000000010));
let word = Word::Status(StatusWord::from(0b0001100000000010));
let result = message.add(word.clone());

assert_eq!(result, Ok(1));
Expand All @@ -234,7 +234,7 @@ mod tests {
let mut message = Message::new();

// word is using the reserved bits (0b0000000011100000)
let word = Word::Status(StatusWord::new(0b0000000011100000));
let word = Word::Status(StatusWord::from(0b0000000011100000));
let result = message.add(word.clone());
assert!(result.is_err());
}
Expand All @@ -243,7 +243,7 @@ mod tests {
fn test_message_status_no_data() {
let mut message = Message::new();

let word = Word::Status(StatusWord::new(0b0001100000000010));
let word = Word::Status(StatusWord::from(0b0001100000000010));
message.add(word.clone()).unwrap();

assert_eq!(message.word_count(), 1);
Expand All @@ -255,10 +255,10 @@ mod tests {
fn test_message_status_add_data() {
let mut message = Message::new();

let status = Word::Status(StatusWord::new(0b0001100000000000));
let status = Word::Status(StatusWord::from(0b0001100000000000));
message.add(status.clone()).unwrap();

let data = Word::Data(DataWord::new(0b0110100001101001));
let data = Word::Data(DataWord::from(0b0110100001101001));
message.add(data.clone()).unwrap();

assert_eq!(message.word_count(), 2);
Expand Down
6 changes: 3 additions & 3 deletions src/message/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Packet {
/// Convert this packet into a data word
pub fn to_data(&self) -> Result<DataWord> {
if self.is_valid() & self.is_data() {
Ok(DataWord::new(self.value()))
Ok(DataWord::from(self.value()))
} else {
Err(Error::PacketIsInvalid)
}
Expand All @@ -74,7 +74,7 @@ impl Packet {
/// Convert this packet into a status word
pub fn to_status(&self) -> Result<StatusWord> {
if self.is_valid() & self.is_service() {
Ok(StatusWord::new(self.value()))
Ok(StatusWord::from(self.value()))
} else {
Err(Error::PacketIsInvalid)
}
Expand All @@ -83,7 +83,7 @@ impl Packet {
/// Convert this packet into a command word
pub fn to_command(&self) -> Result<CommandWord> {
if self.is_valid() & self.is_service() {
Ok(CommandWord::new(self.value()))
Ok(CommandWord::from(self.value()))
} else {
Err(Error::PacketIsInvalid)
}
Expand Down
Loading

0 comments on commit 4bb83d5

Please sign in to comment.