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
73
74
75
76
77
78
79
80
81
82
//! # Fill
//!
use a1_notation::Row;
use serde::{Deserialize, Serialize};
use std::cmp;

mod into;

pub(crate) const ROW_MAX: usize = 1000;

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub struct Fill {
    pub amount: Option<usize>,
    pub start_row: Row,
}

impl Fill {
    #[must_use]
    pub(crate) fn clone_to_row<R: Into<Row>>(&self, row: R) -> Self {
        Self {
            amount: self.amount,
            start_row: row.into(),
        }
    }

    #[must_use]
    pub(crate) fn end_row(&self) -> Row {
        if let Some(a) = self.amount {
            cmp::min(self.start_row.y + a, ROW_MAX).into()
        } else {
            ROW_MAX.into()
        }
    }

    pub fn new<R: Into<Row>>(start_row: R, amount: Option<usize>) -> Self {
        Self {
            amount,
            start_row: start_row.into(),
        }
    }
}

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

    #[test]
    fn clone_to_row() {
        let fill = Fill {
            amount: Some(10),
            start_row: 0.into(),
        };

        assert_eq!(
            fill.clone_to_row(55),
            Fill {
                amount: Some(10),
                start_row: 55.into()
            }
        );
    }

    #[test]
    fn end_row_with_amount() {
        let fill = Fill {
            amount: Some(10),
            start_row: 0.into(),
        };

        assert_eq!(fill.end_row(), 10.into());
    }

    #[test]
    fn end_row_without_amount() {
        let fill = Fill {
            amount: None,
            start_row: 0.into(),
        };

        assert_eq!(fill.end_row(), 1000.into());
    }
}