Skip to content

Commit

Permalink
string serialiser impl, and various misc tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
meadowsys committed Jul 28, 2024
1 parent a488f92 commit 8386337
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/serialiser_binary/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl Serialise for bool {
}

pub struct BoolSerialiser {
value: bool
val: bool
}

impl BoolSerialiser {
#[inline]
fn new(val: bool) -> Self {
Self { value: val }
Self { val }
}
}

Expand All @@ -36,7 +36,7 @@ impl<'h> Serialiser<'h> for BoolSerialiser {

// SAFETY: provided buffer is guaranteed by caller to have reserved
// at least the amount returned by `calc_needed_capacity` (1)
unsafe { buf.write_byte(self.value as u8 + 0xa0) }
unsafe { buf.write_byte(self.val as u8 + 0xa0) }
}
}

Expand Down
37 changes: 34 additions & 3 deletions src/serialiser_binary/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Clone, Debug)]
pub struct Error {
expected: &'static str,
found: &'static str
pub expected: &'static str,
pub found: &'static str
}

impl Error {
Expand All @@ -22,6 +22,18 @@ impl Error {
{
Err(self.into())
}

#[inline(always)]
pub fn expected(mut self, expected: &'static str) -> Self {
self.expected = expected;
self
}

#[inline(always)]
pub fn found(mut self, found: &'static str) -> Self {
self.found = found;
self
}
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -178,10 +190,26 @@ consts! {
DESC_EXPECTED_U128 = "an unsigned integer, 128 bit"
DESC_EXPECTED_I128 = "a signed integer, 128 bit"

#[cfg(target_pointer_width = "64")]
DESC_EXPECTED_USIZE = "an unsigned integer, word sized (64 bit)"
#[cfg(target_pointer_width = "32")]
DESC_EXPECTED_USIZE = "an unsigned integer, word sized (32 bit)"
#[cfg(target_pointer_width = "16")]
DESC_EXPECTED_USIZE = "an unsigned integer, word sized (16 bit)"

#[cfg(target_pointer_width = "64")]
DESC_EXPECTED_ISIZE = "a signed integer, word sized (64 bit)"
#[cfg(target_pointer_width = "32")]
DESC_EXPECTED_ISIZE = "a signed integer, word sized (32 bit)"
#[cfg(target_pointer_width = "16")]
DESC_EXPECTED_ISIZE = "a signed integer, word sized (16 bit)"

DESC_EXPECTED_BOOL = "a bool"

DESC_EXPECTED_F32 = "a floating point number, 32 bit"
DESC_EXPECTED_F64 = "a floating point number, 64 bit"

DESC_EXPECTED_STR = "an utf-8 encoded string"
}

consts! {
Expand All @@ -203,15 +231,18 @@ consts! {

DESC_FOUND_EOF = "eof"

DESC_FOUND_OVERFLOWING_INT = "an overflowing integer"
DESC_FOUND_SOMETHING_ELSE = "something else"
DESC_FOUND_TRAILING_BYTES = "trailing bytes"

DESC_FOUND_OVERFLOWING_INT = "an overflowing integer"

// DESC_FOUND_SMALLINT = "a smallint"
// DESC_FOUND_SMALLINT_NEGATIVE = "a negative smallint"

// DESC_FOUND_INT_NEGATIVE = "a negative integer"
// DESC_FOUND_INT_TOO_LARGE_U8 = "an integer that overflows unsigned 8 bits"
// DESC_FOUND_INT_TOO_LARGE_I8 = "an integer that overflows signed 8 bits"
// ...

DESC_FOUND_INVALID_UTF_8 = "invalid utf-8"
}
4 changes: 2 additions & 2 deletions src/serialiser_binary/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ consts! {

MARKER_NULL = 0xa7

MARKER_STRING_8 = 0xa8
MARKER_STRING_XL = 0xa9
MARKER_STR_8 = 0xa8
MARKER_STR_XL = 0xa9

MARKER_ARRAY_8 = 0xaa
MARKER_ARRAY_XL = 0xab
Expand Down
29 changes: 21 additions & 8 deletions src/serialiser_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod marker;

mod bool;
mod number;
mod string;

pub use self::bool::BoolSerialiser;
pub use self::number::{
Expand Down Expand Up @@ -270,6 +271,18 @@ impl<'h> Input<'h> for InputSliceBuffer<'h> {
}
}

macro_rules! use_marker {
($buf:ident) => {
use_ok!(
$buf.read_byte(),
#err err => err.expected(
$crate::serialiser_binary::expected::DESC_EXPECTED_MARKER
).wrap()
)
}
}
use use_marker;

macro_rules! use_ok {
($result:expr) => {
match $result {
Expand Down Expand Up @@ -339,14 +352,14 @@ macro_rules! consts {
$mod_vis:vis mod $mod_name:ident
$((
$(#[$meta:meta])*
$name:ident, $value:expr, $($type:tt)+
$name:ident, $val:expr, $($type:tt)+
))*
} => {
$(#[$mod_meta])*
$mod_vis mod $mod_name {
$(
$(#[$meta])*
pub const $name: $($type)+ = $value;
pub const $name: $($type)+ = $val;
)*
}
};
Expand All @@ -357,14 +370,14 @@ macro_rules! consts {
$mod_vis:vis mod $mod_name:ident
$((
$(#[$meta:meta])*
$name:ident, $value:expr, $($type:tt)+
$name:ident, $val:expr, $($type:tt)+
))*
} => {
$(#[$mod_meta])*
$mod_vis mod $mod_name {
$(
$(#[$meta])*
pub static $name: $($type)+ = $value;
pub static $name: $($type)+ = $val;
)*
}
};
Expand All @@ -373,27 +386,27 @@ macro_rules! consts {
const type u8
$(#[$mod_meta:meta])*
$mod_vis:vis mod $mod_name:ident
$($(#[$meta:meta])* $name:ident = $value:expr)*
$($(#[$meta:meta])* $name:ident = $val:expr)*
} => {
consts! {
@impl(const)
$(#[$mod_meta])*
$mod_vis mod $mod_name
$(($(#[$meta])* $name, $value, u8))*
$(($(#[$meta])* $name, $val, u8))*
}
};

{
static type &'static str
$(#[$mod_meta:meta])*
$mod_vis:vis mod $mod_name:ident
$($(#[$meta:meta])* $name:ident = $value:expr)*
$($(#[$meta:meta])* $name:ident = $val:expr)*
} => {
consts! {
@impl(static)
$(#[$mod_meta])*
$mod_vis mod $mod_name
$(($(#[$meta])* $name, $value, &'static str))*
$(($(#[$meta])* $name, $val, &'static str))*
}
};
}
Expand Down
Loading

0 comments on commit 8386337

Please sign in to comment.