aboutsummaryrefslogtreecommitdiffstats
path: root/src/creator.rs
blob: 3966470094ba88dd4c99940937e9a5f24c1b8e2f (plain) (blame)
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
use std::convert::TryFrom as _;

#[derive(Debug, Clone)]
pub struct Creator {
    base_time: Option<std::time::Instant>,
}

impl Creator {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn frame(
        &mut self,
        cur_time: std::time::Instant,
        data: &[u8],
    ) -> crate::error::Result<Vec<u8>> {
        let base_time = if let Some(base_time) = &self.base_time {
            base_time
        } else {
            self.base_time = Some(cur_time);
            self.base_time.as_ref().unwrap()
        };
        Vec::<u8>::try_from(crate::frame::Frame {
            time: cur_time - *base_time,
            data: data.to_vec(),
        })
    }
}

impl Default for Creator {
    fn default() -> Self {
        Self { base_time: None }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_basic() {
        let mut creator = Creator::new();
        let base_time = std::time::Instant::now();
        assert_eq!(
            creator.frame(base_time, b"").unwrap(),
            vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );
        assert_eq!(
            creator
                .frame(
                    base_time + std::time::Duration::new(38, 123_456_000),
                    b"\x1b[2Jfoobar"
                )
                .unwrap(),
            vec![
                38, 0, 0, 0, 64, 226, 1, 0, 10, 0, 0, 0, 27, 91, 50, 74, 102,
                111, 111, 98, 97, 114,
            ],
        );
    }
}