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
66
67
68
69
70
71
72
use super::consts::*;
use super::slab::*;
use super::slab_def::*;
use super::slab_kind::*;

#[derive(Debug, Default)]
pub struct Row {
    /// Slabs containing the actual data
    pub slabs: [Slab; WIDTH_MAX],
}

impl Row {
    /// Create a new row, with a given slab def.
    ///
    /// # Examples
    ///
    /// ```
    /// use babalcore::*;
    ///
    /// let _row = Row::new(SlabDef::Floor);
    /// ```
    pub fn new(def: SlabDef) -> Row {
        Row {
            slabs: [Slab::new(def); WIDTH_MAX],
        }
    }

    /// Get a row content, at a given time.
    ///
    /// # Examples
    ///
    /// ```
    /// use babalcore::*;
    ///
    /// let row = Row::new(SlabDef::Floor);
    /// assert_eq!(SlabKind::Floor, row.get(123, 5));
    /// ```
    pub fn get(&self, now_msec: i64, col: isize) -> SlabKind {
        if col < 0 || col >= WIDTH_MAX as isize {
            return SlabKind::Void;
        }
        self.slabs[col as usize].get(now_msec)
    }
}

impl std::fmt::Display for Row {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str_list: Vec<String> = self
            .slabs
            .iter()
            .map(|x| format!("{}", x).to_string())
            .collect();
        write!(f, "{}", str_list.join(""))
    }
}

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

    #[test]
    fn test_fmt() {
        assert_eq!(
            "                                ",
            format!("{}", Row::new(SlabDef::Void))
        );
        assert_eq!(
            "################################",
            format!("{}", Row::new(SlabDef::Floor))
        );
    }
}