Skip to main content

write

Function write 

Source
pub fn write<S, E, F>(
    sink: S,
    options: Options,
    source: F,
) -> Result<S, WriteError<S::Error, E>>
where S: Sink, E: From<Error>, F: FnMut(&mut Writer<'_>) -> Result<(), E>,
Expand description

Writes the file source describes to sink, header first and CRC last, without seeking.

The header holds the length of what follows, so source runs twice: once to size the file, then to write it. It must write the same messages both times, as a source reading a stored log does; if it does not, the second pass fails with Error::SourceChanged.

use fit_encoder::*;

let start = Timestamp::from_unix(1_764_547_200).unwrap();
let watch = Device::new(Manufacturer::DEVELOPMENT, 1).product_name("My watch");
let mut buf = [0u8; 2048];
let sink = write(SliceSink::new(&mut buf), Options::new(), |w| -> Result<(), Error> {
    w.start(start, &watch)?;
    w.session(start, Sport::RUNNING, SubSport::TREADMILL)?;
    for s in 0..60 {
        w.record(&Record::new(start.checked_add(s).unwrap()).heart_rate(140))?;
    }
    w.end(start.checked_add(60).unwrap())
})
.unwrap();
assert_eq!(&sink.written()[8..12], b".FIT");

§Errors

WriteError::Sink with the sink’s error, or WriteError::Source with the source’s, which includes the Errors it passed on.