rustyfit 0.5.0

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)]
/// WeatherConditions is a WeatherConditions message.
pub struct WeatherConditions {
    /// time of update for current conditions, else forecast time
    pub timestamp: typedef::DateTime,
    /// Current or forecast
    pub weather_report: typedef::WeatherReport,
    /// Units: C
    pub temperature: i8,
    /// Corresponds to GSC Response weatherIcon field
    pub condition: typedef::WeatherStatus,
    /// Units: degrees
    pub wind_direction: u16,
    /// Scale: 1000; Units: m/s
    pub wind_speed: u16,
    /// range 0-100
    pub precipitation_probability: u8,
    /// Units: C; Heat Index if GCS heatIdx above or equal to 90F or wind chill if GCS windChill below or equal to 32F
    pub temperature_feels_like: i8,
    pub relative_humidity: u8,
    /// string corresponding to GCS response location string
    pub location: String,
    pub observed_at_time: typedef::DateTime,
    /// Units: semicircles
    pub observed_location_lat: i32,
    /// Units: semicircles
    pub observed_location_long: i32,
    pub day_of_week: typedef::DayOfWeek,
    /// Units: C
    pub high_temperature: i8,
    /// Units: C
    pub low_temperature: i8,
    /// 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 WeatherConditions {
    /// Value's type: `u32`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`
    pub const WEATHER_REPORT: u8 = 0;
    /// Value's type: `i8`; Units: `C`
    pub const TEMPERATURE: u8 = 1;
    /// Value's type: `u8`
    pub const CONDITION: u8 = 2;
    /// Value's type: `u16`; Units: `degrees`
    pub const WIND_DIRECTION: u8 = 3;
    /// Value's type: `u16`; Scale: `1000`; Units: `m/s`
    pub const WIND_SPEED: u8 = 4;
    /// Value's type: `u8`
    pub const PRECIPITATION_PROBABILITY: u8 = 5;
    /// Value's type: `i8`; Units: `C`
    pub const TEMPERATURE_FEELS_LIKE: u8 = 6;
    /// Value's type: `u8`
    pub const RELATIVE_HUMIDITY: u8 = 7;
    /// Value's type: `String`
    pub const LOCATION: u8 = 8;
    /// Value's type: `u32`
    pub const OBSERVED_AT_TIME: u8 = 9;
    /// Value's type: `i32`; Units: `semicircles`
    pub const OBSERVED_LOCATION_LAT: u8 = 10;
    /// Value's type: `i32`; Units: `semicircles`
    pub const OBSERVED_LOCATION_LONG: u8 = 11;
    /// Value's type: `u8`
    pub const DAY_OF_WEEK: u8 = 12;
    /// Value's type: `i8`; Units: `C`
    pub const HIGH_TEMPERATURE: u8 = 13;
    /// Value's type: `i8`; Units: `C`
    pub const LOW_TEMPERATURE: u8 = 14;

    /// Create new WeatherConditions with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            weather_report: typedef::WeatherReport(u8::MAX),
            temperature: i8::MAX,
            condition: typedef::WeatherStatus(u8::MAX),
            wind_direction: u16::MAX,
            wind_speed: u16::MAX,
            precipitation_probability: u8::MAX,
            temperature_feels_like: i8::MAX,
            relative_humidity: u8::MAX,
            location: String::new(),
            observed_at_time: typedef::DateTime(u32::MAX),
            observed_location_lat: i32::MAX,
            observed_location_long: i32::MAX,
            day_of_week: typedef::DayOfWeek(u8::MAX),
            high_temperature: i8::MAX,
            low_temperature: i8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `wind_speed` in its scaled value. It returns invalid f64 when value is valid.
    pub fn wind_speed_scaled(&self) -> f64 {
        if self.wind_speed == u16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.wind_speed as f64 / 1000.0 - 0.0
    }

    /// Set `wind_speed` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_wind_speed_scaled(&mut self, v: f64) -> &mut WeatherConditions {
        let unscaled = (v + 0.0) * 1000.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
            self.wind_speed = u16::MAX;
            return self;
        }
        self.wind_speed = unscaled as u16;
        self
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [32767, 0, 0, 2305843009213693952];
        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 {
            timestamp: typedef::DateTime(vals[253].as_u32()),
            weather_report: typedef::WeatherReport(vals[0].as_u8()),
            temperature: vals[1].as_i8(),
            condition: typedef::WeatherStatus(vals[2].as_u8()),
            wind_direction: vals[3].as_u16(),
            wind_speed: vals[4].as_u16(),
            precipitation_probability: vals[5].as_u8(),
            temperature_feels_like: vals[6].as_i8(),
            relative_humidity: vals[7].as_u8(),
            location: vals[8].as_string(),
            observed_at_time: typedef::DateTime(vals[9].as_u32()),
            observed_location_lat: vals[10].as_i32(),
            observed_location_long: vals[11].as_i32(),
            day_of_week: typedef::DayOfWeek(vals[12].as_u8()),
            high_temperature: vals[13].as_i8(),
            low_temperature: vals[14].as_i8(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        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.weather_report != typedef::WeatherReport(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::WEATHER_REPORT,
                value: Value::Uint8(m.weather_report.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.temperature != i8::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::SINT8,
                value: Value::Int8(m.temperature),
                is_expanded: false,
            };
            len += 1;
        }
        if m.condition != typedef::WeatherStatus(u8::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::WEATHER_STATUS,
                value: Value::Uint8(m.condition.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wind_direction != u16::MAX {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.wind_direction),
                is_expanded: false,
            };
            len += 1;
        }
        if m.wind_speed != u16::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.wind_speed),
                is_expanded: false,
            };
            len += 1;
        }
        if m.precipitation_probability != u8::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.precipitation_probability),
                is_expanded: false,
            };
            len += 1;
        }
        if m.temperature_feels_like != i8::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::SINT8,
                value: Value::Int8(m.temperature_feels_like),
                is_expanded: false,
            };
            len += 1;
        }
        if m.relative_humidity != u8::MAX {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.relative_humidity),
                is_expanded: false,
            };
            len += 1;
        }
        if m.location != String::new() {
            arr[len] = Field {
                num: 8,
                profile_type: ProfileType::STRING,
                value: Value::String(m.location),
                is_expanded: false,
            };
            len += 1;
        }
        if m.observed_at_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 9,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.observed_at_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.observed_location_lat != i32::MAX {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.observed_location_lat),
                is_expanded: false,
            };
            len += 1;
        }
        if m.observed_location_long != i32::MAX {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::SINT32,
                value: Value::Int32(m.observed_location_long),
                is_expanded: false,
            };
            len += 1;
        }
        if m.day_of_week != typedef::DayOfWeek(u8::MAX) {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::DAY_OF_WEEK,
                value: Value::Uint8(m.day_of_week.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.high_temperature != i8::MAX {
            arr[len] = Field {
                num: 13,
                profile_type: ProfileType::SINT8,
                value: Value::Int8(m.high_temperature),
                is_expanded: false,
            };
            len += 1;
        }
        if m.low_temperature != i8::MAX {
            arr[len] = Field {
                num: 14,
                profile_type: ProfileType::SINT8,
                value: Value::Int8(m.low_temperature),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::WEATHER_CONDITIONS,
            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,
        }
    }
}