rustyfit 0.4.1

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
Documentation
// Code generated by fitgen/main.go. DO NOT EDIT.

// Copyright 2025 The RustyFIT Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]

use crate::profile::{ProfileType, typedef};
use crate::proto::*;

#[derive(Debug, Clone)]
/// NapEvent is a NapEvent message.
pub struct NapEvent {
    pub message_index: typedef::MessageIndex,
    pub timestamp: typedef::DateTime,
    /// Units: seconds
    pub start_time: typedef::DateTime,
    /// Units: minutes
    pub start_timezone_offset: i16,
    /// Units: seconds
    pub end_time: typedef::DateTime,
    /// Units: minutes
    pub end_timezone_offset: i16,
    pub feedback: typedef::NapPeriodFeedback,
    pub is_deleted: typedef::Bool,
    pub source: typedef::NapSource,
    /// The timestamp representing when this nap event was last updated
    pub update_timestamp: typedef::DateTime,
    /// unknown_fields are fields that are exist but they are not defined in Profile.xlsx
    pub unknown_fields: Vec<Field>,
    /// developer_fields are custom data fields (Added since protocol version 2.0)
    pub developer_fields: Vec<DeveloperField>,
}

impl NapEvent {
    /// Value's type: `u16`
    pub const MESSAGE_INDEX: u8 = 254;
    /// Value's type: `u32`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u32`; Units: `seconds`
    pub const START_TIME: u8 = 0;
    /// Value's type: `i16`; Units: `minutes`
    pub const START_TIMEZONE_OFFSET: u8 = 1;
    /// Value's type: `u32`; Units: `seconds`
    pub const END_TIME: u8 = 2;
    /// Value's type: `i16`; Units: `minutes`
    pub const END_TIMEZONE_OFFSET: u8 = 3;
    /// Value's type: `u8`
    pub const FEEDBACK: u8 = 4;
    /// Value's type: `u8`
    pub const IS_DELETED: u8 = 5;
    /// Value's type: `u8`
    pub const SOURCE: u8 = 6;
    /// Value's type: `u32`
    pub const UPDATE_TIMESTAMP: u8 = 7;

    /// Create new NapEvent with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            message_index: typedef::MessageIndex(u16::MAX),
            timestamp: typedef::DateTime(u32::MAX),
            start_time: typedef::DateTime(u32::MAX),
            start_timezone_offset: i16::MAX,
            end_time: typedef::DateTime(u32::MAX),
            end_timezone_offset: i16::MAX,
            feedback: typedef::NapPeriodFeedback(u8::MAX),
            is_deleted: typedef::Bool(u8::MAX),
            source: typedef::NapSource(u8::MAX),
            update_timestamp: typedef::DateTime(u32::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}

impl Default for NapEvent {
    fn default() -> Self {
        Self::new()
    }
}

impl From<&Message> for NapEvent {
    /// from creates new NapEvent struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 255] = [const { &Value::Invalid }; 255];

        const KNOWN_NUMS: [u64; 4] = [255, 0, 0, 6917529027641081856];
        let mut n = 0u64;
        for field in &mesg.fields {
            n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
        }
        let mut unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);

        for field in &mesg.fields {
            if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
                unknown_fields.push(field.clone());
                continue;
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            message_index: typedef::MessageIndex(vals[254].as_u16()),
            timestamp: typedef::DateTime(vals[253].as_u32()),
            start_time: typedef::DateTime(vals[0].as_u32()),
            start_timezone_offset: vals[1].as_i16(),
            end_time: typedef::DateTime(vals[2].as_u32()),
            end_timezone_offset: vals[3].as_i16(),
            feedback: typedef::NapPeriodFeedback(vals[4].as_u8()),
            is_deleted: typedef::Bool(vals[5].as_u8()),
            source: typedef::NapSource(vals[6].as_u8()),
            update_timestamp: typedef::DateTime(vals[7].as_u32()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<NapEvent> for Message {
    fn from(m: NapEvent) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 10];
        let mut len = 0usize;

        if m.message_index != typedef::MessageIndex(u16::MAX) {
            arr[len] = Field {
                num: 254,
                profile_type: ProfileType::MESSAGE_INDEX,
                value: Value::Uint16(m.message_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.timestamp != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 253,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.start_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.start_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.start_timezone_offset != i16::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SINT16,
                value: Value::Int16(m.start_timezone_offset),
                is_expanded: false,
            };
            len += 1;
        }
        if m.end_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.end_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.end_timezone_offset != i16::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::SINT16,
                value: Value::Int16(m.end_timezone_offset),
                is_expanded: false,
            };
            len += 1;
        }
        if m.feedback != typedef::NapPeriodFeedback(u8::MAX) {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::NAP_PERIOD_FEEDBACK,
                value: Value::Uint8(m.feedback.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.is_deleted != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.is_deleted.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.source != typedef::NapSource(u8::MAX) {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::NAP_SOURCE,
                value: Value::Uint8(m.source.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.update_timestamp != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.update_timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::NAP_EVENT,
            fields: {
                let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
                fields.extend_from_slice(&arr[..len]);
                fields.extend_from_slice(&m.unknown_fields);
                fields
            },
            developer_fields: m.developer_fields,
        }
    }
}