diff options
| author | Teddy Wing | 2018-11-02 04:38:38 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-11-02 04:38:38 +0100 | 
| commit | 75d52e385fa66d6e151c9baa2cf22c2223c39ff0 (patch) | |
| tree | aa80def97b6355338799818ab8353bdb3c44e8f2 /src | |
| parent | e4c21b11069297d289f25834cfc3c001a4604b5f (diff) | |
| download | dome-key-map-75d52e385fa66d6e151c9baa2cf22c2223c39ff0.tar.bz2 | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/parser.rs | 107 | 
1 files changed, 80 insertions, 27 deletions
| 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<HeadphoneButton>; -#[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<Flag>, @@ -103,12 +103,30 @@ impl KeyboardKeyWithModifiers {      }  } -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)]  pub enum Action {      String(String),      Map(Vec<KeyboardKeyWithModifiers>),  } +impl Action { +    pub fn parse( +        &self, +    ) -> Result<Option<Action>, CombineErrors<char, &str, SourcePosition>> { +        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<char, &str, SourcePosition>> { +        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(())      }  } | 
