~ruther/map-led-strip

ref: 8ae60ccfb8f93b1bc11585503f27fc2a501e6ed9 map-led-strip/src/commands/command_argument.rs -rw-r--r-- 1.2 KiB
8ae60ccf — František Boháček feat: add command handling support 1 year, 9 months ago
                                                                                
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 esp_println::{print, println};

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct CommandArgument<'d>
{
    pub data: &'d [char],
}

impl<'d> CommandArgument<'d>
{
    pub fn new(data: &'d [char]) -> Self
    {
        Self {
            data
        }
    }

    pub fn chars(&self) -> &'d [char]
    {
        self.data
    }

    pub fn try_to_integer(&self) -> Option<u32>
    {
        let length = self.data.len();
        let mut multiplier = 1u32;
        for _ in 0..length - 1 {
            multiplier *= 10;
        }

        let mut result = 0u32;

        for c in self.chars().iter() {
            let num = (*c as u32) - (b'0' as u32);
            if num > 9 {
                return None;
            }

            result += num * multiplier;
            multiplier /= 10;
        }

        Some(result)
    }

    pub fn compare(&self, to: &str) -> bool {
        if self.data.len() != to.len() {
            return false;
        }

        let to = to.as_bytes();
        for (i, c) in self.data.iter().enumerate() {
            let compare_against = to[i];

            if compare_against != (*c) as u8 {
                return false;
            }
        }

        true
    }
}
Do not follow this link