ConversionBlock

Struct ConversionBlock 

Source
pub struct ConversionBlock {
Show 18 fields pub header: BlockHeader, pub name_addr: Option<u64>, pub unit_addr: Option<u64>, pub comment_addr: Option<u64>, pub inverse_addr: Option<u64>, pub refs: Vec<u64>, pub conversion_type: ConversionType, pub precision: u8, pub flags: u16, pub ref_count: u16, pub value_count: u16, pub phys_range_min: Option<f64>, pub phys_range_max: Option<f64>, pub values: Vec<f64>, pub formula: Option<String>, pub resolved_texts: Option<BTreeMap<usize, String>>, pub resolved_conversions: Option<BTreeMap<usize, Box<ConversionBlock>>>, pub default_conversion: Option<Box<ConversionBlock>>,
}

Fields§

§header: BlockHeader§name_addr: Option<u64>§unit_addr: Option<u64>§comment_addr: Option<u64>§inverse_addr: Option<u64>§refs: Vec<u64>§conversion_type: ConversionType§precision: u8§flags: u16§ref_count: u16§value_count: u16§phys_range_min: Option<f64>§phys_range_max: Option<f64>§values: Vec<f64>§formula: Option<String>§resolved_texts: Option<BTreeMap<usize, String>>

Pre-resolved text strings for text-based conversions (ValueToText, RangeToText, etc.) Maps refs indices to their resolved text content

§resolved_conversions: Option<BTreeMap<usize, Box<ConversionBlock>>>

Pre-resolved nested conversion blocks for chained conversions Maps refs indices to their resolved ConversionBlock content

§default_conversion: Option<Box<ConversionBlock>>

Default conversion for fallback cases (similar to asammdf’s “default_addr”) This is typically the last reference in refs for some conversion types

Implementations§

Source§

impl ConversionBlock

Source

pub fn resolve_all_dependencies(&mut self, file_data: &[u8]) -> Result<()>

Resolve all dependencies for this conversion block to make it self-contained. This reads referenced text blocks and nested conversions from the file data and stores them in the resolved_texts and resolved_conversions fields.

Supports arbitrary depth conversion chains with cycle detection.

§Arguments
  • file_data - Memory mapped MDF bytes used to read referenced data
§Returns

Ok(()) on success or an Error if resolution fails

Source

pub fn resolve_all_dependencies_with_address( &mut self, file_data: &[u8], current_address: u64, ) -> Result<()>

Resolve all dependencies with a known current block address (used internally)

Source

pub fn get_resolved_text(&self, ref_index: usize) -> Option<&String>

Get a resolved text string for a given refs index. Returns the text if it was resolved during dependency resolution.

Source

pub fn get_resolved_conversion( &self, ref_index: usize, ) -> Option<&ConversionBlock>

Get a resolved nested conversion for a given refs index. Returns the conversion block if it was resolved during dependency resolution.

Source

pub fn get_default_conversion(&self) -> Option<&ConversionBlock>

Get the default conversion for fallback cases. Returns the default conversion if it was resolved during dependency resolution.

Source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Serialize this conversion block back to bytes.

§Returns

A byte vector containing the encoded block or an Error if serialization fails.

Source

pub fn identity() -> Self

Creates an identity conversion (1:1, no change).

This is useful when you want to explicitly indicate that no conversion is applied, while still having a conversion block for consistency.

§Example
use mdf4_rs::blocks::ConversionBlock;

let conv = ConversionBlock::identity();
Source

pub fn linear(offset: f64, factor: f64) -> Self

Creates a linear conversion: physical = offset + factor * raw.

This is the most common conversion type, used for scaling and offset adjustments. The MDF 4.1 specification defines linear conversion as: y = P1 + P2 * x where P1 is the offset and P2 is the factor.

§Arguments
  • offset - The offset value (P1 in the MDF spec)
  • factor - The scaling factor (P2 in the MDF spec)
§Example
use mdf4_rs::blocks::ConversionBlock;

// Convert raw temperature: physical = -40.0 + 0.1 * raw
let temp_conv = ConversionBlock::linear(-40.0, 0.1);

// Convert RPM: physical = 0.0 + 0.25 * raw
let rpm_conv = ConversionBlock::linear(0.0, 0.25);
Source

pub fn rational(p1: f64, p2: f64, p3: f64, p4: f64, p5: f64, p6: f64) -> Self

Creates a rational conversion: physical = (P1 + P2*x + P3*x²) / (P4 + P5*x + P6*x²).

Rational conversions are used for more complex non-linear transformations.

§Arguments
  • p1 - Numerator constant term
  • p2 - Numerator linear coefficient
  • p3 - Numerator quadratic coefficient
  • p4 - Denominator constant term
  • p5 - Denominator linear coefficient
  • p6 - Denominator quadratic coefficient
§Example
use mdf4_rs::blocks::ConversionBlock;

// Simple linear via rational: physical = (0 + 2*x + 0*x²) / (1 + 0*x + 0*x²) = 2*x
let conv = ConversionBlock::rational(0.0, 2.0, 0.0, 1.0, 0.0, 0.0);
Source

pub fn is_identity(&self) -> bool

Check if this is a trivial identity conversion that can be omitted.

Returns true if:

  • The conversion type is Identity, OR
  • The conversion type is Linear with offset=0 and factor=1
Source

pub fn with_physical_range(self, min: f64, max: f64) -> Self

Set the physical range limits for this conversion.

§Arguments
  • min - Minimum physical value
  • max - Maximum physical value
Source§

impl ConversionBlock

Source

pub fn resolve_formula(&mut self, file_data: &[u8]) -> Result<()>

Resolve and store the algebraic formula referenced by this block.

§Arguments
  • file_data - Memory mapped MDF bytes used to read the formula text.
§Returns

Ok(()) on success or an crate::Error if the formula block cannot be read.

Source§

impl ConversionBlock

Source

pub fn apply_decoded( &self, value: DecodedValue, file_data: &[u8], ) -> Result<DecodedValue>

Applies the conversion formula to a decoded channel value.

Depending on the conversion type, this method either returns a numeric value (wrapped as DecodedValue::Float) or a character string (wrapped as DecodedValue::String). For non-numeric conversions such as Algebraic or Table look-ups, placeholder implementations are provided and can be extended later.

§Parameters
  • value: The already-decoded channel value (as DecodedValue).
§Returns

A DecodedValue where numeric conversion types yield a Float and string conversion types yield a String.

Trait Implementations§

Source§

impl BlockParse<'_> for ConversionBlock

Source§

const ID: &'static str = "##CC"

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

fn parse_header(bytes: &[u8]) -> Result<BlockHeader>

Source§

impl Clone for ConversionBlock

Source§

fn clone(&self) -> ConversionBlock

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConversionBlock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ConversionBlock

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ConversionBlock

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,