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
use std::marker::PhantomData;

/// WS2812 ESP32 RMT Driver error.
#[derive(thiserror::Error, Debug)]
#[error("mock Ws2812Esp32RmtDriverError")]
pub struct Ws2812Esp32RmtDriverError;

/// Mock of Low-level WS2812 ESP32 RMT driver.
///
/// If the target vendor does not equals to "espressif", this mock is used instead of genuine
/// Low-level WS2812 ESP32 RMT driver.
pub struct Ws2812Esp32RmtDriver<'d> {
    /// Pixel binary array to be written
    pub pixel_data: Option<Vec<u8>>,

    /// Dummy phantom to take care of lifetime
    phantom: PhantomData<&'d Option<Vec<u8>>>,
}

impl<'d> Ws2812Esp32RmtDriver<'d> {
    /// Creates a mock of `Ws2812Esp32RmtDriver`.
    pub fn new() -> Result<Self, Ws2812Esp32RmtDriverError> {
        Ok(Self {
            pixel_data: None,
            phantom: Default::default(),
        })
    }

    /// Writes a pixel-byte sequence.
    pub fn write_blocking<'a, 'b, T>(
        &'a mut self,
        pixel_sequence: T,
    ) -> Result<(), Ws2812Esp32RmtDriverError>
    where
        'b: 'a,
        T: Iterator<Item = u8> + Send + 'b,
    {
        self.pixel_data = Some(pixel_sequence.collect());
        Ok(())
    }

    /// Writes a pixel-byte sequence.
    #[cfg(feature = "unstable")]
    pub fn write<'a, 'b, T>(
        &'a mut self,
        pixel_sequence: T,
    ) -> Result<(), Ws2812Esp32RmtDriverError>
    where
        'b: 'a,
        T: Iterator<Item = u8> + Send + 'b,
    {
        self.pixel_data = Some(pixel_sequence.collect());
        Ok(())
    }
}

#[test]
fn test_ws2812_esp32_rmt_driver_mock() {
    let sample_data: [u8; 6] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05];

    let mut driver = Ws2812Esp32RmtDriver::new().unwrap();
    assert_eq!(driver.pixel_data, None);
    driver.write_blocking(sample_data.iter().cloned()).unwrap();
    assert_eq!(driver.pixel_data.unwrap(), &sample_data);
}