diff --git a/CHANGELOG.md b/CHANGELOG.md index be8d509..6104b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added new `util` module which contains the following (new) helper modules: - `UnsignedEnum` trait as an abstraction for unsigned byte fields with variable lengths. It is - not tied to the ECSS PFC value like the `EcssEnumeration` trait. + not tied to the ECSS PFC value like the `EcssEnumeration` trait. The method to retrieve + the size of the unsigned enumeration in bytes is now called `size`. - `GenericUnsignedByteField` and helper typedefs `UnsignedU8`, `UnsignedU16`, `UnsignedU32` and `UnsignedU64` as helper types implementing `UnsignedEnum` - `UnsignedByteField` as a type-erased helper. diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 2ef5771..a05c9c3 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -171,26 +171,26 @@ impl CommonPduConfig { let source_id = source_id.into(); let dest_id = dest_id.into(); let transaction_seq_num = transaction_seq_num.into(); - if source_id.len() != dest_id.len() { + if source_id.size() != dest_id.size() { return Err(PduError::SourceDestIdLenMissmatch(( - source_id.len(), - dest_id.len(), + source_id.size(), + dest_id.size(), ))); } - if source_id.len() != 1 - && source_id.len() != 2 - && source_id.len() != 4 - && source_id.len() != 8 + if source_id.size() != 1 + && source_id.size() != 2 + && source_id.size() != 4 + && source_id.size() != 8 { - return Err(PduError::InvalidEntityLen(source_id.len() as u8)); + return Err(PduError::InvalidEntityLen(source_id.size() as u8)); } - if transaction_seq_num.len() != 1 - && transaction_seq_num.len() != 2 - && transaction_seq_num.len() != 4 - && transaction_seq_num.len() != 8 + if transaction_seq_num.size() != 1 + && transaction_seq_num.size() != 2 + && transaction_seq_num.size() != 4 + && transaction_seq_num.size() != 8 { return Err(PduError::InvalidTransactionSeqNumLen( - transaction_seq_num.len() as u8, + transaction_seq_num.size() as u8, )); } Ok(Self { @@ -298,9 +298,9 @@ impl PduHeader { /// Returns only the length of the PDU header when written to a raw buffer. pub fn header_len(&self) -> usize { FIXED_HEADER_LEN - + self.pdu_conf.source_entity_id.len() - + self.pdu_conf.transaction_seq_num.len() - + self.pdu_conf.dest_entity_id.len() + + self.pdu_conf.source_entity_id.size() + + self.pdu_conf.transaction_seq_num.size() + + self.pdu_conf.dest_entity_id.size() } /// Returns the full length of the PDU when written to a raw buffer, which is the header length @@ -312,16 +312,16 @@ impl PduHeader { pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result { // 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.len() != self.pdu_conf.dest_entity_id.len() { + 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.len(), - self.pdu_conf.dest_entity_id.len(), + self.pdu_conf.source_entity_id.size(), + self.pdu_conf.dest_entity_id.size(), ))); } if buf.len() < FIXED_HEADER_LEN - + self.pdu_conf.source_entity_id.len() - + self.pdu_conf.transaction_seq_num.len() + + self.pdu_conf.source_entity_id.size() + + self.pdu_conf.transaction_seq_num.size() { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { found: buf.len(), @@ -340,22 +340,22 @@ impl PduHeader { buf[current_idx..current_idx + 2].copy_from_slice(&self.pdu_datafield_len.to_be_bytes()); current_idx += 2; buf[current_idx] = ((self.seg_ctrl as u8) << 7) - | (((self.pdu_conf.source_entity_id.len() - 1) as u8) << 4) + | (((self.pdu_conf.source_entity_id.size() - 1) as u8) << 4) | ((self.seg_metadata_flag as u8) << 3) - | ((self.pdu_conf.transaction_seq_num.len() - 1) as u8); + | ((self.pdu_conf.transaction_seq_num.size() - 1) as u8); current_idx += 1; self.pdu_conf.source_entity_id.write_to_be_bytes( - &mut buf[current_idx..current_idx + self.pdu_conf.source_entity_id.len()], + &mut buf[current_idx..current_idx + self.pdu_conf.source_entity_id.size()], )?; - current_idx += self.pdu_conf.source_entity_id.len(); + current_idx += self.pdu_conf.source_entity_id.size(); self.pdu_conf.transaction_seq_num.write_to_be_bytes( - &mut buf[current_idx..current_idx + self.pdu_conf.transaction_seq_num.len()], + &mut buf[current_idx..current_idx + self.pdu_conf.transaction_seq_num.size()], )?; - current_idx += self.pdu_conf.transaction_seq_num.len(); + current_idx += self.pdu_conf.transaction_seq_num.size(); self.pdu_conf.dest_entity_id.write_to_be_bytes( - &mut buf[current_idx..current_idx + self.pdu_conf.dest_entity_id.len()], + &mut buf[current_idx..current_idx + self.pdu_conf.dest_entity_id.size()], )?; - current_idx += self.pdu_conf.dest_entity_id.len(); + current_idx += self.pdu_conf.dest_entity_id.size(); Ok(current_idx) } diff --git a/src/cfdp/tlv.rs b/src/cfdp/tlv.rs index 2bb3e9d..4ec2580 100644 --- a/src/cfdp/tlv.rs +++ b/src/cfdp/tlv.rs @@ -175,17 +175,17 @@ impl EntityIdTlv { } pub fn len_value(&self) -> usize { - self.entity_id.len() + self.entity_id.size() } pub fn len_full(&self) -> usize { - 2 + self.entity_id.len() + 2 + self.entity_id.size() } pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result { Self::len_check(buf)?; buf[0] = TlvType::EntityId as u8; - buf[1] = self.entity_id.len() as u8; + buf[1] = self.entity_id.size() as u8; self.entity_id.write_to_be_bytes(&mut buf[2..]) } @@ -205,8 +205,8 @@ impl EntityIdTlv { pub fn to_tlv(self, buf: &mut [u8]) -> Result { Self::len_check(buf)?; self.entity_id - .write_to_be_bytes(&mut buf[2..2 + self.entity_id.len()])?; - Tlv::new(TlvType::EntityId, &buf[2..2 + self.entity_id.len()]).map_err(|e| match e { + .write_to_be_bytes(&mut buf[2..2 + self.entity_id.size()])?; + Tlv::new(TlvType::EntityId, &buf[2..2 + self.entity_id.size()]).map_err(|e| match e { TlvLvError::ByteConversionError(e) => e, // All other errors are impossible. _ => panic!("unexpected TLV error"), diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index 952ba9b..e5ceedc 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -324,7 +324,7 @@ impl GenericEcssEnumWrapper { } impl UnsignedEnum for GenericEcssEnumWrapper { - fn len(&self) -> usize { + fn size(&self) -> usize { (self.pfc() / 8) as usize } diff --git a/src/util.rs b/src/util.rs index b5d34b1..d77ca66 100644 --- a/src/util.rs +++ b/src/util.rs @@ -68,9 +68,9 @@ impl ToBeBytes for u64 { } } -#[allow(clippy::len_without_is_empty)] pub trait UnsignedEnum { - fn len(&self) -> usize; + /// Size of the unsigned enumeration in bytes. + fn size(&self) -> usize; /// Write the unsigned enumeration to a raw buffer. Returns the written size on success. fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result; } @@ -160,18 +160,18 @@ impl UnsignedByteField { } impl UnsignedEnum for UnsignedByteField { - fn len(&self) -> usize { + fn size(&self) -> usize { self.width } fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result { - if buf.len() < self.len() { + if buf.len() < self.size() { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { - expected: self.len(), + expected: self.size(), found: buf.len(), })); } - match self.len() { + match self.size() { 0 => Ok(0), 1 => { let u8 = UnsignedByteFieldU8::try_from(*self).unwrap(); @@ -210,18 +210,18 @@ impl GenericUnsignedByteField { } impl UnsignedEnum for GenericUnsignedByteField { - fn len(&self) -> usize { + fn size(&self) -> usize { self.value.written_len() } fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result { - if buf.len() < self.len() { + if buf.len() < self.size() { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { found: buf.len(), - expected: self.len(), + expected: self.size(), })); } - buf[0..self.len()].copy_from_slice(self.value.to_be_bytes().as_ref()); + buf[0..self.size()].copy_from_slice(self.value.to_be_bytes().as_ref()); Ok(self.value.written_len()) } }