//! 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. use super::automations::said; use super::{ColumnType, TableDefinition}; /// Why a table is not one a thought is filed into. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Unfileable { /// 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 }, } impl Unfileable { /// The refusal a person reads, naming the table and what stands in the way. pub fn sentence(&self, definition: &TableDefinition) -> String { match self { 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`). pub fn 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. pub fn fileable(definition: &TableDefinition, source: &str) -> Result<(), Unfileable> { if definition.id == source { return Err(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) { return Err(Unfileable::TitleNotTyped); } if definition.add == Some(false) { return Err(Unfileable::NotAdded); } if definition.setup == Some(true) { return Err(Unfileable::Settings); } match definition.column(&definition.title) { Some(title) if title.readonly != Some(true) => {} _ => return Err(Unfileable::TitleNotTyped), } if let Some(column) = definition.columns.iter().find(|column| { column.key != definition.title && column.required == Some(true) && column.default.is_none() }) { return Err(Unfileable::NeedsMore { column: column.key.clone(), }); } if definition.made_by_verb != Some(true) { if let Some(column) = parent_link(definition) { return Err(Unfileable::BelongsToAParent { column: column.to_string(), }); } } Ok(()) } /// [`fileable`], as a yes or no. pub fn 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. pub fn fileable_tables<'a>(all: &'a [TableDefinition], source: &str) -> Vec<&'a TableDefinition> { let mut 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;