#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate std;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
pub use alloc::borrow::ToOwned;
#[cfg(not(feature = "std"))]
pub use alloc::vec::Vec;
#[cfg(feature = "std")]
pub use std::vec::Vec;
#[cfg(not(feature = "bytes_backend"))]
pub mod buffer;
pub mod interpreter;
pub use bebytes_derive::BeBytes;
#[doc(hidden)]
pub use interpreter::{StringInterpreter, Utf8};
#[cfg(feature = "bytes_backend")]
pub use bytes::buf::BufMut;
#[cfg(feature = "bytes_backend")]
pub use bytes::{Bytes, BytesMut};
#[cfg(not(feature = "bytes_backend"))]
pub use buffer::{BufMut, Bytes, BytesMut};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BeBytesError {
EmptyBuffer,
InsufficientData {
expected: usize,
actual: usize,
},
InvalidDiscriminant {
value: u8,
type_name: &'static str,
},
InvalidDiscriminantLarge {
value: u128,
type_name: &'static str,
},
InvalidBitField {
value: u128,
max: u128,
field: &'static str,
},
InvalidUtf8 {
field: &'static str,
},
MarkerNotFound {
marker: u8,
field: &'static str,
},
InvalidChar {
value: u32,
},
}
impl core::fmt::Display for BeBytesError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::EmptyBuffer => write!(f, "No bytes provided"),
Self::InsufficientData { expected, actual } => {
write!(f, "Not enough bytes: expected {expected}, got {actual}")
}
Self::InvalidDiscriminant { value, type_name } => {
write!(f, "Invalid discriminant {value} for type {type_name}")
}
Self::InvalidDiscriminantLarge { value, type_name } => {
write!(f, "Invalid discriminant {value} for type {type_name}")
}
Self::InvalidBitField { value, max, field } => {
write!(f, "Value {value} exceeds maximum {max} for field {field}")
}
Self::InvalidUtf8 { field } => {
write!(f, "Invalid UTF-8 sequence in field '{field}'")
}
Self::MarkerNotFound { marker, field } => {
write!(f, "Marker byte 0x{marker:02X} not found in field '{field}'")
}
Self::InvalidChar { value } => {
write!(f, "Invalid Unicode code point: 0x{value:08X}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for BeBytesError {}
pub trait BeBytes {
fn field_size() -> usize;
#[cfg(feature = "std")]
fn to_be_bytes(&self) -> std::vec::Vec<u8>;
#[cfg(not(feature = "std"))]
fn to_be_bytes(&self) -> alloc::vec::Vec<u8>;
fn try_from_be_bytes(bytes: &'_ [u8]) -> core::result::Result<(Self, usize), BeBytesError>
where
Self: Sized;
#[cfg(feature = "std")]
fn to_le_bytes(&self) -> std::vec::Vec<u8>;
#[cfg(not(feature = "std"))]
fn to_le_bytes(&self) -> alloc::vec::Vec<u8>;
fn to_be_bytes_buf(&self) -> Bytes {
Bytes::from(self.to_be_bytes())
}
fn to_le_bytes_buf(&self) -> Bytes {
Bytes::from(self.to_le_bytes())
}
fn try_from_le_bytes(bytes: &'_ [u8]) -> core::result::Result<(Self, usize), BeBytesError>
where
Self: Sized;
fn encode_be_to<B: BufMut>(&self, buf: &mut B) -> core::result::Result<(), BeBytesError> {
let bytes = self.to_be_bytes();
buf.put_slice(&bytes);
Ok(())
}
fn encode_le_to<B: BufMut>(&self, buf: &mut B) -> core::result::Result<(), BeBytesError> {
let bytes = self.to_le_bytes();
buf.put_slice(&bytes);
Ok(())
}
}