Skip to content

Commit

Permalink
Updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhouse committed Mar 12, 2024
1 parent fd1e417 commit 968e39d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ from strings.
- [x] Words have parsing tests
- [x] Words have conversion tests
- [x] Documentation exists for words
- [ ] Messages implemented
- [x] Messages implemented
- [x] Message struct is created
- [x] Messages can be constructed from words
- [x] Messages can be parsed from binary
Expand Down
9 changes: 8 additions & 1 deletion src/message/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{errors::*, Header, Packet, Word};
///
/// * Command or status words are always the first word.
/// * Data words are limited based on the command word count.
/// * For status words, data words are parsed to the end of the buffer
///
/// Messages do not validate larger messaging patterns that
/// require context about previous messages or terminal type.
Expand Down Expand Up @@ -103,7 +104,9 @@ impl<const WORDS: usize> Message<WORDS> {
///
/// Each word is a triplet containing 3-bit sync, 16-bit word,
/// and 1-bit parity. It is assumed that the message
/// being parsed is aligned to the beginning of the slice.
/// being parsed is aligned to the beginning of the slice
/// (the leftmost three bits of the first byte are the sync
/// field of the command word).
///
/// # Arguments
///
Expand Down Expand Up @@ -141,6 +144,10 @@ impl<const WORDS: usize> Message<WORDS> {
/// byte array. Slice the input data to avoid parsing
/// any unwanted words.
///
/// It is assumed that the message being parsed is aligned
/// to the beginning of the slice (the leftmost three bits
/// of the first byte are the sync field of the status word).
///
/// # Arguments
///
/// * `data` - A slice of bytes to parse
Expand Down
42 changes: 28 additions & 14 deletions src/message/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,21 @@ mod tests {
use crate::flags::{Address, BroadcastReceived, SubAddress};

#[test]
fn test_packet_parse_offset_13() {
fn test_packet_command_bad_sync() {
let result1: Result<CommandWord> = Packet::read(&[0b10000000, 0b00000000, 0b00010000], 0)
.unwrap()
.try_into();

let result2: Result<CommandWord> = Packet::read(&[0b00100000, 0b00000000, 0b00010000], 0)
.unwrap()
.try_into();

assert!(result1.is_ok());
assert_eq!(result2, Err(Error::PacketIsInvalid));
}

#[test]
fn test_packet_read_offset_13() {
let result = Packet::read(
&[0b00000000, 0b00000111, 0b10000000, 0b00000001, 0b10000000],
13,
Expand All @@ -276,7 +290,7 @@ mod tests {
}

#[test]
fn test_packet_parse_offset_12() {
fn test_packet_read_offset_12() {
let packet = Packet::read(&[0b00000000, 0b00001111, 0b00000000, 0b00000011], 12).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand All @@ -285,7 +299,7 @@ mod tests {
}

#[test]
fn test_packet_parse_offset_6() {
fn test_packet_read_offset_6() {
let packet = Packet::read(&[0b00000011, 0b11000000, 0b00000000, 0b11000000], 6).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand All @@ -294,7 +308,7 @@ mod tests {
}

#[test]
fn test_packet_parse_offset_4() {
fn test_packet_read_offset_4() {
let packet = Packet::read(&[0b00001111, 0b00000000, 0b00000011], 4).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand All @@ -303,7 +317,7 @@ mod tests {
}

#[test]
fn test_packet_parse_offset_0() {
fn test_packet_read_offset_0() {
let packet = Packet::read(&[0b11110000, 0b00000000, 0b00110000], 0).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand Down Expand Up @@ -456,7 +470,7 @@ mod tests {
}

#[test]
fn test_packet_parse_word_alternate() {
fn test_packet_read_word_alternate() {
let packet = Packet::read(&[0b00010101, 0b01010101, 0b01000000], 0).unwrap();

assert_eq!(packet.sync, 0b00000000);
Expand All @@ -465,7 +479,7 @@ mod tests {
}

#[test]
fn test_packet_parse_word_ones() {
fn test_packet_read_word_ones() {
let packet = Packet::read(&[0b00011111, 0b11111111, 0b11100000], 0).unwrap();

assert_eq!(packet.sync, 0b00000000);
Expand All @@ -474,7 +488,7 @@ mod tests {
}

#[test]
fn test_packet_parse_word_zeroes() {
fn test_packet_read_word_zeroes() {
let packet = Packet::read(&[0b11100000, 0b00000000, 0b00010000], 0).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand All @@ -483,7 +497,7 @@ mod tests {
}

#[test]
fn test_packet_parse_sync_zeroes() {
fn test_packet_read_sync_zeroes() {
let packet = Packet::read(&[0b00011111, 0b11111111, 0b11111111], 0).unwrap();

assert_eq!(packet.sync, 0b00000000);
Expand All @@ -492,7 +506,7 @@ mod tests {
}

#[test]
fn test_packet_parse_sync_ones() {
fn test_packet_read_sync_ones() {
let packet = Packet::read(&[0b11100000, 0b00000000, 0b00000000], 0).unwrap();

assert_eq!(packet.sync, 0b00000111);
Expand All @@ -501,7 +515,7 @@ mod tests {
}

#[test]
fn test_packet_parse_parity_one() {
fn test_packet_read_parity_one() {
let packet = Packet::read(
&[
0b00000000, 0b00000000, 0b00010000, // 20th
Expand All @@ -516,7 +530,7 @@ mod tests {
}

#[test]
fn test_packet_parse_parity_one_right() {
fn test_packet_read_parity_one_right() {
let packet = Packet::read(
&[
0b00000000, 0b00000000, 0b00001000, // 21st
Expand All @@ -531,7 +545,7 @@ mod tests {
}

#[test]
fn test_packet_parse_parity_one_left() {
fn test_packet_read_parity_one_left() {
let packet = Packet::read(
&[
0b00000000, 0b00000000, 0b00100000, // 19th
Expand All @@ -546,7 +560,7 @@ mod tests {
}

#[test]
fn test_packet_parse_parity_zero() {
fn test_packet_read_parity_zero() {
let packet = Packet::read(
&[
0b11111111, 0b11111111, 0b11101111, // 20th
Expand Down

0 comments on commit 968e39d

Please sign in to comment.