diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-13 15:30:16 +0100 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-11-13 15:30:16 +0100 |
commit | a85725eede89f8d7fecd10dc0628a01e48cccd7d (patch) | |
tree | 2ac1b7473b95b0791552988b6f9b6294675348f2 /src/configuration/uapi/set.rs | |
parent | Work on UAPI serialize device (diff) | |
download | wireguard-rs-a85725eede89f8d7fecd10dc0628a01e48cccd7d.tar.xz wireguard-rs-a85725eede89f8d7fecd10dc0628a01e48cccd7d.zip |
Initial version of full UAPI parser
Diffstat (limited to '')
-rw-r--r-- | src/configuration/uapi/set.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/configuration/uapi/set.rs b/src/configuration/uapi/set.rs index c609d83..4c2c554 100644 --- a/src/configuration/uapi/set.rs +++ b/src/configuration/uapi/set.rs @@ -13,12 +13,19 @@ enum ParserState { Interface, } -struct LineParser<C: Configuration> { - config: C, +pub struct LineParser<'a, C: Configuration> { + config: &'a C, state: ParserState, } -impl<C: Configuration> LineParser<C> { +impl<'a, C: Configuration> LineParser<'a, C> { + pub fn new(config: &'a C) -> LineParser<'a, C> { + LineParser { + config, + state: ParserState::Interface, + } + } + fn new_peer(value: &str) -> Result<ParserState, ConfigError> { match <[u8; 32]>::from_hex(value) { Ok(pk) => Ok(ParserState::Peer { @@ -29,7 +36,7 @@ impl<C: Configuration> LineParser<C> { } } - fn parse_line(&mut self, key: &str, value: &str) -> Option<ConfigError> { + pub fn parse_line(&mut self, key: &str, value: &str) -> Result<(), ConfigError> { // add the peer if not update_only let flush_peer = |st: ParserState| -> ParserState { match st { @@ -48,7 +55,7 @@ impl<C: Configuration> LineParser<C> { }; // parse line and update parser state - match self.state { + self.state = match self.state { // configure the interface ParserState::Interface => match key { // opt: set private key @@ -199,8 +206,8 @@ impl<C: Configuration> LineParser<C> { // unknown key _ => Err(ConfigError::InvalidKey), }, - } - .map(|st| self.state = st) - .err() + }?; + + Ok(()) } } |