Skip to main content

ez_tui/forms/
node.rs

1use crate::{EzCptIds, Form, FormField, LayoutItem};
2use ratatui::layout::{Constraint, Direction};
3
4/// A form node is either a field or whole sub-form.
5#[derive(Debug)]
6pub enum FormNode<FID>
7where
8    FID: EzCptIds,
9{
10    /// A single field in the form.
11    Field(FormField<FID>),
12    /// A sub-form that can contain multiple fields or other sub-forms.
13    Form(Form<FID>),
14}
15impl<FID> FormNode<FID>
16where
17    FID: EzCptIds,
18{
19    pub(crate) fn as_layout_item(&self, _direction: Direction, _count: usize) -> LayoutItem<FID> {
20        match self {
21            FormNode::Field(field) => LayoutItem::Component(field.id().clone()),
22            FormNode::Form(form) => LayoutItem::NestedLayout(form.custom_layout()),
23        }
24    }
25    pub(crate) fn as_constraint(&self, direction: Direction, count: usize) -> Constraint {
26        match self {
27            FormNode::Field(field) => field.as_constraint(direction, count),
28            FormNode::Form(form) => form.as_constraint(direction, count),
29        }
30    }
31}