From 9dbfbd0dc36d14fb76014c276f458e98af4055af Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Wed, 10 Mar 2021 11:26:40 +0100 Subject: [PATCH] View: return String instead of id --- src/ui/layout/mod.rs | 4 ++-- src/ui/layout/view.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ui/layout/mod.rs b/src/ui/layout/mod.rs index e3f962e..1ba7201 100644 --- a/src/ui/layout/mod.rs +++ b/src/ui/layout/mod.rs @@ -41,7 +41,7 @@ use tui::widgets::Widget; /// /// Msg is an enum returned after an event is raised for a certain component /// Yep, I took inspiration from Elm. -#[derive(std::fmt::Debug, PartialEq)] +#[derive(std::fmt::Debug, PartialEq, Eq)] pub enum Msg { OnSubmit(Payload), OnChange(Payload), @@ -52,7 +52,7 @@ pub enum Msg { /// ## Payload /// /// Payload describes a component value -#[derive(std::fmt::Debug, PartialEq)] +#[derive(std::fmt::Debug, PartialEq, Eq)] pub enum Payload { Text(String), Signed(isize), diff --git a/src/ui/layout/view.rs b/src/ui/layout/view.rs index c42890f..8b3cc12 100644 --- a/src/ui/layout/view.rs +++ b/src/ui/layout/view.rs @@ -85,10 +85,10 @@ impl View { /// /// Update component properties /// Returns `None` if component doesn't exist - pub fn update<'a>(&mut self, id: &'a str, props: Props) -> Option<(&'a str, Msg)> { + pub fn update(&mut self, id: &str, props: Props) -> Option<(String, Msg)> { match self.components.get_mut(id) { None => None, - Some(cmp) => Some((id, cmp.update(props))), + Some(cmp) => Some((id.to_string(), cmp.update(props))), } } @@ -110,12 +110,12 @@ impl View { /// /// Handle event for the focused component (if any) /// Returns `None` if no component is focused - pub fn on(&mut self, ev: InputEvent) -> Option<(&str, Msg)> { + pub fn on(&mut self, ev: InputEvent) -> Option<(String, Msg)> { match self.focus.as_ref() { None => None, Some(id) => match self.components.get_mut(id) { None => None, - Some(cmp) => Some((id, cmp.on(ev))), + Some(cmp) => Some((id.to_string(), cmp.on(ev))), }, } } @@ -341,7 +341,7 @@ mod tests { assert_eq!( view.on(InputEvent::Key(KeyEvent::from(KeyCode::Char('1')))) .unwrap(), - (input, Msg::None) + (input.to_string(), Msg::None) ); // Verify new value assert_eq!( @@ -352,7 +352,7 @@ mod tests { assert_eq!( view.on(InputEvent::Key(KeyEvent::from(KeyCode::Enter))) .unwrap(), - (input, Msg::OnSubmit(Payload::Text(String::from("text1")))) + (input.to_string(), Msg::OnSubmit(Payload::Text(String::from("text1")))) ); }