From 965541e42207daf6ca1ef8f134a9c48a2fa3c88f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 20:41:45 +0200 Subject: [PATCH 1/6] getter function for datafield len --- src/cfdp/pdu/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 6d93da9..d015d99 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -340,6 +340,10 @@ impl PduHeader { + self.pdu_conf.dest_entity_id.size() } + pub fn pdu_datafield_len(&self) -> usize { + self.pdu_datafield_len.into() + } + /// Returns the full length of the PDU when written to a raw buffer, which is the header length /// plus the PDU datafield length. pub fn pdu_len(&self) -> usize { From 990b8de519d2b04c0026c56d27e1c0d09703197d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 20:42:45 +0200 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95da44f..42af3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - `UnsignedByteField` and `GenericUnsignedByteField` `new` methods are `const` now. +# Added + +- Added `pdu_datafield_len` getter function for `PduHeader` + # [v0.7.0-beta.0] 2023-08-16 - Moved MSRV from v1.60 to v1.61. From 9dfc593d2a70110e8ecb4b4151e8555092407e00 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 21:04:27 +0200 Subject: [PATCH 3/6] fixes for pdu error enum --- src/cfdp/pdu/eof.rs | 2 +- src/cfdp/pdu/finished.rs | 2 +- src/cfdp/pdu/metadata.rs | 2 +- src/cfdp/pdu/mod.rs | 7 +++---- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cfdp/pdu/eof.rs b/src/cfdp/pdu/eof.rs index 71f977b..16f2bba 100644 --- a/src/cfdp/pdu/eof.rs +++ b/src/cfdp/pdu/eof.rs @@ -106,7 +106,7 @@ impl EofPdu { } generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], FileDirectiveType::EofPdu)) + PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::EofPdu))) })?; if directive_type != FileDirectiveType::EofPdu { return Err(PduError::WrongDirectiveType(( diff --git a/src/cfdp/pdu/finished.rs b/src/cfdp/pdu/finished.rs index 29cf747..463ca8d 100644 --- a/src/cfdp/pdu/finished.rs +++ b/src/cfdp/pdu/finished.rs @@ -170,7 +170,7 @@ impl<'fs_responses> FinishedPdu<'fs_responses> { let min_expected_len = current_idx + 2; generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], FileDirectiveType::FinishedPdu)) + PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::FinishedPdu))) })?; if directive_type != FileDirectiveType::FinishedPdu { return Err(PduError::WrongDirectiveType(( diff --git a/src/cfdp/pdu/metadata.rs b/src/cfdp/pdu/metadata.rs index 2e3b5f0..fe2e4d2 100644 --- a/src/cfdp/pdu/metadata.rs +++ b/src/cfdp/pdu/metadata.rs @@ -248,7 +248,7 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> { } generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], FileDirectiveType::MetadataPdu)) + PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::MetadataPdu))) })?; if directive_type != FileDirectiveType::MetadataPdu { return Err(PduError::WrongDirectiveType(( diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index d015d99..729a88a 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -43,8 +43,8 @@ pub enum PduError { WrongDirectiveType((FileDirectiveType, FileDirectiveType)), /// The directive type field contained a value not in the range of permitted values. /// The first tuple entry will be the found raw number, the second entry the expected entry - /// type. - InvalidDirectiveType((u8, FileDirectiveType)), + /// type when applicable. + InvalidDirectiveType((u8, Option)), /// Invalid condition code. Contains the raw detected value. InvalidConditionCode(u8), /// Invalid checksum type which is not part of the checksums listed in the @@ -101,8 +101,7 @@ impl Display for PduError { PduError::InvalidDirectiveType((found, expected)) => { write!( f, - "invalid directive type value {found}, expected {expected:?} ({})", - *expected as u8 + "invalid directive type value {found}, expected {expected:?}" ) } PduError::InvalidChecksumType(checksum_type) => { From c96a86a9948394272f864dec80ef5253be492a0e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 21:22:49 +0200 Subject: [PATCH 4/6] this should be better --- CHANGELOG.md | 2 ++ src/cfdp/pdu/eof.rs | 13 ++++++----- src/cfdp/pdu/finished.rs | 13 ++++++----- src/cfdp/pdu/metadata.rs | 13 ++++++----- src/cfdp/pdu/mod.rs | 48 ++++++++++++++++++++++------------------ 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42af3f7..d0a612f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Changed - `UnsignedByteField` and `GenericUnsignedByteField` `new` methods are `const` now. +- `PduError` variants which contained a tuple variant with multiple fields were converted to a + struct variant. # Added diff --git a/src/cfdp/pdu/eof.rs b/src/cfdp/pdu/eof.rs index 16f2bba..e93a580 100644 --- a/src/cfdp/pdu/eof.rs +++ b/src/cfdp/pdu/eof.rs @@ -106,13 +106,16 @@ impl EofPdu { } generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::EofPdu))) + PduError::InvalidDirectiveType { + found: buf[current_idx], + expected: Some(FileDirectiveType::EofPdu), + } })?; if directive_type != FileDirectiveType::EofPdu { - return Err(PduError::WrongDirectiveType(( - directive_type, - FileDirectiveType::EofPdu, - ))); + return Err(PduError::WrongDirectiveType { + found: directive_type, + expected: FileDirectiveType::EofPdu, + }); } current_idx += 1; let condition_code = ConditionCode::try_from((buf[current_idx] >> 4) & 0b1111) diff --git a/src/cfdp/pdu/finished.rs b/src/cfdp/pdu/finished.rs index 463ca8d..c0afa98 100644 --- a/src/cfdp/pdu/finished.rs +++ b/src/cfdp/pdu/finished.rs @@ -170,13 +170,16 @@ impl<'fs_responses> FinishedPdu<'fs_responses> { let min_expected_len = current_idx + 2; generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::FinishedPdu))) + PduError::InvalidDirectiveType { + found: buf[current_idx], + expected: Some(FileDirectiveType::FinishedPdu), + } })?; if directive_type != FileDirectiveType::FinishedPdu { - return Err(PduError::WrongDirectiveType(( - directive_type, - FileDirectiveType::FinishedPdu, - ))); + return Err(PduError::WrongDirectiveType { + found: directive_type, + expected: FileDirectiveType::FinishedPdu, + }); } current_idx += 1; let condition_code = ConditionCode::try_from((buf[current_idx] >> 4) & 0b1111) diff --git a/src/cfdp/pdu/metadata.rs b/src/cfdp/pdu/metadata.rs index fe2e4d2..de84f22 100644 --- a/src/cfdp/pdu/metadata.rs +++ b/src/cfdp/pdu/metadata.rs @@ -248,13 +248,16 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> { } generic_length_checks_pdu_deserialization(buf, min_expected_len, full_len_without_crc)?; let directive_type = FileDirectiveType::try_from(buf[current_idx]).map_err(|_| { - PduError::InvalidDirectiveType((buf[current_idx], Some(FileDirectiveType::MetadataPdu))) + PduError::InvalidDirectiveType { + found: buf[current_idx], + expected: Some(FileDirectiveType::MetadataPdu), + } })?; if directive_type != FileDirectiveType::MetadataPdu { - return Err(PduError::WrongDirectiveType(( - directive_type, - FileDirectiveType::MetadataPdu, - ))); + return Err(PduError::WrongDirectiveType { + found: directive_type, + expected: FileDirectiveType::MetadataPdu, + }); } current_idx += 1; let (fss_len, file_size) = diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 729a88a..afb0bb1 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -35,16 +35,19 @@ pub enum PduError { InvalidEntityLen(u8), /// Invalid length for the entity ID detected. Only the values 1, 2, 4 and 8 are supported. InvalidTransactionSeqNumLen(u8), - /// The first entry will be the source entity ID length, the second one the destination entity - /// ID length. - SourceDestIdLenMissmatch((usize, usize)), - /// The first tuple entry will be the found directive type, the second entry the expected entry - /// type. - WrongDirectiveType((FileDirectiveType, FileDirectiveType)), + SourceDestIdLenMissmatch { + src_id_len: usize, + dest_id_len: usize, + }, + WrongDirectiveType { + found: FileDirectiveType, + expected: FileDirectiveType, + }, /// The directive type field contained a value not in the range of permitted values. - /// The first tuple entry will be the found raw number, the second entry the expected entry - /// type when applicable. - InvalidDirectiveType((u8, Option)), + InvalidDirectiveType { + found: u8, + expected: Option, + }, /// Invalid condition code. Contains the raw detected value. InvalidConditionCode(u8), /// Invalid checksum type which is not part of the checksums listed in the @@ -80,10 +83,13 @@ impl Display for PduError { "cfdp version missmatch, found {raw}, expected {CFDP_VERSION_2}" ) } - PduError::SourceDestIdLenMissmatch((src_len, dest_len)) => { + PduError::SourceDestIdLenMissmatch { + src_id_len, + dest_id_len, + } => { write!( f, - "missmatch of PDU source length {src_len} and destination length {dest_len}" + "missmatch of PDU source length {src_id_len} and destination length {dest_id_len}" ) } PduError::ByteConversionError(e) => { @@ -92,13 +98,13 @@ impl Display for PduError { PduError::FileSizeTooLarge(value) => { write!(f, "file size value {value} exceeds allowed 32 bit width") } - PduError::WrongDirectiveType((found, expected)) => { + PduError::WrongDirectiveType { found, expected } => { write!(f, "found directive type {found:?}, expected {expected:?}") } PduError::InvalidConditionCode(raw_code) => { write!(f, "found invalid condition code with raw value {raw_code}") } - PduError::InvalidDirectiveType((found, expected)) => { + PduError::InvalidDirectiveType { found, expected } => { write!( f, "invalid directive type value {found}, expected {expected:?}" @@ -216,10 +222,10 @@ impl CommonPduConfig { let source_id = source_id.into(); let dest_id = dest_id.into(); if source_id.size() != dest_id.size() { - return Err(PduError::SourceDestIdLenMissmatch(( - source_id.size(), - dest_id.size(), - ))); + return Err(PduError::SourceDestIdLenMissmatch { + src_id_len: source_id.size(), + dest_id_len: dest_id.size(), + }); } if source_id.size() != 1 && source_id.size() != 2 @@ -353,10 +359,10 @@ impl PduHeader { // Internal note: There is currently no way to pass a PDU configuration like this, but // this check is still kept for defensive programming. if self.pdu_conf.source_entity_id.size() != self.pdu_conf.dest_entity_id.size() { - return Err(PduError::SourceDestIdLenMissmatch(( - self.pdu_conf.source_entity_id.size(), - self.pdu_conf.dest_entity_id.size(), - ))); + return Err(PduError::SourceDestIdLenMissmatch { + src_id_len: self.pdu_conf.source_entity_id.size(), + dest_id_len: self.pdu_conf.dest_entity_id.size(), + }); } if buf.len() < FIXED_HEADER_LEN From f208a9b0f0ded1171afb4bdf4a4f9f9d12a144bb Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 21:24:12 +0200 Subject: [PATCH 5/6] fixed a test --- src/cfdp/pdu/mod.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index afb0bb1..7ed249c 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -83,10 +83,7 @@ impl Display for PduError { "cfdp version missmatch, found {raw}, expected {CFDP_VERSION_2}" ) } - PduError::SourceDestIdLenMissmatch { - src_id_len, - dest_id_len, - } => { + PduError::SourceDestIdLenMissmatch { src_id_len, dest_id_len }=> { write!( f, "missmatch of PDU source length {src_id_len} and destination length {dest_id_len}" @@ -98,7 +95,7 @@ impl Display for PduError { PduError::FileSizeTooLarge(value) => { write!(f, "file size value {value} exceeds allowed 32 bit width") } - PduError::WrongDirectiveType { found, expected } => { + PduError::WrongDirectiveType { found, expected }=> { write!(f, "found directive type {found:?}, expected {expected:?}") } PduError::InvalidConditionCode(raw_code) => { @@ -225,6 +222,7 @@ impl CommonPduConfig { return Err(PduError::SourceDestIdLenMissmatch { src_id_len: source_id.size(), dest_id_len: dest_id.size(), + }); } if source_id.size() != 1 @@ -931,9 +929,9 @@ mod tests { CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_id); assert!(pdu_conf_res.is_err()); let error = pdu_conf_res.unwrap_err(); - if let PduError::SourceDestIdLenMissmatch((src_len, dest_len)) = error { - assert_eq!(src_len, 1); - assert_eq!(dest_len, 2); + if let PduError::SourceDestIdLenMissmatch { src_id_len, dest_id_len }= error { + assert_eq!(src_id_len, 1); + assert_eq!(dest_id_len, 2); } } From 805065a7b9be27a04d459ab5bbb058e231d297c4 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 17 Aug 2023 22:13:00 +0200 Subject: [PATCH 6/6] cargo fmt --- src/cfdp/pdu/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 7ed249c..980607f 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -83,7 +83,10 @@ impl Display for PduError { "cfdp version missmatch, found {raw}, expected {CFDP_VERSION_2}" ) } - PduError::SourceDestIdLenMissmatch { src_id_len, dest_id_len }=> { + PduError::SourceDestIdLenMissmatch { + src_id_len, + dest_id_len, + } => { write!( f, "missmatch of PDU source length {src_id_len} and destination length {dest_id_len}" @@ -95,7 +98,7 @@ impl Display for PduError { PduError::FileSizeTooLarge(value) => { write!(f, "file size value {value} exceeds allowed 32 bit width") } - PduError::WrongDirectiveType { found, expected }=> { + PduError::WrongDirectiveType { found, expected } => { write!(f, "found directive type {found:?}, expected {expected:?}") } PduError::InvalidConditionCode(raw_code) => { @@ -222,7 +225,6 @@ impl CommonPduConfig { return Err(PduError::SourceDestIdLenMissmatch { src_id_len: source_id.size(), dest_id_len: dest_id.size(), - }); } if source_id.size() != 1 @@ -929,7 +931,11 @@ mod tests { CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_id); assert!(pdu_conf_res.is_err()); let error = pdu_conf_res.unwrap_err(); - if let PduError::SourceDestIdLenMissmatch { src_id_len, dest_id_len }= error { + if let PduError::SourceDestIdLenMissmatch { + src_id_len, + dest_id_len, + } = error + { assert_eq!(src_id_len, 1); assert_eq!(dest_id_len, 2); }