//! Which tables a thought can be FILED into by its words alone — "File into…"//! on a thought, and the capture sweep filing one automatically.//!//! ONE rule, here. The app's picker (`TablePicking.choices` in//! `apps/cotton/Sources/CottonUI/Editors/TablePicker.swift`) is its Swift twin//! and offers exactly these; `file_thought` refuses any other table, and the//! capture sweep offers its model only these. A table is fileable when://!//! - it is not the table the thought is filed FROM;//! - something is added to it by typing (`add` is not false);//! - it is not part of how the app is set up (`setup`: never where words are//! filed);//! - its title column exists and is not read-only;//! - no other column is required without a default (the words are all there is);//! - its record does not belong to ONE parent through a link the person never//! sets — but a todo or a note is made by its own verb, whatever it links.usesuper::automations::said;usesuper::{ColumnType, TableDefinition};/// Why a table is not one a thought is filed into.#[derive(Debug, Clone, PartialEq, Eq)]pubenumUnfileable {/// The table the thought is filed from.Source,/// Nothing is added to it by typing.NotAdded,/// A table over a Settings source.Settings,/// Its title cannot take creation words: missing, read-only, or an alias/// of a non-addable engine projection.TitleNotTyped,/// Another column is required and has no default.NeedsMore { column: String },/// Its record belongs to one parent, through this link column.BelongsToAParent { column: String },}implUnfileable {/// The refusal a person reads, naming the table and what stands in the way.pubfn sentence(&self, definition: &TableDefinition) -> String {matchself {Unfileable::Source => said("a_thought_is_filed_into_another_table", &[("table", &definition.plural)]),Unfileable::NotAdded => said("nothing_is_added_by_hand_so_a_thought", &[("table", &definition.plural)]),Unfileable::Settings => said("is_part_of_how_the_app_is_set_up", &[("table", &definition.plural)]),Unfileable::TitleNotTyped => said("has_no_title_a_thought_s_words_can_go_in", &[("table", &definition.plural)]),Unfileable::NeedsMore { column } => said("needs_more_than_the_thought_s_words", &[("name", &definition.name), ("column", &definition.column(column).map_or_else(|| column.clone(), super::Column::label))], ),Unfileable::BelongsToAParent { column } => {let parent = definition .column(column) .and_then(|column| column.link_to.clone()) .unwrap_or_else(|| column.clone()); said("belongs_to_a_parent_add_it_from_that_page", &[("name", &definition.name.to_lowercase()), ("parent", &parent.replace('_', " "))], ) } } }}/// The link column a record of `definition` belongs to its one parent through:/// a single, read-only, not computed link — set by the parent's page, never/// by the person — that is not a link to the picture or file the record/// shows (`media`).pubfn parent_link(definition: &TableDefinition) -> Option<&str> { definition .columns .iter() .find(|column| { column.column_type == ColumnType::Link && column.readonly == Some(true) && column.multiple != Some(true) && column.computed.is_none() && column.media != Some(true) }) .map(|column| column.key.as_str())}/// Whether a thought in `source` can be filed into `definition` by its words/// alone, and if not, why.pubfn fileable(definition: &TableDefinition, source: &str) -> Result<(), Unfileable> {if definition.id == source {returnErr(Unfileable::Source); }// An engine projection may let a person rename an existing record while// providing no title input that creates one. An editable account alias is// not a creation title; keep that refusal even when its alias is writable.if definition.derived == Some(true) && definition.add == Some(false) {returnErr(Unfileable::TitleNotTyped); }if definition.add == Some(false) {returnErr(Unfileable::NotAdded); }if definition.setup == Some(true) {returnErr(Unfileable::Settings); }match definition.column(&definition.title) {Some(title) if title.readonly != Some(true) => {} _ => returnErr(Unfileable::TitleNotTyped), }ifletSome(column) = definition.columns.iter().find(|column| { column.key != definition.title && column.required == Some(true) && column.default.is_none() }) {returnErr(Unfileable::NeedsMore { column: column.key.clone(), }); }if definition.made_by_verb != Some(true) {ifletSome(column) = parent_link(definition) {returnErr(Unfileable::BelongsToAParent { column: column.to_string(), }); } }Ok(())}/// [`fileable`], as a yes or no.pubfn is_fileable(definition: &TableDefinition, source: &str) -> bool { fileable(definition, source).is_ok()}/// Every table of `all` a thought in `source` can be filed into: the usual/// ones first (`filing_rank`, lowest first), the rest by name, case aside —/// the picker's order.pubfn fileable_tables<'a>(all: &'a [TableDefinition], source: &str) -> Vec<&'a TableDefinition> {letmut offered: Vec<&TableDefinition> = all .iter() .filter(|definition| is_fileable(definition, source)) .collect();let rank = |definition: &TableDefinition| definition.filing_rank.unwrap_or(u32::MAX); offered.sort_by(|a, b| { rank(a) .cmp(&rank(b)) .then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())) }); offered}#[cfg(test)]#[path = "fileable_tests.rs"]mod tests;