diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-11 23:13:46 +0100 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-11 23:13:46 +0100 |
commit | 5b555a2e176bd5310d2efa614f67c96cb314eda4 (patch) | |
tree | b79cb04b38e052fb8ed63d7212020a1c8a31b6f2 /src/configuration/uapi/mod.rs | |
parent | Implemented UAPI "get" line-parser (diff) | |
download | wireguard-rs-5b555a2e176bd5310d2efa614f67c96cb314eda4.tar.xz wireguard-rs-5b555a2e176bd5310d2efa614f67c96cb314eda4.zip |
Work on UAPI serialize device
Diffstat (limited to 'src/configuration/uapi/mod.rs')
-rw-r--r-- | src/configuration/uapi/mod.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/configuration/uapi/mod.rs b/src/configuration/uapi/mod.rs index 5d89b94..cba156d 100644 --- a/src/configuration/uapi/mod.rs +++ b/src/configuration/uapi/mod.rs @@ -1,4 +1,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, + } + } +} |