#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Width {
ScreenTwoExtraLarge,
Full,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnSpan {
Columns(u8),
Full,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Component {
Field(String),
Section(Section),
Group(Group),
}
impl From<&str> for Component {
fn from(name: &str) -> Self {
Component::Field(name.to_string())
}
}
impl From<String> for Component {
fn from(name: String) -> Self {
Component::Field(name)
}
}
impl From<Section> for Component {
fn from(section: Section) -> Self {
Component::Section(section)
}
}
impl From<Group> for Component {
fn from(group: Group) -> Self {
Component::Group(group)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Section {
heading: String,
description: Option<String>,
fields: Vec<String>,
column_span: ColumnSpan,
}
impl Section {
pub fn make(heading: impl Into<String>) -> Self {
Section {
heading: heading.into(),
description: None,
fields: Vec::new(),
column_span: ColumnSpan::Columns(1),
}
}
pub fn description(mut self, text: impl Into<String>) -> Self {
self.description = Some(text.into());
self
}
pub fn schema<I, S>(mut self, fields: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.fields.extend(fields.into_iter().map(Into::into));
self
}
pub fn column_span(mut self, columns: u8) -> Self {
self.column_span = ColumnSpan::Columns(columns.clamp(1, 12));
self
}
pub fn column_span_full(mut self) -> Self {
self.column_span = ColumnSpan::Full;
self
}
pub fn heading(&self) -> &str {
&self.heading
}
pub fn get_description(&self) -> Option<&str> {
self.description.as_deref()
}
pub fn fields(&self) -> &[String] {
&self.fields
}
pub fn get_column_span(&self) -> ColumnSpan {
self.column_span
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Group {
components: Vec<Component>,
column_span: ColumnSpan,
}
impl Group {
pub fn make<I, C>(components: I) -> Self
where
I: IntoIterator<Item = C>,
C: Into<Component>,
{
Group {
components: components.into_iter().map(Into::into).collect(),
column_span: ColumnSpan::Columns(1),
}
}
pub fn column_span(mut self, columns: u8) -> Self {
self.column_span = ColumnSpan::Columns(columns.clamp(1, 12));
self
}
pub fn column_span_full(mut self) -> Self {
self.column_span = ColumnSpan::Full;
self
}
pub fn components(&self) -> &[Component] {
&self.components
}
pub fn get_column_span(&self) -> ColumnSpan {
self.column_span
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FormLayout {
columns: Option<u8>,
components: Vec<Component>,
max_content_width: Option<Width>,
}
impl FormLayout {
pub fn new() -> Self {
FormLayout::default()
}
pub fn columns(mut self, columns: u8) -> Self {
self.columns = Some(columns.clamp(1, 12));
self
}
pub fn schema<I, C>(mut self, components: I) -> Self
where
I: IntoIterator<Item = C>,
C: Into<Component>,
{
self.components
.extend(components.into_iter().map(Into::into));
self
}
pub fn max_content_width(mut self, width: Width) -> Self {
self.max_content_width = Some(width);
self
}
pub fn is_empty(&self) -> bool {
self.columns.is_none() && self.components.is_empty() && self.max_content_width.is_none()
}
pub fn column_count(&self) -> Option<u8> {
self.columns
}
pub fn components(&self) -> &[Component] {
&self.components
}
pub fn content_width(&self) -> Option<Width> {
self.max_content_width
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_new_layout_is_empty() {
assert!(FormLayout::new().is_empty());
assert!(!FormLayout::new().columns(2).is_empty());
assert!(!FormLayout::new().max_content_width(Width::Full).is_empty());
}
#[test]
fn columns_and_spans_stay_between_1_and_12() {
assert_eq!(FormLayout::new().columns(0).column_count(), Some(1));
assert_eq!(FormLayout::new().columns(20).column_count(), Some(12));
assert_eq!(
Section::make("A").column_span(0).get_column_span(),
ColumnSpan::Columns(1)
);
assert_eq!(
Group::make(["a"]).column_span_full().get_column_span(),
ColumnSpan::Full
);
}
#[test]
fn schema_adds_in_order() {
let section = Section::make("Content").schema(["title"]).schema(["body"]);
assert_eq!(section.fields(), ["title", "body"]);
let layout = FormLayout::new().schema(["a"]).schema([section.clone()]);
assert_eq!(
layout.components(),
[Component::Field("a".into()), Component::Section(section)]
);
}
}