rustyfit 0.9.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
#![warn(missing_docs)]

mod accumulator;
mod bits;

use crate::{
    crc16::Crc16,
    decoder::{accumulator::Accumulator, bits::Bits},
    profile::{
        ProfileType, lookup,
        typedef::{FitBaseType, MesgNum},
    },
    proto::*,
};
use alloc::{vec, vec::Vec};
use core::mem;
use embedded_io::{Read, ReadExactError};

/// Decoder Error
#[derive(Debug)]
pub enum Error<E> {
    /// I/O related error when reading from the Reader.
    Io {
        /// I/O error
        err: E,
    },
    /// Unexpected EOF occurs during process.
    UnexpectedEof,
    /// File Header's size is not 12 or 14, or data_type is not `.FIT`.
    NotFITFile,
    /// Checksum mismatch either in File Header or in record data.
    ChecksumMismatch {
        /// Expected CRC retrieved from FIT File.
        found: u16,
        /// Actual CRC calculated by Decoder
        calculated: u16,
    },
    /// Missing message definition for the current message data.
    MissingMessageDefinition {
        /// Local Message Number
        local_mesg_num: u8,
    },
}

impl<E> core::fmt::Display for Error<E>
where
    E: core::fmt::Display,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match &self {
            Self::Io { err } => write!(f, "io error: {}", err),
            Self::UnexpectedEof => write!(f, "unexpected EOF"),
            Self::NotFITFile => write!(f, "not a FIT file"),
            Self::ChecksumMismatch { found, calculated } => {
                write!(
                    f,
                    "checksum mismatch, found {} calculated {}",
                    found, calculated
                )
            }
            Self::MissingMessageDefinition { local_mesg_num } => write!(
                f,
                "missing message definition for local message number {}",
                local_mesg_num
            ),
        }
    }
}

impl<E> From<ReadExactError<E>> for Error<E> {
    fn from(err: ReadExactError<E>) -> Self {
        match err {
            ReadExactError::UnexpectedEof => Error::UnexpectedEof,
            ReadExactError::Other(err) => Error::Io { err },
        }
    }
}

impl<E> core::error::Error for Error<E> where E: core::fmt::Debug + core::fmt::Display {}

#[derive(Clone, Copy)]
struct Options {
    checksum: bool,
    expand_components: bool,
}

/// Decoder for decoding FIT file.
pub struct Decoder {
    buf: [u8; 256],
    cur: u32,
    crc16: Crc16,
    mesg_definitions: [MessageDefinition; 16],
    accumulator: Accumulator,
    timestamp: u32,
    mesg: Message,
    field_descriptions: Vec<LocalFieldDescription>,
    options: Options,
}

impl Decoder {
    /// Create new Decoder for decoding FIT file.
    /// For more options, use `Decoder::builder()` to build the Decoder.
    pub const fn new() -> Self {
        Builder::new().build()
    }

    /// Create new Decoder with options for decoding FIT file.
    pub const fn builder() -> Builder {
        Builder::new()
    }

    /// Creates a `Stream` from a mutably borrowed `Decoder` for streaming decoding of the given `reader`.
    ///
    /// Example:
    ///
    /// ```
    /// # use embedded_io_adapters::std::FromStd;
    /// # use rustyfit::{Decoder, StreamingIterator};
    /// # use std::fs::File;
    /// # let f = File::open("tests/data/from_official_sdk/Activity.fit").unwrap();
    /// # let mut reader = FromStd::new(f);
    /// // ...
    /// let mut dec = Decoder::new();
    /// let mut stream = dec.stream(&mut reader);
    ///
    /// while let Some(event) = stream.next() {
    ///    // do something with event having borrowed data
    /// }
    ///
    /// ```
    pub fn stream<'a, R>(&'a mut self, reader: R) -> Stream<'a, R> {
        self.reset();

        Stream {
            reader,
            decoder: self,
            state: State::FileHeader,
            data_size: 0,
        }
    }

    /// Decode return a single FIT sequence. If it's a chained FIT file, call this method multiple times.
    pub fn decode<R>(&mut self, mut reader: R) -> Result<Option<FIT>, Error<R::Error>>
    where
        R: Read,
    {
        self.reset();

        let file_header = match self.decode_file_header(&mut reader)? {
            Some(file_header) => file_header,
            None => return Ok(None),
        };

        let mut messages = Vec::new();

        while self.cur < file_header.data_size {
            self.read_exact_inc(&mut reader, 1)?;

            let header = self.buf[0];

            if header & Message::HEADER_MASK == Message::DEFINITION_MASK {
                let local_mesg_num = (header & Message::LOCAL_NUM_MASK) as usize;
                let mut mesg_def = mem::take(&mut self.mesg_definitions[local_mesg_num]);

                mesg_def.header = header;

                let result = self.decode_message_definition(&mut reader, &mut mesg_def);
                self.mesg_definitions[local_mesg_num] = mesg_def;
                result?;

                continue;
            }

            let local_mesg_num = local_mesg_num_from_mesg_header(header);
            if self.mesg_definitions[local_mesg_num].header == 0 {
                return Err(Error::MissingMessageDefinition {
                    local_mesg_num: local_mesg_num as u8,
                });
            }

            let mesg_def = mem::take(&mut self.mesg_definitions[local_mesg_num]);
            let mut mesg = mem::take(&mut self.mesg);

            mesg.header = header;

            let result = self.decode_message_data(&mut reader, &mut mesg, &mesg_def);
            self.mesg_definitions[local_mesg_num] = mesg_def;
            self.mesg = mesg;
            result?;

            messages.push(self.mesg.clone());
        }

        let crc = self.decode_crc(&mut reader)?;

        Ok(Some(FIT {
            file_header,
            messages,
            crc,
        }))
    }

    fn decode_file_header<R>(
        &mut self,
        reader: &mut R,
    ) -> Result<Option<FileHeader>, Error<R::Error>>
    where
        R: Read,
    {
        if let Err(err) = reader.read_exact(&mut self.buf[..1]) {
            if let ReadExactError::UnexpectedEof = err {
                return Ok(None);
            }
            return Err(Error::from(err));
        };

        let size = self.buf[0];
        if size != 12 && size != 14 {
            return Err(Error::NotFITFile);
        }

        reader.read_exact(&mut self.buf[1..size as usize])?;

        if &self.buf[8..12] != FileHeader::DATA_TYPE.as_bytes() {
            return Err(Error::NotFITFile);
        }

        let crc = match size {
            14 => u16::from_le_bytes([self.buf[12], self.buf[13]]),
            _ => 0,
        };

        if size == 14 && crc != 0 {
            self.crc16.write(&self.buf[..12]);
            if self.options.checksum && crc != self.crc16.sum16() {
                return Err(Error::ChecksumMismatch {
                    found: crc,
                    calculated: self.crc16.sum16(),
                });
            }
            self.crc16.reset();
        }

        Ok(Some(FileHeader {
            size,
            protocol_version: ProtocolVersion(self.buf[1]),
            profile_version: u16::from_le_bytes([self.buf[2], self.buf[3]]),
            data_size: u32::from_le_bytes(self.buf[4..8].try_into().unwrap()),
            crc,
        }))
    }

    /// Reads the exact number of bytes required to fill buf, increment n read bytes and calculate checksum.
    fn read_exact_inc<R>(&mut self, reader: &mut R, n: usize) -> Result<(), Error<R::Error>>
    where
        R: Read,
    {
        reader.read_exact(&mut self.buf[..n])?;
        self.cur += n as u32;
        if self.options.checksum {
            self.crc16.write(&self.buf[..n]);
        }
        Ok(())
    }

    fn decode_message_definition<R>(
        &mut self,
        reader: &mut R,
        mesg_def: &mut MessageDefinition,
    ) -> Result<(), Error<R::Error>>
    where
        R: Read,
    {
        self.read_exact_inc(reader, 5)?;

        mesg_def.reserved = self.buf[0];
        mesg_def.arch = self.buf[1];
        mesg_def.mesg_num = MesgNum(match mesg_def.arch {
            0 => u16::from_le_bytes([self.buf[2], self.buf[3]]),
            _ => u16::from_be_bytes([self.buf[2], self.buf[3]]),
        });
        mesg_def.field_definitions.clear();
        mesg_def.developer_field_definitions.clear();

        mesg_def.field_definitions.reserve_exact(255);

        let mut n = self.buf[4] as usize * 3;
        while n > 0 {
            let chunk = n.min(255);
            self.read_exact_inc(reader, chunk)?;
            n -= chunk;

            mesg_def
                .field_definitions
                .extend(self.buf[..chunk].chunks_exact(3).map(|b| FieldDefinition {
                    num: b[0],
                    size: b[1],
                    base_type: FitBaseType(b[2]),
                }));
        }

        if mesg_def.header & Message::DEV_DATA_MASK == Message::DEV_DATA_MASK {
            self.read_exact_inc(reader, 1)?;

            mesg_def.developer_field_definitions.reserve_exact(255);

            let mut n = self.buf[0] as usize * 3;
            while n > 0 {
                let chunk = n.min(255);
                self.read_exact_inc(reader, chunk)?;
                n -= chunk;

                mesg_def
                    .developer_field_definitions
                    .extend(
                        self.buf[..chunk]
                            .chunks_exact(3)
                            .map(|b| DeveloperFieldDefinition {
                                num: b[0],
                                size: b[1],
                                developer_data_index: b[2],
                            }),
                    );
            }
        }

        Ok(())
    }

    fn decode_message_data<R>(
        &mut self,
        reader: &mut R,
        mesg: &mut Message,
        mesg_def: &MessageDefinition,
    ) -> Result<(), Error<R::Error>>
    where
        R: Read,
    {
        mesg.num = mesg_def.mesg_num;
        mesg.fields.clear();
        mesg.developer_fields.clear();

        if mesg.header & Message::COMPRESSED_HEADER_MASK == Message::COMPRESSED_HEADER_MASK {
            let last_time_offset = (self.timestamp & Message::COMPRESSED_TIME_MASK as u32) as u8;
            let time_offset = mesg.header & Message::COMPRESSED_TIME_MASK;
            self.timestamp = self.timestamp.wrapping_add(
                (time_offset.wrapping_sub(last_time_offset) & Message::COMPRESSED_TIME_MASK) as u32,
            );

            mesg.fields.push(Field {
                num: Field::TIMESTAMP,
                profile_type: ProfileType::DATE_TIME,
                is_expanded: false,
                value: Value::Uint32(self.timestamp),
            });
        }

        self.decode_fields(reader, mesg, mesg_def)?;

        self.decode_developer_fields(reader, mesg, mesg_def)?;

        // Developer Data Lookup, currently we allow missing developer_data_id
        if mesg.num == MesgNum::FIELD_DESCRIPTION {
            self.field_descriptions
                .push(LocalFieldDescription::from(&*mesg));
        }

        if !self.options.expand_components {
            return Ok(());
        }

        // Now that all fields has been decoded, we need to expand all components and accumulate the accumulable values.
        for i in 0..mesg.fields.len() {
            let field = &mesg.fields[i];
            if !field.value.is_valid(field.profile_type.base_type()) {
                continue;
            }
            let field_ref = match lookup::field_reference(mesg.num, field.num) {
                Some(field_ref) => field_ref,
                None => continue,
            };
            let components = match mesg.sub_field_substitution(&field_ref) {
                Some(sub_field) => sub_field.components,
                None => field_ref.components,
            };
            if components.is_empty() {
                continue;
            }
            if let Some(bits) = &mut Bits::new(&field.value) {
                self.expand_components(mesg, bits, components);
            };
        }

        Ok(())
    }

    fn decode_fields<R>(
        &mut self,
        reader: &mut R,
        mesg: &mut Message,
        mesg_def: &MessageDefinition,
    ) -> Result<(), Error<R::Error>>
    where
        R: Read,
    {
        for field_def in &mesg_def.field_definitions {
            self.read_exact_inc(reader, field_def.size as usize)?;
            let mut buf = &self.buf[..field_def.size as usize];

            let num = field_def.num;
            let base_type: FitBaseType;
            let profile_type: ProfileType;
            let accumulate: bool;
            let array: bool;

            match lookup::field_reference(mesg_def.mesg_num, num) {
                Some(field_ref) => {
                    base_type = field_ref.base_type;
                    profile_type = field_ref.profile_type;
                    accumulate = field_ref.accumulate;
                    array = field_ref.array;
                }
                None => {
                    base_type = field_def.base_type;
                    profile_type = ProfileType::from(field_def.base_type);
                    accumulate = false;
                    array = match base_type {
                        FitBaseType::STRING => Value::strcount(buf) > 1,
                        _ => {
                            field_def.size > base_type.size()
                                && field_def.size % base_type.size() == 0
                        }
                    }
                }
            };

            if field_def.size < base_type.size() {
                buf = slice_buffer_to_match_type_size(
                    &mut self.buf,
                    mesg_def.arch,
                    field_def.size as usize,
                    base_type.size() as usize,
                );
            }

            let value = Value::from_parts(buf, array, base_type, mesg_def.arch);

            if num == Field::TIMESTAMP
                && base_type == FitBaseType::UINT32
                && let Value::Uint32(v) = value
            {
                self.timestamp = v;
            }

            if accumulate {
                self.accumulator.collect(mesg.num, num, &value);
            }

            mesg.fields.push(Field {
                num,
                profile_type,
                is_expanded: false,
                value,
            });
        }

        Ok(())
    }

    fn expand_components(&mut self, mesg: &mut Message, bits: &mut Bits, components: &[Component]) {
        for component in components {
            let mut val = match bits.pull(component.bits) {
                Some(v) => v,
                None => break,
            };

            let field_num = component.field_num;
            if component.accumulate {
                val = self
                    .accumulator
                    .accumulate(mesg.num, field_num, val, component.bits);
            }

            let field_ref = match lookup::field_reference(mesg.num, field_num) {
                Some(v) => v,
                None => continue,
            };

            let scaled_val = val as f64 / component.scale - component.offset;
            val = ((scaled_val + field_ref.offset) * field_ref.scale) as u64;
            let value = convert_u64_to_value(val, field_ref.base_type);

            match mesg.fields.iter_mut().find(|v| v.num == field_num) {
                Some(v) => {
                    if field_ref.array {
                        push_value_to_vec(&mut v.value, &value);
                    } else {
                        v.value = value;
                    }
                }
                None => {
                    mesg.fields.push(Field {
                        num: field_num,
                        profile_type: field_ref.profile_type,
                        is_expanded: true,
                        value: if field_ref.array {
                            let mut vec_value = Value::Invalid;
                            push_value_to_vec(&mut vec_value, &value);
                            vec_value
                        } else {
                            value
                        },
                    });
                }
            };

            let components = match mesg.sub_field_substitution(&field_ref) {
                Some(sub_field) => sub_field.components,
                None => field_ref.components,
            };

            if components.is_empty() {
                continue;
            }

            let value = convert_u64_to_value(val, field_ref.base_type);
            if !value.is_valid(field_ref.base_type) {
                continue;
            }

            if let Some(bits) = &mut Bits::new(&value) {
                self.expand_components(mesg, bits, components);
            };
        }
    }

    fn decode_developer_fields<R>(
        &mut self,
        reader: &mut R,
        mesg: &mut Message,
        mesg_def: &MessageDefinition,
    ) -> Result<(), Error<R::Error>>
    where
        R: Read,
    {
        for dev_field_def in &mesg_def.developer_field_definitions {
            self.read_exact_inc(reader, dev_field_def.size as usize)?;
            let mut buf = &self.buf[..dev_field_def.size as usize];

            let field_desc = match self.field_descriptions.iter().find(|v| {
                v.developer_data_index == dev_field_def.developer_data_index
                    && v.field_definition_number == dev_field_def.num
            }) {
                Some(field_desc) => field_desc,
                None => continue, // Currently we ignore missing field_description
            };

            let base_type = field_desc.fit_base_type_id;
            if dev_field_def.size < base_type.size() {
                buf = slice_buffer_to_match_type_size(
                    &mut self.buf,
                    mesg_def.arch,
                    dev_field_def.size as usize,
                    base_type.size() as usize,
                );
            }

            let size = dev_field_def.size;
            let array = match base_type {
                FitBaseType::STRING => Value::strcount(buf) > 1,
                _ => size > base_type.size() && size % base_type.size() == 0,
            };

            let value = Value::from_parts(buf, array, base_type, mesg_def.arch);

            mesg.developer_fields.push(DeveloperField {
                num: dev_field_def.num,
                developer_data_index: dev_field_def.developer_data_index,
                value,
            });
        }

        Ok(())
    }

    fn decode_crc<R>(&mut self, reader: &mut R) -> Result<u16, Error<R::Error>>
    where
        R: Read,
    {
        let mut arr = [0u8; 2];
        reader.read_exact(&mut arr)?;

        let found = u16::from_le_bytes(arr);
        let calculated = self.crc16.sum16();
        if self.options.checksum && found != calculated {
            return Err(Error::ChecksumMismatch { found, calculated });
        }

        Ok(found)
    }

    fn reset(&mut self) {
        self.cur = 0;
        self.crc16.reset();
        self.accumulator.reset();
        self.timestamp = 0;

        self.field_descriptions.clear();
        self.field_descriptions.reserve_exact(32);

        for mesg_def in &mut self.mesg_definitions {
            mesg_def.header = 0;
        }

        self.mesg.fields.clear();
        self.mesg.fields.reserve_exact(255);

        self.mesg.developer_fields.clear();
        self.mesg.developer_fields.reserve_exact(255);
    }
}

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

fn local_mesg_num_from_mesg_header(header: u8) -> usize {
    (match header & Message::COMPRESSED_HEADER_MASK {
        Message::COMPRESSED_HEADER_MASK => {
            (header & Message::COMPRESSED_LOCAL_NUM_MASK) >> Message::COMPRESSED_BIT_SHIFT
        }
        _ => header,
    } & Message::LOCAL_NUM_MASK) as usize
}

fn slice_buffer_to_match_type_size(
    arr: &mut [u8],
    arch: u8,
    current_len: usize,
    target_len: usize,
) -> &[u8] {
    if arch == 0 {
        arr[current_len..target_len].fill(0);
        &arr[..target_len]
    } else {
        arr.copy_within(..current_len, target_len - current_len);
        arr[..target_len - current_len].fill(0);
        &arr[..target_len]
    }
}

fn convert_u64_to_value(val: u64, base_type: FitBaseType) -> Value {
    match base_type {
        FitBaseType::SINT8 => Value::Int8(val as i8),
        FitBaseType::ENUM | FitBaseType::BYTE | FitBaseType::UINT8 | FitBaseType::UINT8Z => {
            Value::Uint8(val as u8)
        }
        FitBaseType::SINT16 => Value::Int16(val as i16),
        FitBaseType::UINT16 | FitBaseType::UINT16Z => Value::Uint16(val as u16),
        FitBaseType::SINT32 => Value::Int32(val as i32),
        FitBaseType::UINT32 | FitBaseType::UINT32Z => Value::Uint32(val as u32),
        FitBaseType::FLOAT32 => Value::Float32(val as f32),
        FitBaseType::FLOAT64 => Value::Float64(val as f64),
        FitBaseType::SINT64 => Value::Int64(val as i64),
        FitBaseType::UINT64 | FitBaseType::UINT64Z => Value::Uint64(val),
        _ => Value::Invalid,
    }
}

fn push_value_to_vec(vec_value: &mut Value, value: &Value) {
    match value {
        Value::Uint8(v) => match vec_value {
            Value::VecUint8(vs) => vs.push(*v),
            _ => *vec_value = Value::VecUint8(vec![*v]),
        },
        Value::Int8(v) => match vec_value {
            Value::VecInt8(vs) => vs.push(*v),
            _ => *vec_value = Value::VecInt8(vec![*v]),
        },
        Value::Uint16(v) => match vec_value {
            Value::VecUint16(vs) => vs.push(*v),
            _ => *vec_value = Value::VecUint16(vec![*v]),
        },
        Value::Int16(v) => match vec_value {
            Value::VecInt16(vs) => vs.push(*v),
            _ => *vec_value = Value::VecInt16(vec![*v]),
        },
        Value::Uint32(v) => match vec_value {
            Value::VecUint32(vs) => vs.push(*v),
            _ => *vec_value = Value::VecUint32(vec![*v]),
        },
        Value::Int32(v) => match vec_value {
            Value::VecInt32(vs) => vs.push(*v),
            _ => *vec_value = Value::VecInt32(vec![*v]),
        },
        Value::Float32(v) => match vec_value {
            Value::VecFloat32(vs) => vs.push(*v),
            _ => *vec_value = Value::VecFloat32(vec![*v]),
        },
        Value::Float64(v) => match vec_value {
            Value::VecFloat64(vs) => vs.push(*v),
            _ => *vec_value = Value::VecFloat64(vec![*v]),
        },
        Value::Int64(v) => match vec_value {
            Value::VecInt64(vs) => vs.push(*v),
            _ => *vec_value = Value::VecInt64(vec![*v]),
        },
        Value::Uint64(v) => match vec_value {
            Value::VecUint64(vs) => vs.push(*v),
            _ => *vec_value = Value::VecUint64(vec![*v]),
        },
        _ => {}
    }
}

/// Build Decoder with some options.
pub struct Builder {
    options: Options,
}

impl Builder {
    /// Create new DecoderBuilder.
    pub const fn new() -> Self {
        Self {
            options: Options {
                checksum: true,
                expand_components: true,
            },
        }
    }

    /// Toggle for checksum calculation (default: `true`).
    /// If you want to retrieve the data regardless its integrity, set this to `false`.
    pub const fn checksum(mut self, v: bool) -> Self {
        self.options.checksum = v;
        self
    }

    /// Toggle for field's components expansion (default: `true`).
    pub const fn expand_components(mut self, v: bool) -> Self {
        self.options.expand_components = v;
        self
    }

    /// Build Decoder based on given options (if any).
    pub const fn build(&self) -> Decoder {
        Decoder {
            buf: [0u8; 256],
            cur: 0,
            crc16: Crc16::new(),
            mesg_definitions: [const {
                MessageDefinition {
                    header: 0,
                    reserved: 0,
                    arch: 0,
                    mesg_num: MesgNum(0),
                    field_definitions: Vec::new(),
                    developer_field_definitions: Vec::new(),
                }
            }; 16],
            accumulator: Accumulator::new(),
            timestamp: 0,
            mesg: Message {
                header: 0,
                num: MesgNum(0),
                fields: Vec::new(),
                developer_fields: Vec::new(),
            },
            field_descriptions: Vec::new(),
            options: self.options,
        }
    }
}

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

/// Event is FIT segments encountered by the `StreamDecoder`.
#[derive(Debug)]
pub enum Event<'a> {
    /// Returned when the `StreamDecoder` encounter a `FileHeader`.
    FileHeader(FileHeader),
    /// Returned when the `StreamDecoder` encounter a `MessageDefinition`.
    MessageDefinition(&'a MessageDefinition),
    /// Returned when the `StreamDecoder` encounter a `Message`.
    Message(&'a Message),
    /// Returned when the `StreamDecoder` encounter a `File`'s CRC.
    Crc(u16),
}

enum State {
    FileHeader,
    Message,
    Crc,
}

/// A `Stream` from a mutably borrowed `Decoder` for streaming decoding.
pub struct Stream<'a, R> {
    reader: R,
    decoder: &'a mut Decoder,
    state: State,
    data_size: u32,
}

impl<'a, R: Read> Stream<'a, R> {
    /// Discard this current sequence and make `Stream` pointing to the next sequence.
    pub fn discard(&mut self) -> Result<(), Error<R::Error>> {
        loop {
            match self.state {
                State::FileHeader => {
                    let checksum = self.decoder.options.checksum;
                    self.decoder.options.checksum = false; // no checksum

                    let result = self.decoder.decode_file_header(&mut self.reader);
                    self.decoder.options.checksum = checksum; // restore checksum
                    self.data_size = match result? {
                        Some(file_header) => file_header.data_size,
                        None => break,
                    };

                    self.state = State::Message;
                }
                State::Message => {
                    while self.decoder.cur < self.data_size {
                        let buf = &mut self.decoder.buf[..256];
                        let remaining = self.data_size - self.decoder.cur;
                        let n = (remaining as usize).min(buf.len());
                        self.reader.read_exact(&mut buf[..n])?;
                        self.decoder.cur += n as u32;
                    }
                    self.state = State::Crc;
                }
                State::Crc => {
                    self.reader.read_exact(&mut self.decoder.buf[..2])?;
                    self.state = State::FileHeader;
                    break;
                }
            }
        }
        self.decoder.reset();

        Ok(())
    }
}

/// An Iterator-like trait that return borrowed Item rather than owned Item.
/// StreamingIterator is lazy and do nothing unless `next()` is called.
pub trait StreamingIterator {
    /// The type of the elements being iterated over.
    type Item<'a>
    where
        Self: 'a;

    /// Advances the iterator and returns the next value.
    /// Returns [`None`] when iteration is finished.
    fn next(&mut self) -> Option<Self::Item<'_>>;
}

impl<'a, R: Read> StreamingIterator for Stream<'a, R> {
    type Item<'b>
        = Result<Event<'b>, Error<R::Error>>
    where
        Self: 'b;

    /// Decode next `DecoderEvent` until it returns `None` indicating no more data is available from the `reader`.
    /// Since this is lazily evaluated, users can decide when to stop without required to read the whole reader.
    fn next(&mut self) -> Option<Result<Event<'_>, Error<R::Error>>> {
        match self.state {
            State::FileHeader => {
                let file_header = match self.decoder.decode_file_header(&mut self.reader) {
                    Ok(file_header) => file_header,
                    Err(err) => return Some(Err(err)),
                }?;
                self.data_size = file_header.data_size;
                self.state = State::Message;
                Some(Ok(Event::FileHeader(file_header)))
            }
            State::Message => {
                if let Err(err) = self.decoder.read_exact_inc(&mut self.reader, 1) {
                    return Some(Err(err));
                }

                let header = self.decoder.buf[0];

                if header & Message::HEADER_MASK == Message::DEFINITION_MASK {
                    let local_mesg_num = (header & Message::LOCAL_NUM_MASK) as usize;
                    let mut mesg_def =
                        mem::take(&mut self.decoder.mesg_definitions[local_mesg_num]);

                    mesg_def.header = header;

                    let result = self
                        .decoder
                        .decode_message_definition(&mut self.reader, &mut mesg_def);

                    self.decoder.mesg_definitions[local_mesg_num] = mesg_def;

                    if let Err(err) = result {
                        return Some(Err(err));
                    }

                    return Some(Ok(Event::MessageDefinition(
                        &self.decoder.mesg_definitions[local_mesg_num],
                    )));
                }

                let local_mesg_num = local_mesg_num_from_mesg_header(header);
                if self.decoder.mesg_definitions[local_mesg_num].header == 0 {
                    return Some(Err(Error::MissingMessageDefinition {
                        local_mesg_num: local_mesg_num as u8,
                    }));
                }

                let mesg_def = mem::take(&mut self.decoder.mesg_definitions[local_mesg_num]);
                let mut mesg = mem::take(&mut self.decoder.mesg);

                mesg.header = header;

                let result =
                    self.decoder
                        .decode_message_data(&mut self.reader, &mut mesg, &mesg_def);

                self.decoder.mesg_definitions[local_mesg_num] = mesg_def;
                self.decoder.mesg = mesg;

                if let Err(err) = result {
                    return Some(Err(err));
                }

                if self.decoder.cur >= self.data_size {
                    self.state = State::Crc;
                }

                Some(Ok(Event::Message(&self.decoder.mesg)))
            }
            State::Crc => {
                let crc = match self.decoder.decode_crc(&mut self.reader) {
                    Ok(crc) => crc,
                    Err(err) => return Some(Err(err)),
                };
                self.decoder.reset();
                self.state = State::FileHeader;
                Some(Ok(Event::Crc(crc)))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        fs::File,
        io::{BufReader, Cursor, Seek, SeekFrom, Write, empty},
    };

    use crate::{
        Decoder, StreamingIterator,
        decoder::convert_u64_to_value,
        profile::{PROFILE_VERSION, typedef::FitBaseType},
        proto::{FileHeader, Message, MessageDefinition, ProtocolVersion, Value},
    };
    use embedded_io_adapters::std::FromStd;

    #[test]
    fn test_decode_file_header() {
        let mut reader = [
            14, // header
            32, // protocol version
            84, 8, // profile version: 2132
            214, 204, 9, 0, // data size: 642262
            46, 70, 73, 84, // data type: .FIT
            56, 50, // crc: 12856
        ]
        .as_ref();

        let expected = FileHeader {
            size: 14,
            protocol_version: ProtocolVersion::V2,
            profile_version: 2132,
            data_size: 642262,
            crc: 12856,
        };

        let mut dec = Decoder::new();
        let file_header = dec.decode_file_header(&mut reader).unwrap().unwrap();

        assert_eq!(file_header, expected)
    }

    #[test]
    fn test_decompress_timestamp_on_decode_message_data() {
        let mut dec = Decoder::new();
        let timestamp = 1000;
        dec.timestamp = timestamp;
        let mesg_def = MessageDefinition::default();

        let time_offset = (timestamp + 1 & Message::COMPRESSED_TIME_MASK as u32) as u8;
        let mut mesg = Message {
            header: Message::COMPRESSED_HEADER_MASK | time_offset,
            ..Default::default()
        };

        let mut empty = FromStd::new(empty());
        dec.decode_message_data(&mut empty, &mut mesg, &mesg_def)
            .unwrap();
        assert_eq!(dec.timestamp, timestamp + 1, "time_offset {}", time_offset);

        let time_offset = (timestamp + 10 & Message::COMPRESSED_TIME_MASK as u32) as u8;
        let mut mesg = Message {
            header: Message::COMPRESSED_HEADER_MASK | time_offset,
            ..Default::default()
        };

        dec.decode_message_data(&mut empty, &mut mesg, &mesg_def)
            .unwrap();
        assert_eq!(dec.timestamp, timestamp + 10, "time_offset {}", time_offset);
    }

    #[test]
    fn test_convert_u64_to_value() {
        let input = 1u64;

        let tt = [
            (FitBaseType::SINT8, Value::Int8(1)),
            (FitBaseType::ENUM, Value::Uint8(1)),
            (FitBaseType::BYTE, Value::Uint8(1)),
            (FitBaseType::UINT8, Value::Uint8(1)),
            (FitBaseType::UINT8Z, Value::Uint8(1)),
            (FitBaseType::SINT16, Value::Int16(1)),
            (FitBaseType::UINT16, Value::Uint16(1)),
            (FitBaseType::UINT16Z, Value::Uint16(1)),
            (FitBaseType::SINT32, Value::Int32(1)),
            (FitBaseType::UINT32, Value::Uint32(1)),
            (FitBaseType::UINT32Z, Value::Uint32(1)),
            (FitBaseType::FLOAT32, Value::Float32(1.0)),
            (FitBaseType::FLOAT64, Value::Float64(1.0)),
            (FitBaseType::SINT64, Value::Int64(1)),
            (FitBaseType::UINT64, Value::Uint64(1)),
            (FitBaseType::UINT64Z, Value::Uint64(1)),
        ];

        for tc in tt {
            let val = convert_u64_to_value(input, tc.0);
            assert_eq!(tc.1, val, "input: {:?}", tc);
        }
    }

    #[test]
    fn test_stream() {
        let file = File::open("tests/data/large.fit").unwrap();
        let br = BufReader::new(file);
        let mut reader = FromStd::new(br);

        let mut dec = Decoder::new();
        let mut stream = dec.stream(&mut reader);

        while let Some(event) = stream.next() {
            event.unwrap();
        }
    }

    #[test]
    fn test_discard() {
        const DATA_SIZE: u32 = 1008;

        let mut cursor = Cursor::new(Vec::<u8>::with_capacity(14 + DATA_SIZE as usize + 2));
        cursor.write_all(&[14, ProtocolVersion::V1.0]).unwrap();
        cursor.write_all(&PROFILE_VERSION.to_le_bytes()).unwrap();
        cursor.write_all(&DATA_SIZE.to_le_bytes()).unwrap();
        cursor.write_all(FileHeader::DATA_TYPE.as_bytes()).unwrap();
        cursor.write_all(&[0, 0]).unwrap(); // FileHeader's CRC
        cursor.write_all(&[0u8; DATA_SIZE as usize]).unwrap();
        cursor.write_all(&[0, 0]).unwrap(); // CRC

        cursor.seek(SeekFrom::Start(0)).unwrap();

        let mut dec = Decoder::new();
        let mut stream = dec.stream(FromStd::new(cursor));

        stream
            .discard()
            .expect("should return Ok since reader is valid");

        stream
            .discard()
            .expect("should return Ok since reader is already empty");
    }
}