Just adding some comments.

This commit is contained in:
2025-11-14 20:05:38 -05:00
parent eb0e531d3a
commit 7b24e8c2f7
2 changed files with 78 additions and 1 deletions

View File

@ -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 pub trait FromBytes: Sized
{ {
type Error; 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<Parsed<Self>, Self::Error>; fn from_bytes(bytes: &[u8]) -> Result<Parsed<Self>, 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 pub trait IntoBytes
{ {
type Error; 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<usize, Self::Error>; fn into_bytes(self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
} }
/// 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 pub trait FromBits: Sized
{ {
type Error; 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<Self, Self::Error>; fn from_bits(bytes: &[u8], mask: &[u8]) -> Result<Self, Self::Error>;
} }
/// Trait for types that can be serialized into a bitfield.
///
/// Allows writing values into selected bits of a buffer.
pub trait IntoBits pub trait IntoBits
{ {
type Error; 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>; 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<T> pub struct Parsed<T>
{ {
/// The number of bytes read from the input buffer.
pub bytes_read: usize, pub bytes_read: usize,
/// The parsed value.
pub data: T pub data: T
} }
impl<T> Parsed<T> impl<T> Parsed<T>
{ {
/// 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 pub fn new(bytes_read: usize, data: T) -> Self
{ {
Parsed Parsed

View File

@ -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; mod project;