Lines
92.5 %
Functions
50 %
Branches
100 %
pub mod amount;
pub mod date;
pub mod editor;
pub mod grid;
mod hints;
pub mod rows;
pub mod select;
pub mod splits;
pub mod table_panel;
pub use amount::AmountWidget;
pub use date::DateWidget;
pub use editor::{Editor, TEXT_AREA_ROWS};
pub use hints::{Hint, Hints, completion, matching};
pub use rows::RowsWidget;
pub use select::{SelectOption, SelectWidget, with_none};
pub use splits::{FocusedSubWidget, SplitRow, SplitsWidget};
pub use table_panel::TablePanel;
#[cfg(test)]
mod tests;
/// Discriminant for widget-first key routing without carrying widget data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WidgetKind {
Text,
TextArea,
Select,
Amount,
Date,
Splits,
Rows,
}
/// The primary widget union — enum dispatch, no `Box<dyn>`, derives `Debug`.
#[derive(Debug)]
pub enum Widget {
Text(Editor),
Select(SelectWidget),
Amount(AmountWidget),
Date(DateWidget),
Splits(SplitsWidget),
Rows(RowsWidget),
impl Widget {
/// The submittable string value of this widget.
///
/// Returns `""` for `Splits` and `Rows` (rows validated separately via [`SplitsWidget::rows`]).
#[must_use]
pub fn value(&self) -> &str {
match self {
Self::Text(ed) => ed.buffer(),
Self::Select(sw) => sw.value(),
Self::Amount(aw) => aw.value(),
Self::Date(dw) => dw.value(),
Self::Splits(_) | Self::Rows(_) => "",
/// Mutable reference to the inner [`Editor`] for `Text`, `Amount`, and
/// `Date` variants. Returns `None` for `Select`, `Splits` and `Rows`.
pub fn as_text_mut(&mut self) -> Option<&mut Editor> {
Self::Text(ed) => Some(ed),
Self::Amount(aw) => Some(aw.as_editor_mut()),
Self::Date(dw) => Some(dw.as_editor_mut()),
Self::Select(_) | Self::Splits(_) | Self::Rows(_) => None,
/// The editor of the focused cell of a grid, or of the field itself for the text-like
/// widgets; `None` on a select cell.
pub fn focused_editor_mut(&mut self) -> Option<&mut Editor> {
Self::Splits(sw) => sw.focused_editor_mut(),
Self::Rows(rows) => rows.focused_editor_mut(),
other => other.as_text_mut(),
/// Replaces a text-like widget's content or focuses a select's option by id;
/// splits take no text.
pub fn set_text(&mut self, text: &str) {
Self::Text(ed) => *ed = Editor::with_buffer(text).multiline(ed.is_multiline()),
Self::Amount(aw) => *aw = AmountWidget::with_value(text),
Self::Date(dw) => *dw = DateWidget::with_value(text),
Self::Select(sw) => sw.select_id(text),
Self::Splits(_) | Self::Rows(_) => {}
/// Variant discriminant used for widget-first key routing decisions; an
/// open tag list of the splits takes keys like a rows editor.
pub fn kind(&self) -> WidgetKind {
Self::Text(ed) if ed.is_multiline() => WidgetKind::TextArea,
Self::Text(_) => WidgetKind::Text,
Self::Select(_) => WidgetKind::Select,
Self::Amount(_) => WidgetKind::Amount,
Self::Date(_) => WidgetKind::Date,
Self::Splits(sw) if sw.tags_open() => WidgetKind::Rows,
Self::Splits(_) => WidgetKind::Splits,
Self::Rows(_) => WidgetKind::Rows,