mdf4_rs/blocks/conversion/
logic.rs

1use super::{bitfield, linear, table_lookup, text};
2use crate::Result;
3use crate::blocks::conversion::base::ConversionBlock;
4use crate::blocks::conversion::types::ConversionType;
5use crate::types::DecodedValue;
6
7impl ConversionBlock {
8    /// Applies the conversion formula to a decoded channel value.
9    ///
10    /// Depending on the conversion type, this method either returns a numeric value
11    /// (wrapped as DecodedValue::Float) or a character string (wrapped as DecodedValue::String).
12    /// For non-numeric conversions such as Algebraic or Table look-ups, placeholder implementations
13    /// are provided and can be extended later.
14    ///
15    /// # Parameters
16    /// * `value`: The already-decoded channel value (as DecodedValue).
17    ///
18    /// # Returns
19    /// A DecodedValue where numeric conversion types yield a Float and string conversion types yield a String.
20    pub fn apply_decoded(&self, value: DecodedValue, file_data: &[u8]) -> Result<DecodedValue> {
21        match self.conversion_type {
22            ConversionType::Identity => Ok(value),
23            ConversionType::Linear => linear::apply_linear(self, value),
24            ConversionType::Rational => linear::apply_rational(self, value),
25            ConversionType::Algebraic => linear::apply_algebraic(self, value),
26            ConversionType::TableLookupInterp => {
27                table_lookup::apply_table_lookup(self, value, true)
28            }
29            ConversionType::TableLookupNoInterp => {
30                table_lookup::apply_table_lookup(self, value, false)
31            }
32            ConversionType::RangeLookup => table_lookup::apply_range_lookup(self, value),
33            ConversionType::ValueToText => text::apply_value_to_text(self, value, file_data),
34            ConversionType::RangeToText => text::apply_range_to_text(self, value, file_data),
35            ConversionType::TextToValue => text::apply_text_to_value(self, value, file_data),
36            ConversionType::TextToText => text::apply_text_to_text(self, value, file_data),
37            ConversionType::BitfieldText => bitfield::apply_bitfield_text(self, value, file_data),
38            ConversionType::Unknown(_) => Ok(value),
39        }
40    }
41}