From b90953f65e07d2e3d85e1fc1890e5942ccf6e9dd Mon Sep 17 00:00:00 2001 From: veeso Date: Sat, 6 Mar 2021 15:10:19 +0100 Subject: [PATCH] PropValue enum --- src/ui/layout/components/input.rs | 4 ++-- src/ui/layout/components/mod.rs | 2 +- src/ui/layout/mod.rs | 2 +- src/ui/layout/props.rs | 33 ++++++++++++++++++++++++------- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/ui/layout/components/input.rs b/src/ui/layout/components/input.rs index d478813..3d3a3d6 100644 --- a/src/ui/layout/components/input.rs +++ b/src/ui/layout/components/input.rs @@ -25,7 +25,7 @@ // locals use super::super::props::InputType; -use super::{Component, InputEvent, Msg, Payload, Props, PropsBuilder, Render}; +use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render}; // ext use crossterm::event::{KeyCode, KeyModifiers}; use tui::{ @@ -153,7 +153,7 @@ impl Input { // Initialize states let mut states: OwnStates = OwnStates::default(); // Set state value from props - if let Some(val) = props.value.as_ref() { + if let PropValue::Str(val) = props.value.clone() { for ch in val.chars() { states.append(ch, props.input_type); } diff --git a/src/ui/layout/components/mod.rs b/src/ui/layout/components/mod.rs index 678fa28..743be25 100644 --- a/src/ui/layout/components/mod.rs +++ b/src/ui/layout/components/mod.rs @@ -24,7 +24,7 @@ */ // imports -use super::{Component, InputEvent, Msg, Payload, Props, PropsBuilder, Render}; +use super::{Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder, Render}; // exports pub mod file_list; diff --git a/src/ui/layout/mod.rs b/src/ui/layout/mod.rs index a4a08b9..cf2d224 100644 --- a/src/ui/layout/mod.rs +++ b/src/ui/layout/mod.rs @@ -28,7 +28,7 @@ pub mod components; pub mod props; // locals -use props::{Props, PropsBuilder}; +use props::{Props, PropsBuilder, PropValue}; // ext use crossterm::event::Event as InputEvent; use crossterm::event::KeyEvent; diff --git a/src/ui/layout/props.rs b/src/ui/layout/props.rs index d89e3a2..b7662ac 100644 --- a/src/ui/layout/props.rs +++ b/src/ui/layout/props.rs @@ -43,7 +43,7 @@ pub struct Props { pub input_type: InputType, // Input type pub input_len: Option, // max input len pub texts: TextParts, // text parts - pub value: Option, // Initial value + pub value: PropValue, // Initial value } impl Default for Props { @@ -59,7 +59,7 @@ impl Default for Props { input_type: InputType::Text, input_len: None, texts: TextParts::default(), - value: None, + value: PropValue::None, } } } @@ -214,9 +214,9 @@ impl PropsBuilder { /// ### with_value /// /// Set initial value for component - pub fn with_value(&mut self, value: String) -> &mut Self { + pub fn with_value(&mut self, value: PropValue) -> &mut Self { if let Some(props) = self.props.as_mut() { - props.value = Some(value); + props.value = value; } self } @@ -259,6 +259,21 @@ impl Default for TextParts { } } +// -- Prop value + +/// ### PropValue +/// +/// PropValue describes a property initial value +#[derive(Clone, PartialEq, std::fmt::Debug)] +pub enum PropValue { + Str(String), + Unsigned(usize), + Signed(isize), + Float(f64), + Boolean(bool), + None, +} + // -- Input Type /// ## InputType @@ -288,7 +303,7 @@ mod tests { assert!(props.texts.title.is_none()); assert_eq!(props.input_type, InputType::Text); assert!(props.input_len.is_none()); - assert!(props.value.is_none()); + assert_eq!(props.value, PropValue::None); assert!(props.texts.body.is_none()); } @@ -318,7 +333,7 @@ mod tests { )) .with_input(InputType::Password) .with_input_len(16) - .with_value(String::from("Hello")) + .with_value(PropValue::Str(String::from("Hello"))) .build(); assert_eq!(props.background, Color::Blue); assert_eq!(props.bold, true); @@ -327,7 +342,11 @@ mod tests { assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello"); assert_eq!(props.input_type, InputType::Password); assert_eq!(*props.input_len.as_ref().unwrap(), 16); - assert_eq!(props.value.as_ref().unwrap().as_str(), "Hello"); + if let PropValue::Str(s) = props.value { + assert_eq!(s.as_str(), "Hello"); + } else { + panic!("Expected value to be a string"); + } assert_eq!( props.texts.body.as_ref().unwrap().get(0).unwrap().as_str(), "hey"