use std::collections::HashMap;
use crate::{events::Event, rect_style::BorderStyle, screen::Screen};
mod choices;
pub mod constraints;
mod form;
mod text;
pub use choices::Checkbox;
pub use choices::Radio;
use crossterm::style::Color;
pub use form::Form;
pub use text::HiddenText;
pub use text::Text;
use self::constraints::FormConstraint;
pub trait FormField {
fn make(w: u32, options: FormOptions) -> Self
where
Self: Sized;
fn reset(&mut self);
fn get_width(&self) -> u32;
fn get_height(&self) -> u32;
fn get_min_height(&self) -> u32 {
1
}
fn resize(&mut self, w: u32, h: u32);
fn handle_event(&mut self, event: Event);
fn set_active(&mut self, active: bool);
fn is_active(&self) -> bool;
fn validate(&self, validation_result: &mut FormValidationResult);
fn get_output(&self) -> FormValue;
fn set_options(&mut self, options: FormOptions);
fn get_options(&self) -> &FormOptions;
fn should_display_label(&self) -> bool {
self.get_options().label.is_some()
}
fn draw(&mut self, tick: usize) -> &Screen;
fn self_validate(&self, validation_result: &mut FormValidationResult) {
let output = self.get_output();
for constraint in self.get_options().constraints.iter() {
if !constraint.validate(&output) {
validation_result.push(String::from(constraint.get_message()))
}
}
}
}
pub type FormValidationResult = Vec<String>;
#[derive(Debug, Clone)]
pub enum FormValue {
Nothing,
Boolean(bool),
Index(usize),
String(String),
List(Vec<String>),
Vec(Vec<FormValue>),
Map(HashMap<String, FormValue>),
}
impl Default for FormValue {
fn default() -> Self {
Self::Nothing
}
}
#[derive(Debug, Clone)]
pub enum FormError {
FieldNotFound,
ValidationFailed(FormValidationResult),
}
#[derive(Clone, Copy)]
pub struct FormStyle {
pub border: Option<BorderStyle>,
pub fg: Color,
pub bg: Color,
}
impl Default for FormStyle {
fn default() -> Self {
Self {
border: None,
fg: Color::Grey,
bg: Color::Black,
}
}
}
#[derive(Default)]
pub struct FormOptions {
pub style: FormStyle,
pub label: Option<&'static str>,
pub constraints: Vec<Box<dyn FormConstraint>>,
pub custom: HashMap<String, FormValue>,
}