Skip to content

Commit

Permalink
Merge pull request #28 from mjhouse/add_with_messages
Browse files Browse the repository at this point in the history
Added constructor methods to message
  • Loading branch information
mjhouse authored Mar 4, 2024
2 parents 4bb83d5 + d292b53 commit 329dba4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@ or military projects that can't have virally licensed dependencies.
### Creating a message

```rust
use mil_std_1553b::*;

let mut message = Message::new();

message.add_command(CommandWord::new()
.with_subaddress(12)
.with_subaddress(5)
.with_word_count(2)
.build()
.unwrap()
).unwrap();

message.add_data(DataWord::new()).unwrap();
message.add_data(DataWord::new()).unwrap();
# use mil_std_1553b::*;
# fn try_main() -> Result<()> {
let message = Message::new()
.with_command(CommandWord::new()
.with_subaddress(12)
.with_subaddress(5)
.with_word_count(2)
.build()?
)?
.with_data(DataWord::new())?
.with_data(DataWord::new())?;

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

## Words
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
#![deny(clippy::unwrap_used)]
#![doc = include_str!("../README.md")]

pub mod errors;
Expand All @@ -9,6 +10,11 @@ pub mod word;

pub use message::Message;

pub use errors::{
Result,
Error
};

pub use word::{
CommandWord,
StatusWord,
Expand Down
28 changes: 26 additions & 2 deletions src/message/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,21 @@ impl Message {
}

/// Add a generic word to the message, returning size on success
pub fn add(&mut self, word: Word) -> Result<usize> {
match word {
pub fn add<T: Into<Word>>(&mut self, word: T) -> Result<usize> {
match word.into() {
Word::Data(v) => self.add_data(v),
Word::Status(v) => self.add_status(v),
Word::Command(v) => self.add_command(v),
_ => Err(Error::WordIsInvalid),
}
}

/// Constructor method to add a word to the message
pub fn with_word<T: Into<Word>>(mut self, word: T) -> Result<Self> {
self.add(word)?;
Ok(self)
}

/// Add a data word, returning the size of the message on success
pub fn add_data(&mut self, word: DataWord) -> Result<usize> {
if self.is_full() && self.has_command() {
Expand All @@ -131,6 +137,12 @@ impl Message {
}
}

/// Constructor method to add a data word to the message
pub fn with_data<T: Into<DataWord>>(mut self, word: T) -> Result<Self> {
self.add_data(word.into())?;
Ok(self)
}

/// Add a status word, returning the size of the message on success
pub fn add_status(&mut self, word: StatusWord) -> Result<usize> {
if !self.is_empty() {
Expand All @@ -144,6 +156,12 @@ impl Message {
}
}

/// Constructor method to add a status word to the message
pub fn with_status<T: Into<StatusWord>>(mut self, word: T) -> Result<Self> {
self.add_status(word.into())?;
Ok(self)
}

/// Add a command word, returning the size of the message on success
pub fn add_command(&mut self, word: CommandWord) -> Result<usize> {
if !self.is_empty() {
Expand All @@ -154,6 +172,12 @@ impl Message {
Ok(self.count)
}
}

/// Constructor method to add a command word to the message
pub fn with_command<T: Into<CommandWord>>(mut self, word: T) -> Result<Self> {
self.add_command(word.into())?;
Ok(self)
}
}

impl Default for Message {
Expand Down
18 changes: 18 additions & 0 deletions src/word/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ impl Type {
}
}
}

impl From<CommandWord> for Type {
fn from(value: CommandWord) -> Self {
Type::Command(value)
}
}

impl From<StatusWord> for Type {
fn from(value: StatusWord) -> Self {
Type::Status(value)
}
}

impl From<DataWord> for Type {
fn from(value: DataWord) -> Self {
Type::Data(value)
}
}
18 changes: 18 additions & 0 deletions src/word/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,24 @@ impl DataWord {

}

impl Default for CommandWord {
fn default() -> Self {
Self::new()
}
}

impl Default for StatusWord {
fn default() -> Self {
Self::new()
}
}

impl Default for DataWord {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 329dba4

Please sign in to comment.