fit-encoder 0.1.2

Write FIT activity files (the Flexible and Interoperable Data Transfer format) as a stream: file header first, CRC last, sizes known in advance. no_std, no allocation. Independent; not affiliated with Garmin
Documentation
//! Raw fields: a field the crate does not model, given by its number, base type and bytes.

use crate::developer::{Extra, ExtraRepr, Scalar};
use crate::types::BaseType;

/// The most raw fields one message carries.
pub const RAW_FIELDS_MAX: usize = 16;

/// A field of the FIT profile the crate does not model, by its number in its message, for
/// [`Writer::raw_message`](crate::Writer::raw_message) or, as an [`Extra`], for
/// [`Writer::record_with`](crate::Writer::record_with). The encoder checks only that its bytes
/// fit its base type and are not "none"; their meaning is the caller's.
///
/// ```
/// use fit_encoder::{BaseType, RawField};
///
/// let breaths = RawField::number(108, 1850u16); // enhanced_respiration_rate, 18.5/min
/// let gear = RawField::enumeration(0, 43); // event: rear_gear_change
/// let zones = RawField::bytes(1, BaseType::UINT8, &[120, 140, 160, 175]);
/// # let _ = (breaths, gear, zones);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RawField<'a> {
    pub(crate) num: u8,
    pub(crate) base: BaseType,
    pub(crate) value: RawValue<'a>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RawValue<'a> {
    /// A value of its base type, and that type's size.
    Number([u8; 8], u8),
    /// A string in a field of the size given.
    Str(&'a str, u8),
    Bytes(&'a [u8]),
}

impl<'a> RawField<'a> {
    /// Field `num` holding a number, of the base type `T`'s.
    #[must_use]
    pub fn number<T: Scalar>(num: u8, value: T) -> Self {
        let le = T::le(value);
        Self { num, base: BaseType::from_byte(T::BASE), value: RawValue::Number(le, T::SIZE) }
    }

    /// Field `num` holding an `enum` value.
    #[must_use]
    pub const fn enumeration(num: u8, value: u8) -> Self {
        let le = [value, 0, 0, 0, 0, 0, 0, 0];
        Self { num, base: BaseType::ENUM, value: RawValue::Number(le, 1) }
    }

    /// Field `num` holding a string with no NUL, in a field of `size` bytes with room for its
    /// own.
    #[must_use]
    pub const fn string(num: u8, value: &'a str, size: u8) -> Self {
        Self { num, base: BaseType::STRING, value: RawValue::Str(value, size) }
    }

    /// Field `num` holding `bytes` as they are: one value of `base`, or an array of them.
    #[must_use]
    pub const fn bytes(num: u8, base: BaseType, bytes: &'a [u8]) -> Self {
        Self { num, base, value: RawValue::Bytes(bytes) }
    }

    /// The field's size in bytes; 0 when too long for a field.
    pub(crate) fn size(&self) -> u8 {
        match self.value {
            RawValue::Number(_, size) | RawValue::Str(_, size) => size,
            RawValue::Bytes(b) => u8::try_from(b.len()).unwrap_or(0),
        }
    }
}

impl<'a> From<RawField<'a>> for Extra<'a> {
    fn from(field: RawField<'a>) -> Self {
        Extra(ExtraRepr::Raw(field))
    }
}