fit-encoder 0.1.0

Write FIT activity files (the Flexible and Interoperable Data Transfer format) as a stream: file header first, CRC last, sizes known in advance. no_std, no allocation. Independent; not affiliated with Garmin
Documentation
//! The public API as another crate sees it.

use fit_encoder::{
    write_async, AsyncOnePass, AsyncPatchSink, AsyncSink, AsyncWriter, Dev, Device, Error, Extra,
    FinishError, FnSink, Full, LapField, Manufacturer, OnePass, Options, PatchSink, Record,
    RecordField, Sink, SliceSink, Timestamp, VecSink,
};

#[test]
fn a_glob_import_adds_no_methods_to_numbers() {
    #[allow(unused_imports, reason = "what it brings in is under test")]
    use fit_encoder::*;
    assert!(3u8.le(&4));
    assert!(1.5f64.le(&2.0));
}

/// `<T as NotASink<_>>::item` compiles only if `T` is neither sink: if it were, which `A` is
/// meant would be ambiguous.
trait NotASink<A> {
    fn item() {}
}
impl<T: ?Sized> NotASink<()> for T {}
impl<T: ?Sized + Sink> NotASink<u8> for T {}
impl<T: ?Sized + AsyncSink> NotASink<u16> for T {}

#[test]
fn a_vec_gains_no_sink_methods() {
    use std::io::Write;
    <Vec<u8> as NotASink<_>>::item();
    let mut v = Vec::new();
    assert_eq!(v.write(b"FIT").unwrap(), 3);
}

/// A patch sink in memory, awaited.
struct Mem(VecSink);

#[allow(clippy::unused_async_trait_impl, reason = "a sync sink, awaited")]
impl AsyncSink for Mem {
    type Error = core::convert::Infallible;

    async fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        Sink::write(&mut self.0, bytes)
    }
}

#[allow(clippy::unused_async_trait_impl, reason = "a sync sink, awaited")]
impl AsyncPatchSink for Mem {
    async fn patch(&mut self, back: u32, bytes: &[u8]) -> Result<(), Self::Error> {
        PatchSink::patch(&mut self.0, back, bytes)
    }
}

fn send_sync<T: Send + Sync>() {}

#[test]
fn files_and_values_cross_threads() {
    send_sync::<OnePass<SliceSink<'static>>>();
    send_sync::<FinishError<OnePass<SliceSink<'static>>, Full>>();
    send_sync::<AsyncOnePass<Mem>>();
    send_sync::<RecordField<u8>>();
    send_sync::<LapField<str>>();
    send_sync::<Dev>();
    send_sync::<Extra<'static>>();
    send_sync::<Error>();
    send_sync::<Options>();
    send_sync::<Record>();
}

#[test]
fn async_writes_are_send() {
    fn send<T: Send>(_: T) {}
    async fn log(w: &mut AsyncWriter<'_, Mem>) -> Result<(), Error> {
        let t = Timestamp::FIRST_DATE;
        w.start(t, &Device::new(Manufacturer::DEVELOPMENT, 1)).await?;
        w.record(&Record::new(t).heart_rate(60)).await
    }
    send(write_async(Mem(VecSink::default()), Options::new(), async |w| log(w).await));
    let mut file = AsyncOnePass::new(Mem(VecSink::default()), Options::new());
    send(async move {
        let _ = log(&mut file.writer()).await;
        file.finish().await.is_ok()
    });
}

#[test]
fn a_bound_adds_no_methods_to_numbers() {
    fn at_most<T: fit_encoder::Scalar + PartialOrd + Copy>(a: T, b: T) -> bool {
        a.le(&b)
    }
    assert!(at_most(3u8, 4));
}

/// A sink whose error says nothing.
#[derive(Debug)]
struct Mute;

impl Sink for Mute {
    type Error = ();

    fn write(&mut self, _: &[u8]) -> Result<(), ()> {
        Ok(())
    }
}

impl PatchSink for Mute {
    fn patch(&mut self, _: u32, _: &[u8]) -> Result<(), ()> {
        Ok(())
    }
}

#[test]
fn a_failed_finish_displays_whatever_its_sink() {
    let file = OnePass::new(Mute, Options::new()).unwrap();
    assert_eq!(file.finish().unwrap_err().to_string(), Error::Unfinished.to_string());
}

#[test]
fn a_function_sink_debugs() {
    let sink = FnSink(|_: &[u8]| Ok::<(), ()>(()));
    assert_eq!(format!("{sink:?}"), "FnSink(..)");
}