From 7b24e8c2f7204663cb7affddab6cd533aef0dd06 Mon Sep 17 00:00:00 2001 From: Myrddin Dundragon Date: Fri, 14 Nov 2025 20:05:38 -0500 Subject: [PATCH] Just adding some comments. --- src/bits.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 15 ++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/bits.rs b/src/bits.rs index f496263..f403bdf 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -1,40 +1,104 @@ +/// Trait for types that can be parsed from a byte slice. +/// +/// Implementors define how a type is reconstructed from raw bytes, returning +/// the number of bytes consumed and the resulting value. pub trait FromBytes: Sized { type Error; + /// Parse `Self` from a slice of bytes. + /// + /// # Parameters + /// - `bytes`: Input slice containing raw data. + /// + /// # Returns + /// A [`Parsed`] struct with the parsed value and bytes consumed, or an error. + /// + /// # Example + /// ``` + /// ``` fn from_bytes(bytes: &[u8]) -> Result, Self::Error>; } +/// Trait for types that can be serialized into a byte slice. +/// +/// Implementors define how a type converts itself into a buffer of bytes. pub trait IntoBytes { type Error; + /// Serialize `Self` into the provided byte buffer. + /// + /// # Parameters + /// - `buffer`: Mutable slice where the serialized data will be written. + /// + /// # Returns + /// The number of bytes written, or an error. fn into_bytes(self, buffer: &mut [u8]) -> Result; } +/// Trait for types that can be parsed from a bitfield. +/// +/// This is useful for partial-byte fields or packed data structures. pub trait FromBits: Sized { type Error; + /// Parse `Self` from a byte slice using a bitmask. + /// + /// # Parameters + /// - `bytes`: Input slice containing raw data. + /// - `mask`: Bitmask indicating which bits are relevant for parsing. + /// + /// # Returns + /// The parsed value or an error. fn from_bits(bytes: &[u8], mask: &[u8]) -> Result; } +/// Trait for types that can be serialized into a bitfield. +/// +/// Allows writing values into selected bits of a buffer. pub trait IntoBits { type Error; + /// Serialize `Self` into a byte slice using a bitmask. + /// + /// # Parameters + /// - `bytes`: Mutable slice where the bits will be written. + /// - `mask`: Bitmask indicating which bits to modify. + /// + /// # Returns + /// `Ok(())` on success, or an error. fn into_bits(self, bytes: &mut [u8], mask: &[u8]) -> Result<(), Self::Error>; } +/// Represents a parsed value and the number of bytes consumed during parsing. +/// +/// This is returned by [`FromBytes::from_bytes`] to indicate how many bytes +/// were read and the resulting value. pub struct Parsed { + /// The number of bytes read from the input buffer. pub bytes_read: usize, + + /// The parsed value. pub data: T } + + impl Parsed { + /// Construct a new `Parsed` instance. + /// + /// # Parameters + /// - `bytes_read`: Number of bytes consumed. + /// - `data`: The parsed value. + /// + /// # Returns + /// A `Parsed` struct containing the data and bytes read. pub fn new(bytes_read: usize, data: T) -> Self { Parsed diff --git a/src/lib.rs b/src/lib.rs index 85eb74f..1559965 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,17 @@ -//! CCSDS Space Packet Protocol +//! # CCSDS Space Packet Protocol +//! This library implements the +//! [CCSDS Space Packet Protocol (CCSDS 133.0-B-2)][SPP], +//! providing a standardized means of encapsulating and routing telemetry and +//! telecommand data in space communication systems. +//! +//! The Space Packet Protocol defines a uniform structure for data exchange +//! between spacecraft subsystems and ground stations. Each packet contains a +//! primary header, an optional secondary header, and an application data field. +//! +//! ## References +//! - [CCSDS 133.0-B-2, *Space Packet Protocol*, Issue 2, September 2012][SPP] +//! +//! [SPP]: https://ccsds.org/wp-content/uploads/gravity_forms/5-448e85c647331d9cbaf66c096458bdd5/2025/01//133x0b2e2.pdf "CCSDS Space Packet Protocol PDF" mod project;