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::{num::ParseIntError, str::FromStr};

pub enum Step {
    Fixed(isize),
    Percent(i8),
}

impl Default for Step {
    fn default() -> Self {
        Self::Fixed(0)
    }
}

impl FromStr for Step {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(if let Some(s) = s.strip_suffix('%') {
            Self::Percent(s.parse()?)
        } else {
            Self::Fixed(s.parse()?)
        })
    }
}

impl From<isize> for Step {
    fn from(n: isize) -> Self {
        Self::Fixed(n)
    }
}

impl From<usize> for Step {
    fn from(n: usize) -> Self {
        Self::Fixed(n as isize)
    }
}

impl Step {
    #[inline]
    fn fixed<F: FnOnce() -> usize>(self, f: F) -> isize {
        match self {
            Self::Fixed(n) => n,
            Self::Percent(0) => 0,
            Self::Percent(n) => n as isize * f() as isize / 100,
        }
    }

    #[inline]
    pub fn add<F: FnOnce() -> usize>(self, pos: usize, f: F) -> usize {
        let fixed = self.fixed(f);
        if fixed > 0 {
            pos + fixed as usize
        } else {
            pos.saturating_sub(fixed.unsigned_abs())
        }
    }

    #[inline]
    pub fn is_positive(&self) -> bool {
        match *self {
            Self::Fixed(n) => n > 0,
            Self::Percent(n) => n > 0,
        }
    }
}