ableos_userland/libraries/exi/src/types.rs

108 lines
2.2 KiB
Rust

pub struct List {
pub length: usize,
pub values: Vec<EXITypes>,
}
// https://www.w3.org/TR/exi/#encodingUnsignedInteger
pub struct NBitUnsignedInteger {
pub length: Option<Integer>,
pub bits: Vec<bool>,
}
impl NBitUnsignedInteger {
pub fn new(length: Integer) -> Self {
let c = if length.bits == vec![0] {
None
} else {
Some(length)
};
Self {
length: c,
bits: Vec::new(),
}
}
}
pub struct BinaryBlob {
pub length: Integer,
pub bytes: Vec<u8>,
}
pub struct Decimal {
pub lhs: UnsignedInteger,
pub rhs: UnsignedInteger,
}
pub enum EXITypes {
Binary(BinaryBlob),
Boolean(bool),
Decimal(Decimal),
Float(f64),
Integer(Integer),
UnsignedInteger(UnsignedInteger),
QName(String),
DateTime(DateTime),
NBitUnsignedInteger(NBitUnsignedInteger),
String(String),
List(List),
}
pub struct DateTime {
pub year: Integer,
pub month_day: NBitUnsignedInteger,
pub time: NBitUnsignedInteger,
pub fractional_secs: NBitUnsignedInteger,
pub timezone: NBitUnsignedInteger,
pub presence: bool,
}
impl DateTime {
pub fn new() -> Self {
Self {
year: Integer::new(),
month_day: NBitUnsignedInteger::new(9.into()),
time: NBitUnsignedInteger::new(17.into()),
fractional_secs: NBitUnsignedInteger::new(9.into()),
timezone: NBitUnsignedInteger::new(11.into()),
presence: false,
}
}
}
pub struct Integer {
pub negative: bool,
pub bits: Vec<u8>,
}
impl Integer {
pub fn new() -> Self {
Self {
negative: false,
bits: vec![0],
}
}
}
impl From<i8> for Integer {
fn from(value: i8) -> Self {
Self {
negative: value.is_negative(),
bits: value.abs().to_le_bytes().to_vec(),
}
}
}
pub struct UnsignedInteger {
pub bits: Vec<u8>,
}
impl UnsignedInteger {
pub fn new() -> Self {
Self { bits: vec![0] }
}
}
impl From<i8> for UnsignedInteger {
fn from(value: i8) -> Self {
Self {
bits: value.abs().to_le_bytes().to_vec(),
}
}
}