protoflight 0.1.3

Protoflight flight controller.
#![cfg(all(feature = "host", feature = "blackbox"))]

use embassy_futures::yield_now;
#[cfg(feature = "std")]
use std::{fs::File, io::Write};

#[allow(async_fn_in_trait)]
pub trait SdStorage {
    async fn write_all(&mut self, data: &[u8]) -> Result<(), ()>;
    async fn flush(&mut self);
}

pub struct MockSdCard {
    #[cfg(feature = "std")]
    file: File,
}

impl MockSdCard {
    /// # Panics
    #[allow(clippy::expect_used)]
    pub fn new(path: &str) -> Self {
        _ = path;
        Self {
            #[cfg(feature = "std")]
            file: File::create(path).expect("Could not create log file"),
        }
    }
}

impl SdStorage for MockSdCard {
    async fn write_all(&mut self, data: &[u8]) -> Result<(), ()> {
        if data.is_empty() {
            return Ok(());
        }
        #[cfg(feature = "std")]
        self.file.write_all(data).map_err(|_| ())?;
        #[cfg(feature = "std")]
        {
            _ = self.file.flush().ok();
        }
        yield_now().await;
        Ok(())
    }

    async fn flush(&mut self) {
        #[cfg(feature = "std")]
        {
            _ = self.file.flush().ok();
        }
        yield_now().await;
    }
}