use std::fmt;
use crate::buffer::{ByteReader, ByteWriter};
use crate::error::DecoderError;
use crate::strings::HLAunicodeString;
use crate::typed::{BasicData, Decode, Encode, Enumerated, Representation};
macro_rules! basic_type {
(
$(#[$doc:meta])*
$name:ident($native:ty), $size:literal, $to_bytes:ident, $from_bytes:ident
$(, derive($($extra:ident),+))?
) => {
$(#[$doc])*
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default $($(, $extra)+)?)]
pub struct $name(pub $native);
impl $name {
pub const SIZE: usize = $size;
pub const fn new(value: $native) -> Self {
Self(value)
}
pub const fn get(self) -> $native {
self.0
}
}
impl From<$native> for $name {
fn from(value: $native) -> Self {
Self(value)
}
}
impl From<$name> for $native {
fn from(value: $name) -> Self {
value.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl Encode for $name {
const OCTET_BOUNDARY: usize = $size;
fn encode(&self, writer: &mut ByteWriter<'_>) {
writer.align($size);
writer.put_slice(&self.0.$to_bytes());
}
fn encoded_len(&self) -> usize {
$size
}
}
impl Decode for $name {
fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
reader.align($size)?;
Ok(Self(<$native>::$from_bytes(reader.take_array()?)))
}
}
impl BasicData for $name {
type Native = $native;
fn from_native(value: $native) -> Self {
Self(value)
}
fn to_native(self) -> $native {
self.0
}
}
impl Enumerated for $name {
type Representation = Self;
fn to_representation(&self) -> Self {
*self
}
fn from_representation(representation: &Self) -> Option<Self> {
Some(*representation)
}
}
impl Representation<$native> for $name {
const BOUNDARY: usize = $size;
fn encode_value(value: &$native, writer: &mut ByteWriter<'_>) {
Self(*value).encode(writer);
}
fn decode_value(reader: &mut ByteReader<'_>) -> Result<$native, DecoderError> {
Self::decode(reader).map(|value| value.0)
}
fn encoded_len_of(_value: &$native) -> usize {
$size
}
}
};
}
basic_type! {
HLAoctet(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAoctetPairBE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAoctetPairLE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger16BE(i16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger16LE(i16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger32BE(i32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger32LE(i32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger64BE(i64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAinteger64LE(i64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger8(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger16BE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger16LE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger32BE(u32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger32LE(u32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger64BE(u64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunsignedInteger64LE(u64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAfloat32BE(f32), 4, to_be_bytes, from_be_bytes
}
basic_type! {
HLAfloat32LE(f32), 4, to_le_bytes, from_le_bytes
}
basic_type! {
HLAfloat64BE(f64), 8, to_be_bytes, from_be_bytes
}
basic_type! {
HLAfloat64LE(f64), 8, to_le_bytes, from_le_bytes
}
basic_type! {
HLAASCIIchar(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAunicodeChar(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
HLAbyte(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct HLAboolean(pub bool);
impl HLAboolean {
pub const fn new(value: bool) -> Self {
Self(value)
}
pub const fn get(self) -> bool {
self.0
}
}
impl From<bool> for HLAboolean {
fn from(value: bool) -> Self {
Self(value)
}
}
impl From<HLAboolean> for bool {
fn from(value: HLAboolean) -> Self {
value.0
}
}
impl fmt::Display for HLAboolean {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(if self.0 { "HLAtrue" } else { "HLAfalse" })
}
}
impl Encode for HLAboolean {
const OCTET_BOUNDARY: usize = 4;
fn encode(&self, writer: &mut ByteWriter<'_>) {
HLAinteger32BE(i32::from(self.0)).encode(writer);
}
fn encoded_len(&self) -> usize {
4
}
}
impl Decode for HLAboolean {
fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
let raw = HLAinteger32BE::decode(reader)?;
Self::from_representation(&raw).ok_or_else(|| DecoderError::InvalidValue {
type_name: "HLAboolean",
detail: format!("{} is neither HLAfalse (0) nor HLAtrue (1)", raw.0),
})
}
}
impl Enumerated for HLAboolean {
type Representation = HLAinteger32BE;
fn to_representation(&self) -> HLAinteger32BE {
HLAinteger32BE(i32::from(self.0))
}
fn from_representation(representation: &HLAinteger32BE) -> Option<Self> {
match representation.0 {
0 => Some(Self(false)),
1 => Some(Self(true)),
_ => None,
}
}
}
impl Representation<bool> for HLAboolean {
const BOUNDARY: usize = 4;
fn encode_value(value: &bool, writer: &mut ByteWriter<'_>) {
Self(*value).encode(writer);
}
fn decode_value(reader: &mut ByteReader<'_>) -> Result<bool, DecoderError> {
Self::decode(reader).map(|value| value.0)
}
fn encoded_len_of(_value: &bool) -> usize {
4
}
}
pub type HLAcount = HLAinteger32BE;
pub type HLAseconds = HLAinteger32BE;
pub type HLAmsec = HLAinteger32BE;
pub type HLAindex = HLAinteger32BE;
pub type HLAnormalizedFederateHandle = HLAinteger64BE;
pub type HLAnormalizedObjectClassHandle = HLAinteger64BE;
pub type HLAnormalizedInteractionClassHandle = HLAinteger64BE;
pub type HLAnormalizedObjectInstanceHandle = HLAinteger64BE;
pub type HLAinteger64Time = HLAinteger64BE;
pub type HLAfloat64Time = HLAfloat64BE;
pub type HLAupdateRateName = HLAunicodeString;
pub type HLAtoken = [HLAbyte; 0];