mdf4_rs/
types.rs

1//! Shared types used across the library.
2//!
3//! This module contains types that are available with just the `alloc` feature,
4//! making them usable in both std and no_std environments.
5
6use alloc::string::String;
7use alloc::vec::Vec;
8
9/// An enum representing the decoded value of a channel sample.
10///
11/// This type represents all possible values that can be stored in an MDF channel.
12#[derive(Debug, Clone, PartialEq)]
13pub enum DecodedValue {
14    /// Unsigned integer (up to 64 bits)
15    UnsignedInteger(u64),
16    /// Signed integer (up to 64 bits)
17    SignedInteger(i64),
18    /// Floating point value (32 or 64 bit)
19    Float(f64),
20    /// Text string (UTF-8 or converted from Latin-1)
21    String(String),
22    /// Raw byte array
23    ByteArray(Vec<u8>),
24    /// MIME sample data
25    MimeSample(Vec<u8>),
26    /// MIME stream data
27    MimeStream(Vec<u8>),
28    /// Unknown or unsupported data type
29    Unknown,
30}
31
32impl DecodedValue {
33    /// Returns true if this is an integer value (signed or unsigned).
34    #[inline]
35    pub fn is_integer(&self) -> bool {
36        matches!(
37            self,
38            DecodedValue::UnsignedInteger(_) | DecodedValue::SignedInteger(_)
39        )
40    }
41
42    /// Returns true if this is a floating point value.
43    #[inline]
44    pub fn is_float(&self) -> bool {
45        matches!(self, DecodedValue::Float(_))
46    }
47
48    /// Returns true if this is a string value.
49    #[inline]
50    pub fn is_string(&self) -> bool {
51        matches!(self, DecodedValue::String(_))
52    }
53
54    /// Returns true if this is a byte array value.
55    #[inline]
56    pub fn is_bytes(&self) -> bool {
57        matches!(
58            self,
59            DecodedValue::ByteArray(_) | DecodedValue::MimeSample(_) | DecodedValue::MimeStream(_)
60        )
61    }
62
63    /// Attempts to convert to f64, useful for numeric operations.
64    pub fn as_f64(&self) -> Option<f64> {
65        match self {
66            DecodedValue::UnsignedInteger(v) => Some(*v as f64),
67            DecodedValue::SignedInteger(v) => Some(*v as f64),
68            DecodedValue::Float(v) => Some(*v),
69            _ => None,
70        }
71    }
72}
73
74impl core::fmt::Display for DecodedValue {
75    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76        match self {
77            DecodedValue::UnsignedInteger(v) => write!(f, "{}", v),
78            DecodedValue::SignedInteger(v) => write!(f, "{}", v),
79            DecodedValue::Float(v) => write!(f, "{}", v),
80            DecodedValue::String(s) => write!(f, "{}", s),
81            DecodedValue::ByteArray(b) => write!(f, "[{} bytes]", b.len()),
82            DecodedValue::MimeSample(b) => write!(f, "[MIME sample: {} bytes]", b.len()),
83            DecodedValue::MimeStream(b) => write!(f, "[MIME stream: {} bytes]", b.len()),
84            DecodedValue::Unknown => write!(f, "<unknown>"),
85        }
86    }
87}