mod binary;
pub(crate) use binary::BoundedBuffer;
pub use binary::{BinaryDecodable, BinaryEncodable, Context, ContextOwned, DecodingOptions};
mod scalar;
pub use scalar::*;
mod value;
pub use value::*;
pub trait MessageInfo {
fn type_id(&self) -> ObjectId;
}
macro_rules! ua_enum {
($name:ident { $first:ident = $one:expr, $($member:ident = $number:expr,)* }) => {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(i32)]
pub enum $name {
#[default]
$first = $one,
$($member = $number,)*
}
impl BinaryEncodable for $name {
fn encode<W: std::io::Write + ?Sized>(&self, w: &mut W, c: &Context<'_>) -> Result<(), StatusCode> {
(*self as i32).encode(w, c)
}
}
impl BinaryDecodable for $name {
fn decode<R: std::io::Read + ?Sized>(r: &mut R, c: &Context<'_>) -> Result<Self, StatusCode> {
match i32::decode(r, c)? {
$one => Ok(Self::$first),
$($number => Ok(Self::$member),)*
_ => Err(StatusCode::BadDecodingError),
}
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
};
}
macro_rules! ua_struct {
($name:ident { $($field:ident : $ty:ty,)* }) => {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct $name { $(pub $field: $ty,)* }
impl BinaryEncodable for $name {
fn encode<W: std::io::Write + ?Sized>(&self,w: &mut W, c: &Context<'_>) -> Result<(), StatusCode> {
$(self.$field.encode(w, c)?;)*
Ok(())
}
}
impl BinaryDecodable for $name {
fn decode<R: std::io::Read + ?Sized>(r: &mut R, c: &Context<'_>) -> Result<Self, StatusCode> {
Ok(Self { $($field: <$ty>::decode(r, c)?,)* })
}
}
};
}
macro_rules! object_ids {
($($id:ident = $n:expr,)*) => {
#[allow(non_camel_case_types)]
#[repr(u32)]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum ObjectId { $($id=$n,)* }
impl TryFrom<u32> for ObjectId {
type Error = StatusCode;
fn try_from(id: u32) -> Result<Self, StatusCode> {
match id {
$($n => Ok(Self::$id),)*
_ => Err(StatusCode::BadServiceUnsupported),
}
}
}
};
}
macro_rules! message {
($name:ident, $id:ident) => {
impl MessageInfo for $name {
fn type_id(&self) -> ObjectId {
ObjectId::$id
}
}
};
}
mod generated;
use generated::decode_extension;
pub use generated::*;
pub type ContinuationPoint = ByteString;
impl ResponseHeader {
pub fn new_good(request_handle: u32) -> Self {
Self::new_service_result(request_handle, StatusCode::Good)
}
pub fn new_service_result(request_handle: u32, service_result: StatusCode) -> Self {
Self {
timestamp: DateTime::now(),
request_handle,
service_result,
..Default::default()
}
}
}