use std::borrow::Cow;
use xtce_model::{
BooleanExpr, Calibrator, CompareOp, Comparison, ComparisonValue, Condition, ContainerId,
DataEncoding, DiscreteLookup, EntryKind, FloatCoding, IntegerCoding, LocationReference,
MatchCriteria, Operand, ParamId, ParameterType, SizeSpec, StringDelimiter, TypeKind, XtceDb,
};
use crate::bits::{
BitCursor, ones_complement, sign_magnitude, swap_byte_order, twos_complement,
twos_complement_unmasked,
};
use crate::calibrate::{self, CalibrationInput};
use crate::charset;
use crate::error::DecodeError;
use crate::value::{DecodedPacket, EngValue, ParameterValue, RawValue};
const MAX_CONTAINER_DEPTH: usize = 64;
#[derive(Clone, Copy)]
pub struct Decoder<'db> {
db: &'db XtceDb,
root: ContainerId,
capacity_hint: usize,
}
impl<'db> Decoder<'db> {
pub fn new(db: &'db XtceDb) -> Result<Self, DecodeError> {
let root = db
.default_root_container()
.ok_or_else(|| DecodeError::AmbiguousRoot {
candidates: db.root_containers().len(),
})?;
Ok(Self::rooted(db, root))
}
pub fn with_root(db: &'db XtceDb, name: &str) -> Result<Self, DecodeError> {
let root = db
.find_container(name)
.ok_or_else(|| DecodeError::NoSuchContainer {
name: name.to_owned(),
})?;
Ok(Self::rooted(db, root))
}
fn rooted(db: &'db XtceDb, root: ContainerId) -> Self {
Self {
db,
root,
capacity_hint: longest_path_entries(db, root),
}
}
#[must_use]
pub fn db(&self) -> &'db XtceDb {
self.db
}
#[must_use]
pub fn root(&self) -> ContainerId {
self.root
}
pub fn decode<'p>(&self, data: &'p [u8]) -> Result<DecodedPacket<'db, 'p>, DecodeError> {
let mut packet = DecodedPacket::with_capacity(self.db, data, self.root, self.capacity_hint);
self.decode_into(&mut packet, data)?;
Ok(packet)
}
pub fn decode_into<'p>(
&self,
packet: &mut DecodedPacket<'db, 'p>,
data: &'p [u8],
) -> Result<(), DecodeError> {
packet.reset(data, self.root);
let mut cursor = BitCursor::new(data);
let mut current = self.root;
loop {
self.decode_container(current, &mut cursor, packet, 0)?;
let container = self
.db
.container(current)
.ok_or_else(|| DecodeError::dangling_container(current))?;
let mut matched: Option<ContainerId> = None;
let mut extra: Vec<ContainerId> = Vec::new();
for &inheritor in &container.inheritors {
if self.inheritor_matches(inheritor, packet)? {
match matched {
None => matched = Some(inheritor),
Some(_) => extra.push(inheritor),
}
}
}
if !extra.is_empty() {
let mut candidates: Vec<ContainerId> = matched.into_iter().collect();
candidates.extend(extra);
return Err(DecodeError::AmbiguousPacket {
container: self.container_name(current),
candidates: candidates
.into_iter()
.map(|id| self.container_name(id))
.collect(),
});
}
if let Some(next) = matched {
current = next;
} else {
if container.is_abstract {
return Err(DecodeError::UnrecognizedPacket {
container: self.container_name(current),
candidates: container
.inheritors
.iter()
.map(|&id| self.container_name(id))
.collect(),
});
}
break;
}
}
packet.set_container(current);
packet.set_bits_consumed(cursor.position());
Ok(())
}
#[must_use]
pub fn new_packet<'p>(&self, data: &'p [u8]) -> DecodedPacket<'db, 'p> {
DecodedPacket::with_capacity(self.db, data, self.root, self.capacity_hint)
}
fn decode_container<'p>(
&self,
id: ContainerId,
cursor: &mut BitCursor<'p>,
packet: &mut DecodedPacket<'db, 'p>,
depth: usize,
) -> Result<(), DecodeError> {
if depth > MAX_CONTAINER_DEPTH {
return Err(DecodeError::Unsupported {
element: "ContainerRefEntry".to_owned(),
context: format!(
"{} (nesting exceeded {MAX_CONTAINER_DEPTH} levels; the entry list is probably cyclic)",
self.container_name(id)
),
});
}
let container_start = cursor.position();
for entry in self.db.container_entries(id) {
if let Some(location) = entry.location {
let base = match location.reference {
LocationReference::PreviousEntry => cursor.position(),
LocationReference::ContainerStart => container_start,
LocationReference::ContainerEnd | LocationReference::NextEntry => {
return Err(DecodeError::Unsupported {
element: "LocationInContainerInBits".to_owned(),
context: format!(
"{} (referenceLocation containerEnd/nextEntry needs a size this \
decoder does not know until the container is finished)",
self.container_name(id)
),
});
}
};
let target = i64::try_from(base).unwrap_or(i64::MAX) + location.offset_in_bits;
cursor.seek(usize::try_from(target).unwrap_or(0));
}
for _ in 0..entry.repeat.unwrap_or(1) {
match entry.kind {
EntryKind::Parameter(parameter) => {
self.decode_parameter(parameter, cursor, packet)?;
}
EntryKind::Container(child) => {
self.decode_container(child, cursor, packet, depth + 1)?;
}
EntryKind::FixedValue { size_in_bits, .. } => {
cursor
.skip(size_in_bits as usize)
.map_err(|error| DecodeError::Bits {
parameter: format!(
"<FixedValueEntry> in {}",
self.container_name(id)
),
source: error,
})?;
}
EntryKind::Unsupported { element } => {
return Err(DecodeError::Unsupported {
element: self.db.name(element).to_owned(),
context: format!("entry list of {}", self.container_name(id)),
});
}
}
}
}
Ok(())
}
fn decode_parameter<'p>(
&self,
id: ParamId,
cursor: &mut BitCursor<'p>,
packet: &mut DecodedPacket<'db, 'p>,
) -> Result<(), DecodeError> {
let parameter = self
.db
.parameter(id)
.ok_or_else(|| DecodeError::dangling_parameter(id))?;
let name = self.db.name(parameter.name);
let ty = self
.db
.parameter_type(parameter.type_id)
.ok_or(DecodeError::DanglingIndex {
what: "parameter type",
})?;
let bit_offset = cursor.position();
let (raw, eng) = self.decode_value(ty, name, cursor, packet)?;
packet.insert(ParameterValue {
parameter: id,
raw,
eng,
bit_offset,
bit_width: cursor.position().saturating_sub(bit_offset),
});
Ok(())
}
fn decode_value<'p>(
&self,
ty: &'db ParameterType,
name: &'db str,
cursor: &mut BitCursor<'p>,
packet: &DecodedPacket<'db, 'p>,
) -> Result<(RawValue<'p>, EngValue<'db, 'p>), DecodeError> {
if let TypeKind::Unsupported { element } = ty.kind {
return Err(DecodeError::Unsupported {
element: self.db.name(element).to_owned(),
context: format!("parameter {name}"),
});
}
if matches!(ty.kind, TypeKind::String) {
let raw = self.read_string_raw(ty, name, cursor, packet)?;
let RawValue::Bytes(buffer) = raw else {
return Err(DecodeError::DanglingIndex {
what: "string buffer",
});
};
let text = match &buffer {
Cow::Borrowed(slice) => Self::decode_string(ty, name, slice)?,
Cow::Owned(owned) => Cow::Owned(Self::decode_string(ty, name, owned)?.into_owned()),
};
return Ok((RawValue::Bytes(buffer), EngValue::Text(text)));
}
let raw = self.read_raw(ty, name, cursor, packet)?;
match &ty.kind {
TypeKind::Enumerated(list) => {
let value = raw
.as_i128()
.ok_or_else(|| DecodeError::UnknownEnumeration {
parameter: name.to_owned(),
value: 0,
})?;
let label =
list.label_for(value)
.ok_or_else(|| DecodeError::UnknownEnumeration {
parameter: name.to_owned(),
value,
})?;
return Ok((raw, EngValue::Label(self.db.name(label))));
}
TypeKind::Boolean { .. } => {
let truthy = match &raw {
RawValue::Unsigned(value) => *value != 0,
RawValue::Signed(value) => *value != 0,
RawValue::Float(value) => *value != 0.0,
RawValue::Bytes(bytes) => !bytes.is_empty(),
};
return Ok((raw, EngValue::Bool(truthy)));
}
_ => {}
}
let eng = self.calibrate(ty, name, &raw, packet)?;
Ok((raw, eng))
}
fn calibrate<'p>(
&self,
ty: &'db ParameterType,
name: &'db str,
raw: &RawValue<'p>,
packet: &DecodedPacket<'db, 'p>,
) -> Result<EngValue<'db, 'p>, DecodeError> {
let input = match raw {
RawValue::Unsigned(value) => CalibrationInput::Integer(i128::from(*value)),
RawValue::Signed(value) => CalibrationInput::Integer(i128::from(*value)),
RawValue::Float(value) => CalibrationInput::Float(*value),
RawValue::Bytes(bytes) => return Ok(EngValue::Bytes(bytes.clone())),
};
for context in ty.encoding.context_calibrators() {
let mut all = true;
for criteria in &context.criteria {
if !self.evaluate(criteria, packet, Some(raw), name)? {
all = false;
break;
}
}
if all {
return Ok(EngValue::Float(self.run_calibrator(
&context.calibrator,
input,
name,
)?));
}
}
if let Some(calibrator) = ty.encoding.default_calibrator() {
return Ok(EngValue::Float(
self.run_calibrator(calibrator, input, name)?,
));
}
Ok(match raw {
RawValue::Unsigned(value) => EngValue::Unsigned(*value),
RawValue::Signed(value) => EngValue::Signed(*value),
RawValue::Float(value) => EngValue::Float(*value),
RawValue::Bytes(bytes) => EngValue::Bytes(bytes.clone()),
})
}
fn run_calibrator(
&self,
calibrator: &Calibrator,
input: CalibrationInput,
name: &str,
) -> Result<f64, DecodeError> {
if let Calibrator::Unsupported { element } = calibrator {
return Err(DecodeError::Unsupported {
element: self.db.name(*element).to_owned(),
context: format!("calibrator for {name}"),
});
}
calibrate::apply(calibrator, input).map_err(|error| DecodeError::Calibration {
parameter: name.to_owned(),
reason: error.to_string(),
})
}
fn read_raw<'p>(
&self,
ty: &'db ParameterType,
name: &'db str,
cursor: &mut BitCursor<'p>,
packet: &DecodedPacket<'db, 'p>,
) -> Result<RawValue<'p>, DecodeError> {
match &ty.encoding {
DataEncoding::Integer(encoding) => {
let width = encoding.size_in_bits;
let mut bits = cursor
.read_uint(width)
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?;
let swapped = encoding.byte_order == xtce_model::ByteOrder::LeastSignificantFirst;
if swapped {
bits = swap_byte_order(bits, width);
}
Ok(match encoding.coding {
IntegerCoding::Unsigned => RawValue::Unsigned(bits),
IntegerCoding::TwosComplement if swapped => {
RawValue::Signed(twos_complement_unmasked(bits, width).ok_or_else(
|| DecodeError::Unsupported {
element: "IntegerDataEncoding".to_owned(),
context: format!(
"{name}: a {width}-bit little-endian two's-complement \
field can hold a value wider than an i64"
),
},
)?)
}
IntegerCoding::TwosComplement => RawValue::Signed(twos_complement(bits, width)),
IntegerCoding::SignMagnitude => RawValue::Signed(sign_magnitude(bits, width)),
IntegerCoding::OnesComplement => RawValue::Signed(ones_complement(bits, width)),
})
}
DataEncoding::Float(encoding) => {
let width = encoding.size_in_bits;
let mut bits = cursor
.read_uint(width)
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?;
if encoding.byte_order == xtce_model::ByteOrder::LeastSignificantFirst {
bits = swap_byte_order(bits, width);
}
let value = match encoding.coding {
FloatCoding::Ieee754 => {
ieee754(bits, width).ok_or_else(|| DecodeError::Unsupported {
element: "FloatDataEncoding".to_owned(),
context: format!("{name}: IEEE-754 at {width} bits"),
})?
}
FloatCoding::MilStd1750A if width != 32 => {
return Err(DecodeError::Unsupported {
element: "FloatDataEncoding".to_owned(),
context: format!("{name}: MIL-STD-1750A at {width} bits, not 32"),
});
}
FloatCoding::MilStd1750A => mil_std_1750a(bits),
};
Ok(RawValue::Float(value))
}
DataEncoding::Binary(encoding) => {
let width = self.resolve_size(&encoding.size, packet, name)?;
let bytes = cursor
.read_bytes(width)
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?;
Ok(RawValue::Bytes(bytes))
}
DataEncoding::String(_) => self.read_string_raw(ty, name, cursor, packet),
DataEncoding::None => Err(DecodeError::Unsupported {
element: "DataEncoding".to_owned(),
context: format!("{name} has no data encoding"),
}),
}
}
fn read_string_raw<'p>(
&self,
ty: &'db ParameterType,
name: &'db str,
cursor: &mut BitCursor<'p>,
packet: &DecodedPacket<'db, 'p>,
) -> Result<RawValue<'p>, DecodeError> {
let DataEncoding::String(encoding) = &ty.encoding else {
return Err(DecodeError::DanglingIndex {
what: "string encoding",
});
};
let width = self.resolve_size(&encoding.raw_size, packet, name)?;
let bytes = cursor
.read_bytes_left_aligned(width)
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?;
Ok(RawValue::Bytes(bytes))
}
fn decode_string<'p>(
ty: &ParameterType,
name: &str,
buffer: &'p [u8],
) -> Result<Cow<'p, str>, DecodeError> {
let DataEncoding::String(encoding) = &ty.encoding else {
return Ok(Cow::Borrowed(""));
};
let text_bytes: Cow<'p, [u8]> = match &encoding.delimiter {
StringDelimiter::WholeBuffer => Cow::Borrowed(buffer),
StringDelimiter::TerminationChar(terminator) => {
let end = find_subslice(buffer, terminator).ok_or_else(|| {
DecodeError::UnterminatedString {
parameter: name.to_owned(),
bytes: buffer.len(),
}
})?;
Cow::Borrowed(buffer.get(..end).unwrap_or_default())
}
StringDelimiter::LeadingSize { size_in_bits } => {
let mut prefix = BitCursor::new(buffer);
let length_bits =
prefix
.read_uint(*size_in_bits)
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?;
if length_bits % 8 != 0 {
return Err(DecodeError::BadFieldSize {
parameter: name.to_owned(),
bits: i64::try_from(length_bits).unwrap_or(i64::MAX),
});
}
prefix
.read_bytes(usize::try_from(length_bits).unwrap_or(0))
.map_err(|source| DecodeError::Bits {
parameter: name.to_owned(),
source,
})?
}
};
let invalid = |len: usize| DecodeError::InvalidText {
parameter: name.to_owned(),
charset: charset::name(encoding.charset),
bytes: len,
};
match text_bytes {
Cow::Borrowed(slice) => charset::decode(slice, encoding.charset, encoding.byte_order)
.map_err(|_| invalid(slice.len())),
Cow::Owned(owned) => charset::decode(&owned, encoding.charset, encoding.byte_order)
.map(|text| Cow::Owned(text.into_owned()))
.map_err(|_| invalid(owned.len())),
}
}
fn resolve_size<'p>(
&self,
spec: &SizeSpec,
packet: &DecodedPacket<'db, 'p>,
name: &str,
) -> Result<usize, DecodeError> {
let bits: i64 = match spec {
SizeSpec::Fixed(bits) => i64::from(*bits),
SizeSpec::Dynamic {
parameter,
use_calibrated,
adjustment,
} => {
let value =
packet
.get(*parameter)
.ok_or_else(|| DecodeError::ParameterNotYetDecoded {
context: "dynamic field size",
parameter: self.parameter_name(*parameter),
})?;
let integral = if *use_calibrated {
value.eng.as_i128()
} else {
value.raw.as_i128()
};
if let (Some(exact), None) = (integral, adjustment) {
i64::try_from(exact).unwrap_or(i64::MAX)
} else {
{
let numeric = if *use_calibrated {
value.eng.as_f64()
} else {
value.raw.as_f64()
}
.ok_or_else(|| DecodeError::BadFieldSize {
parameter: name.to_owned(),
bits: 0,
})?;
let adjusted = adjustment.map_or(numeric, |a| a.apply(numeric));
adjusted as i64
}
}
}
SizeSpec::DiscreteLookup(lookups) => self.discrete_lookup(lookups, packet, name)?,
SizeSpec::Unsupported { element } => {
return Err(DecodeError::Unsupported {
element: self.db.name(*element).to_owned(),
context: format!("size of {name}"),
});
}
};
usize::try_from(bits).map_err(|_| DecodeError::BadFieldSize {
parameter: name.to_owned(),
bits,
})
}
fn discrete_lookup<'p>(
&self,
lookups: &[DiscreteLookup],
packet: &DecodedPacket<'db, 'p>,
name: &str,
) -> Result<i64, DecodeError> {
for lookup in lookups {
let mut all = true;
for criteria in &lookup.criteria {
if !self.evaluate(criteria, packet, None, name)? {
all = false;
break;
}
}
if all {
return Ok(lookup.value);
}
}
Err(DecodeError::NoDiscreteLookupMatch {
parameter: name.to_owned(),
})
}
fn inheritor_matches(
&self,
id: ContainerId,
packet: &DecodedPacket<'db, '_>,
) -> Result<bool, DecodeError> {
let container = self
.db
.container(id)
.ok_or_else(|| DecodeError::dangling_container(id))?;
for criteria in &container.restriction {
if !self.evaluate(criteria, packet, None, self.db.name(container.name))? {
return Ok(false);
}
}
Ok(true)
}
fn evaluate(
&self,
criteria: &MatchCriteria,
packet: &DecodedPacket<'db, '_>,
current: Option<&RawValue<'_>>,
context: &str,
) -> Result<bool, DecodeError> {
match criteria {
MatchCriteria::Comparison(comparison) => {
self.evaluate_comparison(comparison, packet, current)
}
MatchCriteria::Boolean(expr) => self.evaluate_boolean(expr, packet),
MatchCriteria::Unsupported { element } => Err(DecodeError::Unsupported {
element: self.db.name(*element).to_owned(),
context: format!("match criteria of {context}"),
}),
}
}
fn evaluate_comparison(
&self,
comparison: &Comparison,
packet: &DecodedPacket<'db, '_>,
current: Option<&RawValue<'_>>,
) -> Result<bool, DecodeError> {
let literal_text = self.db.name(comparison.value.text);
let operator = comparison.operator;
let outcome = match packet.get(comparison.parameter) {
Some(value) => {
let scalar = if comparison.use_calibrated {
Scalar::from_eng(&value.eng)
} else {
Scalar::from_raw(&value.raw)
};
test_literal(scalar, operator, &comparison.value, literal_text)
}
None => match current {
Some(raw) => test_literal(
Scalar::from_raw(raw),
operator,
&comparison.value,
literal_text,
),
None => {
return Err(DecodeError::ParameterNotYetDecoded {
context: "comparison",
parameter: self.parameter_name(comparison.parameter),
});
}
},
};
outcome.map_err(|failure| DecodeError::IncomparableValue {
parameter: self.parameter_name(comparison.parameter),
value_kind: failure.value_kind,
literal: literal_text.to_owned(),
})
}
fn evaluate_boolean(
&self,
expr: &BooleanExpr,
packet: &DecodedPacket<'db, '_>,
) -> Result<bool, DecodeError> {
match expr {
BooleanExpr::Condition(condition) => self.evaluate_condition(condition, packet),
BooleanExpr::And(children) => {
for child in children {
if !self.evaluate_boolean(child, packet)? {
return Ok(false);
}
}
Ok(true)
}
BooleanExpr::Or(children) => {
for child in children {
if self.evaluate_boolean(child, packet)? {
return Ok(true);
}
}
Ok(false)
}
}
}
fn evaluate_condition(
&self,
condition: &Condition,
packet: &DecodedPacket<'db, '_>,
) -> Result<bool, DecodeError> {
let left = self.operand_value(condition.left, packet)?;
let outcome = match condition.right {
Operand::Literal(literal) => test_literal(
left,
condition.operator,
&literal,
self.db.name(literal.text),
),
Operand::Parameter { .. } => test_scalars(
left,
condition.operator,
self.operand_value(condition.right, packet)?,
),
};
outcome.map_err(|failure| DecodeError::IncomparableValue {
parameter: self.operand_name(condition.left),
value_kind: failure.value_kind,
literal: self.operand_name(condition.right),
})
}
fn operand_value<'v>(
&self,
operand: Operand,
packet: &'v DecodedPacket<'db, '_>,
) -> Result<Scalar<'v>, DecodeError>
where
'db: 'v,
{
match operand {
Operand::Parameter {
parameter,
use_calibrated,
} => {
let value =
packet
.get(parameter)
.ok_or_else(|| DecodeError::ParameterNotYetDecoded {
context: "condition",
parameter: self.parameter_name(parameter),
})?;
Ok(if use_calibrated {
Scalar::from_eng(&value.eng)
} else {
Scalar::from_raw(&value.raw)
})
}
Operand::Literal(literal) => Ok(Scalar::Text(self.db.name(literal.text))),
}
}
fn operand_name(&self, operand: Operand) -> String {
match operand {
Operand::Parameter { parameter, .. } => self.parameter_name(parameter),
Operand::Literal(literal) => format!("{:?}", self.db.name(literal.text)),
}
}
fn container_name(&self, id: ContainerId) -> String {
self.db
.container(id)
.map_or_else(|| "?".to_owned(), |c| self.db.name(c.name).to_owned())
}
fn parameter_name(&self, id: ParamId) -> String {
self.db
.parameter(id)
.map_or_else(|| "?".to_owned(), |p| self.db.name(p.name).to_owned())
}
}
fn longest_path_entries(db: &XtceDb, root: ContainerId) -> usize {
let mut expanded: Vec<Option<usize>> = vec![None; db.containers().len()];
let mut deepest: Vec<Option<usize>> = vec![None; db.containers().len()];
walk(db, root, 0, &mut expanded, &mut deepest)
}
fn own_entries(db: &XtceDb, id: ContainerId, depth: usize, memo: &mut Vec<Option<usize>>) -> usize {
if depth > MAX_CONTAINER_DEPTH {
return 0;
}
if let Some(Some(cached)) = memo.get(id.index()) {
return *cached;
}
if let Some(slot) = memo.get_mut(id.index()) {
*slot = Some(0);
}
let total = db
.container_entries(id)
.iter()
.map(|entry| {
let repeat = entry.repeat.unwrap_or(1) as usize;
match entry.kind {
EntryKind::Container(child) => own_entries(db, child, depth + 1, memo) * repeat,
_ => repeat,
}
})
.sum();
if let Some(slot) = memo.get_mut(id.index()) {
*slot = Some(total);
}
total
}
fn walk(
db: &XtceDb,
id: ContainerId,
depth: usize,
expanded: &mut Vec<Option<usize>>,
deepest: &mut Vec<Option<usize>>,
) -> usize {
if depth > MAX_CONTAINER_DEPTH {
return 0;
}
if let Some(Some(cached)) = deepest.get(id.index()) {
return *cached;
}
if let Some(slot) = deepest.get_mut(id.index()) {
*slot = Some(0);
}
let here = own_entries(db, id, 0, expanded);
let below = db.container(id).map_or(0, |container| {
container
.inheritors
.iter()
.map(|&child| walk(db, child, depth + 1, expanded, deepest))
.max()
.unwrap_or(0)
});
let total = here + below;
if let Some(slot) = deepest.get_mut(id.index()) {
*slot = Some(total);
}
total
}
#[derive(Clone, Copy, Debug)]
enum Scalar<'a> {
Integer(i128),
Float(f64),
Text(&'a str),
Opaque(&'static str),
}
impl<'a> Scalar<'a> {
fn from_raw(raw: &'a RawValue<'_>) -> Self {
match raw {
RawValue::Unsigned(value) => Self::Integer(i128::from(*value)),
RawValue::Signed(value) => Self::Integer(i128::from(*value)),
RawValue::Float(value) => Self::Float(*value),
RawValue::Bytes(_) => Self::Opaque("binary"),
}
}
fn from_eng(eng: &'a EngValue<'_, '_>) -> Self {
match eng {
EngValue::Unsigned(value) => Self::Integer(i128::from(*value)),
EngValue::Signed(value) => Self::Integer(i128::from(*value)),
EngValue::Bool(value) => Self::Integer(i128::from(*value)),
EngValue::Float(value) => Self::Float(*value),
EngValue::Label(text) => Self::Text(text),
EngValue::Text(text) => Self::Text(text.as_ref()),
EngValue::Bytes(_) => Self::Opaque("binary"),
}
}
const fn kind(self) -> &'static str {
match self {
Self::Integer(_) => "integer",
Self::Float(_) => "float",
Self::Text(_) => "text",
Self::Opaque(kind) => kind,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct Incomparable {
value_kind: &'static str,
}
const fn unordered(operator: CompareOp) -> bool {
matches!(operator, CompareOp::NotEqual)
}
fn test_literal(
value: Scalar<'_>,
operator: CompareOp,
literal: &ComparisonValue,
text: &str,
) -> Result<bool, Incomparable> {
let incomparable = Incomparable {
value_kind: value.kind(),
};
match value {
Scalar::Integer(left) => match literal.as_int {
Some(right) => Ok(operator.matches(left.cmp(&right))),
None => Err(incomparable),
},
Scalar::Float(left) => match literal.as_float {
Some(right) => Ok(ordering(operator, left.partial_cmp(&right))),
None => Err(incomparable),
},
Scalar::Text(left) => Ok(operator.matches(left.cmp(text))),
Scalar::Opaque(_) => Err(incomparable),
}
}
fn test_scalars(
left: Scalar<'_>,
operator: CompareOp,
right: Scalar<'_>,
) -> Result<bool, Incomparable> {
match (left, right) {
(Scalar::Integer(a), Scalar::Integer(b)) => Ok(operator.matches(a.cmp(&b))),
(Scalar::Float(a), Scalar::Float(b)) => Ok(ordering(operator, a.partial_cmp(&b))),
(Scalar::Integer(a), Scalar::Float(b)) => {
Ok(ordering(operator, (a as f64).partial_cmp(&b)))
}
(Scalar::Float(a), Scalar::Integer(b)) => {
Ok(ordering(operator, a.partial_cmp(&(b as f64))))
}
(Scalar::Text(a), Scalar::Text(b)) => Ok(operator.matches(a.cmp(b))),
_ => match operator {
CompareOp::Equal => Ok(false),
CompareOp::NotEqual => Ok(true),
_ => Err(Incomparable {
value_kind: left.kind(),
}),
},
}
}
fn ordering(operator: CompareOp, ordering: Option<std::cmp::Ordering>) -> bool {
match ordering {
Some(ordering) => operator.matches(ordering),
None => unordered(operator),
}
}
fn ieee754(bits: u64, width: u32) -> Option<f64> {
match width {
16 => Some(f64::from(half_to_f32(bits as u16))),
32 => Some(f64::from(f32::from_bits(bits as u32))),
64 => Some(f64::from_bits(bits)),
_ => None,
}
}
fn half_to_f32(bits: u16) -> f32 {
let sign = u32::from(bits >> 15) << 31;
let exponent = u32::from((bits >> 10) & 0x1F);
let fraction = u32::from(bits & 0x03FF);
match exponent {
0 if fraction == 0 => f32::from_bits(sign),
0 => {
let shift = fraction.leading_zeros() - 21;
let exponent = 113 - shift;
let fraction = (fraction << shift) & 0x03FF;
f32::from_bits(sign | (exponent << 23) | (fraction << 13))
}
0x1F => f32::from_bits(sign | 0x7F80_0000 | (fraction << 13)),
_ => f32::from_bits(sign | ((exponent + 127 - 15) << 23) | (fraction << 13)),
}
}
fn mil_std_1750a(bits: u64) -> f64 {
let word = bits as u32;
let exponent = twos_complement(u64::from(word & 0xFF), 8);
let mantissa = twos_complement(u64::from((word >> 8) & 0x00FF_FFFF), 24);
(mantissa as f64) * 2f64.powi(i32::try_from(exponent).unwrap_or(0) - 23)
}
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.is_empty() {
return Some(0);
}
haystack
.windows(needle.len())
.position(|window| window == needle)
}
#[cfg(test)]
mod tests {
use super::*;
use xtce_model::NameId;
fn half_reference(bits: u16) -> f64 {
let sign = if bits >> 15 == 1 { -1.0 } else { 1.0 };
let exponent = i32::from((bits >> 10) & 0x1F);
let fraction = f64::from(bits & 0x03FF);
match exponent {
0 => sign * fraction * 2f64.powi(-24),
0x1F => {
if fraction == 0.0 {
sign * f64::INFINITY
} else {
f64::NAN
}
}
_ => sign * (1.0 + fraction / 1024.0) * 2f64.powi(exponent - 15),
}
}
#[test]
fn half_precision_matches_the_ieee_definition_for_every_encoding() {
for bits in 0u16..=u16::MAX {
let got = f64::from(half_to_f32(bits));
let want = half_reference(bits);
if want.is_nan() {
assert!(got.is_nan(), "bits {bits:#06x}: expected NaN, got {got}");
} else {
assert_eq!(got.to_bits(), want.to_bits(), "bits {bits:#06x}");
}
}
}
#[test]
fn half_precision_subnormals() {
assert_eq!(f64::from(half_to_f32(0x0001)), 2f64.powi(-24));
assert_eq!(f64::from(half_to_f32(0x0002)), 2f64.powi(-23));
assert_eq!(f64::from(half_to_f32(0x03FF)), 1023.0 * 2f64.powi(-24));
assert_eq!(f64::from(half_to_f32(0x0400)), 2f64.powi(-14));
assert_eq!(f64::from(half_to_f32(0x8001)), -(2f64.powi(-24)));
}
#[test]
fn half_precision_specials() {
assert_eq!(f64::from(half_to_f32(0x0000)), 0.0);
assert!(f64::from(half_to_f32(0x8000)).is_sign_negative());
assert_eq!(f64::from(half_to_f32(0x3C00)), 1.0);
assert_eq!(f64::from(half_to_f32(0xC000)), -2.0);
assert!(f64::from(half_to_f32(0x7C00)).is_infinite());
assert!(f64::from(half_to_f32(0xFC00)).is_infinite());
assert!(f64::from(half_to_f32(0x7E00)).is_nan());
}
#[test]
fn mil_std_1750a_reference_values() {
assert_eq!(mil_std_1750a(0x4000_0000), 0.5);
assert_eq!(mil_std_1750a(0x4000_0001), 1.0);
assert_eq!(mil_std_1750a(0x4000_0004), 8.0);
assert_eq!(mil_std_1750a(0x8000_0000), -1.0);
assert_eq!(mil_std_1750a(0x0000_0000), 0.0);
assert_eq!(mil_std_1750a(0xBFFF_FFFF), -0.250_000_059_604_644_8);
assert_eq!(mil_std_1750a(0x8000_0080), -2f64.powi(-128));
assert_eq!(mil_std_1750a(0x4000_007F), 0.5 * 2f64.powi(127));
}
fn literal(text: &str) -> ComparisonValue {
ComparisonValue::new(NameId::ZERO, text)
}
#[test]
fn nan_is_unordered_exactly_as_python_treats_it() {
use CompareOp::{Equal, Greater, GreaterOrEqual, Less, LessOrEqual, NotEqual};
let nan = Scalar::Float(f64::NAN);
for (operator, expected) in [
(Equal, false),
(NotEqual, true),
(Less, false),
(LessOrEqual, false),
(Greater, false),
(GreaterOrEqual, false),
] {
assert_eq!(
test_literal(nan, operator, &literal("0"), "0"),
Ok(expected),
"{operator:?} against a literal"
);
assert_eq!(
test_scalars(nan, operator, Scalar::Float(f64::NAN)),
Ok(expected),
"{operator:?} against another NaN"
);
assert_eq!(
test_scalars(nan, operator, Scalar::Integer(0)),
Ok(expected),
"{operator:?} against an integer"
);
}
}
#[test]
fn negative_zero_equals_zero() {
assert_eq!(
test_literal(
Scalar::Float(-0.0),
CompareOp::Equal,
&literal("0.0"),
"0.0"
),
Ok(true)
);
assert_eq!(
test_scalars(Scalar::Float(-0.0), CompareOp::Equal, Scalar::Float(0.0)),
Ok(true)
);
}
#[test]
fn literals_are_coerced_to_the_value_they_meet() {
assert_eq!(
test_literal(Scalar::Integer(11), CompareOp::Equal, &literal("11"), "11"),
Ok(true)
);
assert!(
test_literal(Scalar::Integer(3), CompareOp::Equal, &literal("3.5"), "3.5").is_err()
);
assert_eq!(
test_literal(Scalar::Float(3.0), CompareOp::Equal, &literal("3"), "3"),
Ok(true)
);
assert_eq!(
test_literal(Scalar::Text("ON"), CompareOp::Equal, &literal("ON"), "ON"),
Ok(true)
);
assert_eq!(
test_literal(Scalar::Text("OFF"), CompareOp::Less, &literal("ON"), "ON"),
Ok(true)
);
assert!(
test_literal(
Scalar::Opaque("binary"),
CompareOp::Equal,
&literal("0"),
"0"
)
.is_err()
);
}
#[test]
fn text_against_a_number_follows_python() {
let text = Scalar::Text("ON");
let number = Scalar::Integer(5);
assert_eq!(test_scalars(text, CompareOp::Equal, number), Ok(false));
assert_eq!(test_scalars(text, CompareOp::NotEqual, number), Ok(true));
assert!(test_scalars(text, CompareOp::Less, number).is_err());
assert!(test_scalars(number, CompareOp::Greater, text).is_err());
}
#[test]
fn operators_over_an_ordering() {
use std::cmp::Ordering::{Equal, Greater, Less};
assert!(CompareOp::Equal.matches(Equal));
assert!(!CompareOp::Equal.matches(Less));
assert!(CompareOp::NotEqual.matches(Less));
assert!(CompareOp::NotEqual.matches(Greater));
assert!(!CompareOp::NotEqual.matches(Equal));
assert!(CompareOp::LessOrEqual.matches(Equal));
assert!(CompareOp::GreaterOrEqual.matches(Equal));
assert!(!CompareOp::Greater.matches(Equal));
}
}