From 75d52e385fa66d6e151c9baa2cf22c2223c39ff0 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 2 Nov 2018 04:38:38 +0100 Subject: MapAction::parse(): Try to return a `Result` (WIP) Want to be able to return a `Result` here so we can print the error from the `ffi` module so we don't have to permit 'stderrlog' on this parser module. Beset with errors like this: error[E0506]: cannot assign to `self.action` because it is borrowed --> src/parser.rs:188:21 | 176 | Action::String(ref s) => { | ----- borrow of `self.action` occurs here ... 188 | self.action = action; | ^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.action` occurs here error: aborting due to previous error Even cloning the `Action` doesn't work because the Combine error `Result` needs an `&str` reference to the parser input, and if we reassign `self.action`, that string reference would disappear. Need to figure out a different way of dealing with this, but I at least want to commit what I have because the next step is going to be something different. --- src/parser.rs | 107 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 80 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/parser.rs b/src/parser.rs index fe5b413..f943ab3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -27,7 +27,7 @@ pub enum HeadphoneButton { } type Trigger = Vec; -#[derive(Debug)] +#[derive(Clone, Debug)] struct Character(autopilot::key::Character); impl PartialEq for Character { @@ -44,7 +44,7 @@ impl Character { } } -#[derive(Debug)] +#[derive(Clone, Debug)] struct KeyCode(autopilot::key::Code); impl PartialEq for KeyCode { @@ -61,7 +61,7 @@ impl KeyCode { } } -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] enum KeyboardKey { Character(Character), KeyCode(KeyCode), @@ -69,7 +69,7 @@ enum KeyboardKey { Nop, } -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub struct KeyboardKeyWithModifiers { key: KeyboardKey, flags: Vec, @@ -103,12 +103,30 @@ impl KeyboardKeyWithModifiers { } } -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub enum Action { String(String), Map(Vec), } +impl Action { + pub fn parse( + &self, + ) -> Result, CombineErrors> { + match self { + Action::String(s) => { + let input = State::new(s.as_str()); + + action_map() + .easy_parse(input) + .map(|t| t.0) + .map(|action| Some(action)) + }, + _ => Ok(None), + } + } +} + #[repr(C)] #[derive(Debug, PartialEq)] pub enum MapKind { @@ -123,35 +141,70 @@ pub struct MapAction { } impl MapAction { - pub fn parse(&mut self) { + pub fn parse( + &mut self, + ) -> Result<(), CombineErrors> { + use std::mem; match self.kind { MapKind::Map => { - let action = match self.action { - Action::String(ref s) => { - let input = State::new(s.as_str()); - - match action_map() - .easy_parse(input) - .map(|t| t.0) - { - Ok(a) => Some(a), - Err(e) => { - error!("{}", e); - - None - }, - } - }, - _ => None, - }; - if let Some(action) = action { - self.action = action; + // match self.action { + // Action::String(ref s) => { + // let input = State::new(s.as_str()); + // + // // match action_map() + // // .easy_parse(input) + // // .map(|t| t.0) + // // { + // // Ok(a) => Some(a), + // // Err(e) => { + // // error!("{}", e); + // // + // // None + // // }, + // // } + // let parsed_action = action_map() + // .easy_parse(input) + // .map(|t| t.0)?; + // + // }, + // _ => (), + // }; + + // let yo = self.action; + // let parsed_action = self.action.parse()?; + let yo = self.action.clone(); + let parsed_action = match yo { + Action::String(s) => { + let input = State::new(s.as_str()); + + action_map() + .easy_parse(input) + .map(|t| t.0) + .map(|action| Some(action)) + }, + _ => Ok(None), + }?; + + if let Some(action) = parsed_action { + // self.action = action; + mem::replace(&mut self.action, action); } + + // self.action = { + // let parsed_action = self.action.parse()?; + // + // match parsed_action { + // Some(a) => a, + // None => self.action, + // } + // }; }, // Commands don't get parsed. They remain `Action::String`s. MapKind::Command => (), - } + }; + + Ok(()) } } -- cgit v1.2.3