Skip to main content

fit_encoder/
raw.rs

1//! Raw fields: a field the crate does not model, given by its number, base type and bytes.
2
3use crate::developer::{Extra, ExtraRepr, Scalar};
4use crate::types::BaseType;
5
6/// The most raw fields one message carries.
7pub const RAW_FIELDS_MAX: usize = 16;
8
9/// A field of the FIT profile the crate does not model, by its number in its message, for
10/// [`Writer::raw_message`](crate::Writer::raw_message) or, as an [`Extra`], for
11/// [`Writer::record_with`](crate::Writer::record_with). The encoder checks only that its bytes
12/// fit its base type and are not "none"; their meaning is the caller's.
13///
14/// ```
15/// use fit_encoder::{BaseType, RawField};
16///
17/// let breaths = RawField::number(108, 1850u16); // enhanced_respiration_rate, 18.5/min
18/// let gear = RawField::enumeration(0, 43); // event: rear_gear_change
19/// let zones = RawField::bytes(1, BaseType::UINT8, &[120, 140, 160, 175]);
20/// # let _ = (breaths, gear, zones);
21/// ```
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub struct RawField<'a> {
24    pub(crate) num: u8,
25    pub(crate) base: BaseType,
26    pub(crate) value: RawValue<'a>,
27}
28
29#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub(crate) enum RawValue<'a> {
31    /// A value of its base type, and that type's size.
32    Number([u8; 8], u8),
33    /// A string in a field of the size given.
34    Str(&'a str, u8),
35    Bytes(&'a [u8]),
36}
37
38impl<'a> RawField<'a> {
39    /// Field `num` holding a number, of the base type `T`'s.
40    #[must_use]
41    pub fn number<T: Scalar>(num: u8, value: T) -> Self {
42        let le = T::le(value);
43        Self { num, base: BaseType::from_byte(T::BASE), value: RawValue::Number(le, T::SIZE) }
44    }
45
46    /// Field `num` holding an `enum` value.
47    #[must_use]
48    pub const fn enumeration(num: u8, value: u8) -> Self {
49        let le = [value, 0, 0, 0, 0, 0, 0, 0];
50        Self { num, base: BaseType::ENUM, value: RawValue::Number(le, 1) }
51    }
52
53    /// Field `num` holding a string with no NUL, in a field of `size` bytes with room for its
54    /// own.
55    #[must_use]
56    pub const fn string(num: u8, value: &'a str, size: u8) -> Self {
57        Self { num, base: BaseType::STRING, value: RawValue::Str(value, size) }
58    }
59
60    /// Field `num` holding `bytes` as they are: one value of `base`, or an array of them.
61    #[must_use]
62    pub const fn bytes(num: u8, base: BaseType, bytes: &'a [u8]) -> Self {
63        Self { num, base, value: RawValue::Bytes(bytes) }
64    }
65
66    /// The field's size in bytes; 0 when too long for a field.
67    pub(crate) fn size(&self) -> u8 {
68        match self.value {
69            RawValue::Number(_, size) | RawValue::Str(_, size) => size,
70            RawValue::Bytes(b) => u8::try_from(b.len()).unwrap_or(0),
71        }
72    }
73}
74
75impl<'a> From<RawField<'a>> for Extra<'a> {
76    fn from(field: RawField<'a>) -> Self {
77        Extra(ExtraRepr::Raw(field))
78    }
79}