rustyfit 0.10.0

The #![no_std] Rust implementation of The Flexible and Interoperable Data Transfer (FIT) Protocol for decoding and encoding Garmin FIT files, supporting FIT Protocol V2.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// 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.

use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use crate::semconv;
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

/// Weather Conditions message.
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
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`; FitBaseType::UINT32; ProfileType::DateTime
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::WeatherReport
    pub const WEATHER_REPORT: u8 = 0;
    /// Value's type: `i8`; FitBaseType::SINT8; ProfileType::Sint8; Units: `C`
    pub const TEMPERATURE: u8 = 1;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::WeatherStatus
    pub const CONDITION: u8 = 2;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Units: `degrees`
    pub const WIND_DIRECTION: u8 = 3;
    /// Value's type: `u16`; FitBaseType::UINT16; ProfileType::Uint16; Scale: `1000`; Units: `m/s`
    pub const WIND_SPEED: u8 = 4;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const PRECIPITATION_PROBABILITY: u8 = 5;
    /// Value's type: `i8`; FitBaseType::SINT8; ProfileType::Sint8; Units: `C`
    pub const TEMPERATURE_FEELS_LIKE: u8 = 6;
    /// Value's type: `u8`; FitBaseType::UINT8; ProfileType::Uint8
    pub const RELATIVE_HUMIDITY: u8 = 7;
    /// Value's type: `String`; FitBaseType::STRING; ProfileType::String
    pub const LOCATION: u8 = 8;
    /// Value's type: `u32`; FitBaseType::UINT32; ProfileType::DateTime
    pub const OBSERVED_AT_TIME: u8 = 9;
    /// Value's type: `i32`; FitBaseType::SINT32; ProfileType::Sint32; Units: `semicircles`
    pub const OBSERVED_LOCATION_LAT: u8 = 10;
    /// Value's type: `i32`; FitBaseType::SINT32; ProfileType::Sint32; Units: `semicircles`
    pub const OBSERVED_LOCATION_LONG: u8 = 11;
    /// Value's type: `u8`; FitBaseType::ENUM; ProfileType::DayOfWeek
    pub const DAY_OF_WEEK: u8 = 12;
    /// Value's type: `i8`; FitBaseType::SINT8; ProfileType::Sint8; Units: `C`
    pub const HIGH_TEMPERATURE: u8 = 13;
    /// Value's type: `i8`; FitBaseType::SINT8; ProfileType::Sint8; 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 `observed_location_lat` in degrees instead of semicircles. It returns `None` when value is invalid.
    pub fn observed_location_lat_degrees(&self) -> Option<f64> {
        semconv::to_degrees(self.observed_location_lat)
    }

    /// Set `observed_location_lat` with a value in degrees instead of semicircles, the value will be converted to semicircles.
    pub fn set_observed_location_lat_degrees(&mut self, v: f64) -> &mut Self {
        self.observed_location_lat = semconv::to_semicircles(v).unwrap_or(i32::MAX);
        self
    }

    /// Returns `observed_location_long` in degrees instead of semicircles. It returns `None` when value is invalid.
    pub fn observed_location_long_degrees(&self) -> Option<f64> {
        semconv::to_degrees(self.observed_location_long)
    }

    /// Set `observed_location_long` with a value in degrees instead of semicircles, the value will be converted to semicircles.
    pub fn set_observed_location_long_degrees(&mut self, v: f64) -> &mut Self {
        self.observed_location_long = semconv::to_semicircles(v).unwrap_or(i32::MAX);
        self
    }

    /// Returns `wind_speed` in its scaled value. It returns `None` when value is invalid.
    ///
    /// Units: m/s
    pub fn wind_speed_scaled(&self) -> Option<f64> {
        if self.wind_speed == u16::MAX {
            return None;
        }
        Some(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 Self {
        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
    }

    fn count_valid_fields(&self) -> usize {
        (self.timestamp.0 != u32::MAX) as usize
            + (self.weather_report.0 != u8::MAX) as usize
            + (self.temperature != i8::MAX) as usize
            + (self.condition.0 != u8::MAX) as usize
            + (self.wind_direction != u16::MAX) as usize
            + (self.wind_speed != u16::MAX) as usize
            + (self.precipitation_probability != u8::MAX) as usize
            + (self.temperature_feels_like != i8::MAX) as usize
            + (self.relative_humidity != u8::MAX) as usize
            + (!self.location.is_empty()) as usize
            + (self.observed_at_time.0 != u32::MAX) as usize
            + (self.observed_location_lat != i32::MAX) as usize
            + (self.observed_location_long != i32::MAX) as usize
            + (self.day_of_week.0 != u8::MAX) as usize
            + (self.high_temperature != i8::MAX) as usize
            + (self.low_temperature != i8::MAX) as usize
    }
}

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 {
        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 v = Self::new();
        v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
        v.developer_fields = mesg.developer_fields.clone();

        for field in &mesg.fields {
            match field.num {
                253 => v.timestamp = typedef::DateTime(field.value.as_u32()),
                0 => v.weather_report = typedef::WeatherReport(field.value.as_u8()),
                1 => v.temperature = field.value.as_i8(),
                2 => v.condition = typedef::WeatherStatus(field.value.as_u8()),
                3 => v.wind_direction = field.value.as_u16(),
                4 => v.wind_speed = field.value.as_u16(),
                5 => v.precipitation_probability = field.value.as_u8(),
                6 => v.temperature_feels_like = field.value.as_i8(),
                7 => v.relative_humidity = field.value.as_u8(),
                8 => v.location = field.value.as_str().to_owned(),
                9 => v.observed_at_time = typedef::DateTime(field.value.as_u32()),
                10 => v.observed_location_lat = field.value.as_i32(),
                11 => v.observed_location_long = field.value.as_i32(),
                12 => v.day_of_week = typedef::DayOfWeek(field.value.as_u8()),
                13 => v.high_temperature = field.value.as_i8(),
                14 => v.low_temperature = field.value.as_i8(),
                _ => v.unknown_fields.push(field.clone()),
            };
        }

        v
    }
}

impl From<WeatherConditions> for Message {
    fn from(m: WeatherConditions) -> Self {
        let mut fields =
            Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());

        if m.timestamp.0 != u32::MAX {
            fields.push(Field {
                num: 253,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            });
        };
        if m.weather_report.0 != u8::MAX {
            fields.push(Field {
                num: 0,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.weather_report.0),
                is_expanded: false,
            });
        };
        if m.temperature != i8::MAX {
            fields.push(Field {
                num: 1,
                base_type: FitBaseType::SINT8,
                value: Value::Int8(m.temperature),
                is_expanded: false,
            });
        };
        if m.condition.0 != u8::MAX {
            fields.push(Field {
                num: 2,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.condition.0),
                is_expanded: false,
            });
        };
        if m.wind_direction != u16::MAX {
            fields.push(Field {
                num: 3,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.wind_direction),
                is_expanded: false,
            });
        };
        if m.wind_speed != u16::MAX {
            fields.push(Field {
                num: 4,
                base_type: FitBaseType::UINT16,
                value: Value::Uint16(m.wind_speed),
                is_expanded: false,
            });
        };
        if m.precipitation_probability != u8::MAX {
            fields.push(Field {
                num: 5,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.precipitation_probability),
                is_expanded: false,
            });
        };
        if m.temperature_feels_like != i8::MAX {
            fields.push(Field {
                num: 6,
                base_type: FitBaseType::SINT8,
                value: Value::Int8(m.temperature_feels_like),
                is_expanded: false,
            });
        };
        if m.relative_humidity != u8::MAX {
            fields.push(Field {
                num: 7,
                base_type: FitBaseType::UINT8,
                value: Value::Uint8(m.relative_humidity),
                is_expanded: false,
            });
        };
        if !m.location.is_empty() {
            fields.push(Field {
                num: 8,
                base_type: FitBaseType::STRING,
                value: Value::String(m.location),
                is_expanded: false,
            });
        };
        if m.observed_at_time.0 != u32::MAX {
            fields.push(Field {
                num: 9,
                base_type: FitBaseType::UINT32,
                value: Value::Uint32(m.observed_at_time.0),
                is_expanded: false,
            });
        };
        if m.observed_location_lat != i32::MAX {
            fields.push(Field {
                num: 10,
                base_type: FitBaseType::SINT32,
                value: Value::Int32(m.observed_location_lat),
                is_expanded: false,
            });
        };
        if m.observed_location_long != i32::MAX {
            fields.push(Field {
                num: 11,
                base_type: FitBaseType::SINT32,
                value: Value::Int32(m.observed_location_long),
                is_expanded: false,
            });
        };
        if m.day_of_week.0 != u8::MAX {
            fields.push(Field {
                num: 12,
                base_type: FitBaseType::ENUM,
                value: Value::Uint8(m.day_of_week.0),
                is_expanded: false,
            });
        };
        if m.high_temperature != i8::MAX {
            fields.push(Field {
                num: 13,
                base_type: FitBaseType::SINT8,
                value: Value::Int8(m.high_temperature),
                is_expanded: false,
            });
        };
        if m.low_temperature != i8::MAX {
            fields.push(Field {
                num: 14,
                base_type: FitBaseType::SINT8,
                value: Value::Int8(m.low_temperature),
                is_expanded: false,
            });
        };

        fields.extend_from_slice(&m.unknown_fields);

        Self {
            header: 0,
            num: typedef::MesgNum::WEATHER_CONDITIONS,
            fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for WeatherConditions {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let n = self.count_valid_fields() + 2;
        let mut state = serializer.serialize_struct("WeatherConditions", n)?;
        if let Some(v) = self.timestamp.unix_timestamp() {
            state.serialize_field("timestamp", &v)?;
        }
        if self.weather_report.0 != u8::MAX {
            state.serialize_field("weather_report", &self.weather_report)?;
        }
        if self.temperature != i8::MAX {
            state.serialize_field("temperature", &self.temperature)?;
        }
        if self.condition.0 != u8::MAX {
            state.serialize_field("condition", &self.condition)?;
        }
        if self.wind_direction != u16::MAX {
            state.serialize_field("wind_direction", &self.wind_direction)?;
        }
        if let Some(v) = self.wind_speed_scaled() {
            state.serialize_field("wind_speed", &v)?;
        }
        if self.precipitation_probability != u8::MAX {
            state.serialize_field("precipitation_probability", &self.precipitation_probability)?;
        }
        if self.temperature_feels_like != i8::MAX {
            state.serialize_field("temperature_feels_like", &self.temperature_feels_like)?;
        }
        if self.relative_humidity != u8::MAX {
            state.serialize_field("relative_humidity", &self.relative_humidity)?;
        }
        if !self.location.is_empty() {
            state.serialize_field("location", &self.location)?;
        }
        if let Some(v) = self.observed_at_time.unix_timestamp() {
            state.serialize_field("observed_at_time", &v)?;
        }
        if let Some(v) = self.observed_location_lat_degrees() {
            state.serialize_field("observed_location_lat", &v)?;
        }
        if let Some(v) = self.observed_location_long_degrees() {
            state.serialize_field("observed_location_long", &v)?;
        }
        if self.day_of_week.0 != u8::MAX {
            state.serialize_field("day_of_week", &self.day_of_week)?;
        }
        if self.high_temperature != i8::MAX {
            state.serialize_field("high_temperature", &self.high_temperature)?;
        }
        if self.low_temperature != i8::MAX {
            state.serialize_field("low_temperature", &self.low_temperature)?;
        }
        if !self.unknown_fields.is_empty() {
            state.serialize_field("unknown_fields", &self.unknown_fields)?;
        }
        if !self.developer_fields.is_empty() {
            state.serialize_field("developer_fields", &self.developer_fields)?;
        }
        state.end()
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
    timestamp: Option<i64>,
    weather_report: typedef::WeatherReport,
    temperature: i8,
    condition: typedef::WeatherStatus,
    wind_direction: u16,
    wind_speed: f64,
    precipitation_probability: u8,
    temperature_feels_like: i8,
    relative_humidity: u8,
    location: String,
    observed_at_time: Option<i64>,
    /// Degrees.
    observed_location_lat: f64,
    /// Degrees.
    observed_location_long: f64,
    day_of_week: typedef::DayOfWeek,
    high_temperature: i8,
    low_temperature: i8,
    unknown_fields: Vec<Field>,
    developer_fields: Vec<DeveloperField>,
}

#[cfg(feature = "serde")]
impl From<De> for WeatherConditions {
    fn from(m: De) -> Self {
        Self {
            timestamp: m.timestamp.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            weather_report: m.weather_report,
            temperature: m.temperature,
            condition: m.condition,
            wind_direction: m.wind_direction,
            wind_speed: {
                let unscaled = (m.wind_speed + 0.0) * 1000.0;
                if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
                    u16::MAX
                } else {
                    unscaled as u16
                }
            },
            precipitation_probability: m.precipitation_probability,
            temperature_feels_like: m.temperature_feels_like,
            relative_humidity: m.relative_humidity,
            location: m.location,
            observed_at_time: m.observed_at_time.map_or_else(
                || typedef::DateTime(u32::MAX),
                typedef::DateTime::from_unix_timestamp,
            ),
            observed_location_lat: semconv::to_semicircles(m.observed_location_lat)
                .unwrap_or(i32::MAX),
            observed_location_long: semconv::to_semicircles(m.observed_location_long)
                .unwrap_or(i32::MAX),
            day_of_week: m.day_of_week,
            high_temperature: m.high_temperature,
            low_temperature: m.low_temperature,
            unknown_fields: m.unknown_fields,
            developer_fields: m.developer_fields,
        }
    }
}

#[cfg(feature = "serde")]
impl Default for De {
    fn default() -> Self {
        Self {
            timestamp: None,
            weather_report: typedef::WeatherReport(u8::MAX),
            temperature: i8::MAX,
            condition: typedef::WeatherStatus(u8::MAX),
            wind_direction: u16::MAX,
            wind_speed: f64::from_bits(u64::MAX),
            precipitation_probability: u8::MAX,
            temperature_feels_like: i8::MAX,
            relative_humidity: u8::MAX,
            location: String::new(),
            observed_at_time: None,
            observed_location_lat: f64::from_bits(u64::MAX),
            observed_location_long: f64::from_bits(u64::MAX),
            day_of_week: typedef::DayOfWeek(u8::MAX),
            high_temperature: i8::MAX,
            low_temperature: i8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }
}