aboutsummaryrefslogtreecommitdiffstats
path: root/src/configuration/uapi/mod.rs
blob: cba156d354a09dbfa14fedf3047547e8d41952d9 (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
mod get;
mod set;

use std::io::{Read, Write};

use super::{ConfigError, Configuration};

const MAX_LINE_LENGTH: usize = 128;

struct Parser<C: Configuration, R: Read, W: Write> {
    config: C,
    reader: R,
    writer: W,
}

impl<C: Configuration, R: Read, W: Write> Parser<C, R, W> {
    fn new(&self, reader: R, writer: W, config: C) -> Parser<C, R, W> {
        Parser {
            config,
            reader,
            writer,
        }
    }

    fn parse(&mut self) -> Option<()> {
        // read string up to maximum length (why is this not in std?)
        let mut line = || {
            let mut m: [u8; 1] = [0u8];
            let mut l: String = String::with_capacity(MAX_LINE_LENGTH);
            while let Ok(_) = self.reader.read_exact(&mut m) {
                let c = m[0] as char;
                if c == '\n' {
                    return Some(l);
                };
                l.push(c);
                if l.len() > MAX_LINE_LENGTH {
                    break;
                }
            }
            None
        };

        match line()?.as_str() {
            "get=1" => Some(()),
            "set=1" => Some(()),
            _ => None,
        }
    }
}