statime/datastructures/
mod.rs

1//! General datastructures as defined by the ptp spec
2
3use core::fmt::Debug;
4
5use self::messages::EnumConversionError;
6
7pub mod common;
8pub mod datasets;
9pub mod messages;
10
11#[derive(Clone, Debug)]
12pub(crate) enum WireFormatError {
13    EnumConversionError,
14    BufferTooShort,
15    CapacityError,
16    Invalid,
17}
18
19impl core::fmt::Display for WireFormatError {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        match self {
22            WireFormatError::EnumConversionError => f.write_str("enum conversion failed"),
23            WireFormatError::BufferTooShort => f.write_str("a buffer is too short"),
24            WireFormatError::CapacityError => f.write_str("a container has insufficient capacity"),
25            WireFormatError::Invalid => f.write_str("an invariant was violated"),
26        }
27    }
28}
29
30#[cfg(feature = "std")]
31impl std::error::Error for WireFormatError {}
32
33impl From<arrayvec::CapacityError> for WireFormatError {
34    fn from(_: arrayvec::CapacityError) -> Self {
35        WireFormatError::CapacityError
36    }
37}
38
39impl From<EnumConversionError> for WireFormatError {
40    fn from(_: EnumConversionError) -> Self {
41        Self::EnumConversionError
42    }
43}
44
45trait WireFormat: Debug + Clone + Eq {
46    /// Serializes the object into the PTP wire format.
47    ///
48    /// Returns the used buffer size that contains the message or an error.
49    fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError>;
50
51    /// Deserializes the object from the PTP wire format.
52    ///
53    /// Returns the object and the size in the buffer that it takes up or an
54    /// error.
55    fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError>;
56}