use fig::Value;
use flower_core::schema::{Constraint, FieldRule};
use flower_core::{
Consequence, FieldType, Icon, PathPat, Presentation, SegPat, Severity, Term, Tint,
};
pub fn path(segments: &[&str]) -> PathPat {
PathPat(
segments
.iter()
.map(|s| match *s {
"*" => SegPat::AnyKey,
"[]" => SegPat::EachItem,
key => SegPat::Key(key.to_string()),
})
.collect(),
)
}
pub fn present(title: &str, icon: Icon) -> Presentation {
Presentation::default().title(title).icon(icon)
}
pub fn term(value: &str, gloss: &str) -> Term {
Term::value(value).description_opt((!gloss.is_empty()).then_some(gloss))
}
pub fn text(at: PathPat, title: &str, icon: Icon) -> FieldRule {
FieldRule::new(at)
.ty(FieldType::Str)
.present(present(title, icon))
}
pub fn toggle(at: PathPat, title: &str) -> FieldRule {
FieldRule::new(at)
.ty(FieldType::Bool)
.present(present(title, Icon::Toggle))
}
pub fn choice(at: PathPat, title: &str, icon: Icon, values: &[(&str, &str)]) -> FieldRule {
choice_terms(
at,
title,
icon,
values.iter().map(|(v, g)| term(v, g)).collect(),
)
}
pub fn choice_terms(at: PathPat, title: &str, icon: Icon, values: Vec<Term>) -> FieldRule {
FieldRule::new(at)
.ty(FieldType::Str)
.constraint(Constraint::Enum {
values,
closed: true,
})
.present(present(title, icon))
}
pub fn open_choice(at: PathPat, title: &str, icon: Icon, values: &[(&str, &str)]) -> FieldRule {
open_choice_terms(
at,
title,
icon,
values.iter().map(|(v, g)| term(v, g)).collect(),
)
}
pub fn open_choice_terms(at: PathPat, title: &str, icon: Icon, values: Vec<Term>) -> FieldRule {
FieldRule::new(at)
.ty(FieldType::Str)
.constraint(Constraint::Enum {
values,
closed: false,
})
.present(present(title, icon))
}
pub fn costly(mut rule: FieldRule, tint: Tint, why: &str) -> FieldRule {
let present = std::mem::take(&mut rule.present);
rule.present = present.tint(tint).description(why);
rule.on_change(Consequence::always(why).severity(Severity::Confirm))
}
pub fn costly_when(
rule: FieldRule,
value: impl Into<Value>,
severity: Severity,
why: &str,
) -> FieldRule {
rule.on_change(Consequence::when(value, why).severity(severity))
}