Lines
85.06 %
Functions
49.06 %
Branches
100 %
//! Static form descriptor types.
use crate::transaction::PAGING_KEYS;
/// The cell of a nested row naming the key of its parent row.
pub const LINK_CELL: &str = "row";
/// The cell of a parent row holding the key its nested rows name.
pub const PARENT_KEY_CELL: &str = "key";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
Accounts,
Commodities,
Templates,
Scripts,
}
impl Source {
/// The name used in descriptors, options requests and rendered forms.
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Accounts => "accounts",
Self::Commodities => "commodities",
Self::Templates => "templates",
Self::Scripts => "scripts",
pub fn by_name(name: &str) -> Option<Self> {
[
Self::Accounts,
Self::Commodities,
Self::Templates,
Self::Scripts,
]
.into_iter()
.find(|source| source.name() == name)
/// Free-text suggestions a text cell offers, served by `form-options` as
/// plain strings rather than picker options.
pub enum Suggest {
TagNames(Scope),
/// The values of the tag of this scope named by the sibling `name` cell.
TagValues(Scope),
impl Suggest {
/// The name used in descriptors and options requests.
Self::TagNames(Scope::Transaction) => "transaction-tag-names",
Self::TagNames(Scope::Split) => "split-tag-names",
Self::TagNames(Scope::Account) => "account-tag-names",
Self::TagNames(Scope::Commodity) => "commodity-tag-names",
Self::TagValues(Scope::Transaction) => "transaction-tag-values",
Self::TagValues(Scope::Split) => "split-tag-values",
Self::TagValues(Scope::Account) => "account-tag-values",
Self::TagValues(Scope::Commodity) => "commodity-tag-values",
Scope::ALL
.flat_map(|scope| [Self::TagNames(scope), Self::TagValues(scope)])
.find(|suggest| suggest.name() == name)
/// The kind of entity whose tags a text cell suggests.
pub enum Scope {
Transaction,
Split,
Account,
Commodity,
impl Scope {
const ALL: [Self; 4] = [
Self::Transaction,
Self::Split,
Self::Account,
Self::Commodity,
];
/// The seven field kinds; another needs owner sign-off.
pub enum Kind {
Text {
multiline: bool,
suggest: Option<Suggest>,
},
Select {
source: Source,
/// The cell of the same row a pick fills when it is empty, from the
/// `:commodity` the picked option carries.
fills: Option<&'static str>,
/// What an empty cell means, shown by clients as its hint.
empty: Option<&'static str>,
Amount,
/// `whole_day` asks for a calendar day, which `range_end` turns into the
/// last instant of that day; otherwise an instant is asked for.
Date {
range_end: bool,
whole_day: bool,
/// A `nested_under` rows field belongs to the rows field of that key: its
/// [`LINK_CELL`] holds the [`PARENT_KEY_CELL`] value of the owning row.
Rows {
fields: &'static [Field],
nested_under: Option<&'static str>,
Hidden,
/// A fixed list of `(key, label)` options; the key is submitted.
Choice {
options: &'static [(&'static str, &'static str)],
default: Option<&'static str>,
pub struct Field {
pub key: &'static str,
pub label: &'static str,
pub kind: Kind,
pub required: bool,
impl Field {
pub const fn nested_under(&self) -> Option<&'static str> {
match self.kind {
Kind::Rows { nested_under, .. } => nested_under,
_ => None,
pub const fn is_nested(&self) -> bool {
self.nested_under().is_some()
/// Whether the field pages a result instead of filtering it.
pub fn is_paging(&self) -> bool {
PAGING_KEYS.contains(&self.key)
pub(crate) const fn field(
key: &'static str,
label: &'static str,
kind: Kind,
required: bool,
) -> Field {
Field {
key,
label,
kind,
required,
pub(crate) const TEXT: Kind = Kind::Text {
multiline: false,
suggest: None,
};
pub(crate) const ACCOUNT: Kind = Kind::Select {
source: Source::Accounts,
fills: None,
empty: None,
pub(crate) const COMMODITY: Kind = Kind::Select {
source: Source::Commodities,
pub enum Action {
Mutation,
Query,
LoadMutation,
pub struct Form {
pub name: &'static str,
pub title: &'static str,
pub fields: &'static [Field],
pub action: Action,
fn has_source(fields: &[Field], source: Source) -> bool {
fields.iter().any(|field| match field.kind {
Kind::Select { source: own, .. } => own == source,
Kind::Rows { fields, .. } => has_source(fields, source),
_ => false,
})
fn has_select(fields: &[Field]) -> bool {
Kind::Select { .. } => true,
Kind::Rows { fields, .. } => has_select(fields),
impl Form {
pub fn has_select(&self) -> bool {
has_select(self.fields)
pub fn has_source(&self, source: Source) -> bool {
has_source(self.fields, source)
/// The fields that filter a result: all but the paging ones.
pub fn filter_fields(&self) -> impl Iterator<Item = &'static Field> {
self.fields.iter().filter(|field| !field.is_paging())
pub fn nested_keys(&self) -> impl Iterator<Item = &'static str> {
self.fields
.iter()
.filter(|field| field.is_nested())
.map(|field| field.key)