//! What links to a record: any table with a link column to table T //! contributes a list to T's record page, found from the definitions alone, //! so a tool installed later appears on a person's page by itself. //! //! Named first, found second: a list the host's own `page` names is the //! host's to draw, and is not found again here. A link column may say //! `"contributes": false` (machinery: a message's reply_to) or //! `"contributes": {"label": "Lessons taken"}`; a join table's link says //! `"contributes": {"through": ""}` and the table //! that column points at is contributed in the join's place; the host may //! say `"contributions": false` (a secret, a device). use serde::{Deserialize, Serialize}; use super::{ColumnType, TableDefinition}; /// A link column's say in what it contributes to the record it points at: /// `false` for nothing, or the words its list is headed with. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(untagged)] pub enum Contributes { Flag(bool), Said(ContributesSaid), } /// The words a contributed list is headed with, and for a join table the /// other link column whose table is contributed in the join's place. At /// least one of the two. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ContributesSaid { #[serde(default, skip_serializing_if = "Option::is_none")] pub label: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub through: Option, } /// The join a contributed table is reached by: the records of the /// contributed table that `to` of `table` points at, for the rows of `table` /// whose `from` points at the page's record. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Through { pub table: String, pub from: String, pub to: String, } /// One list another table contributes to a host's record page, under /// `label`: the records of `table` whose `column` points at the record — or, /// reached by a join, the records of `table` that `through` leads to. /// Exactly one of `column` and `through`. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Contributor { pub table: String, #[serde(skip_serializing_if = "Option::is_none")] pub column: Option, pub label: String, #[serde(skip_serializing_if = "Option::is_none")] pub through: Option, } /// Every list the tables in `tables` contribute to a record of `host`, in /// the order the tables are given — none when `host` is not among them or /// says `contributions: false`. pub fn contributors(tables: &[TableDefinition], host: &str) -> Vec { let Some(host) = tables.iter().find(|table| table.id == host) else { return Vec::new(); }; if host.contributions == Some(false) { return Vec::new(); } // What the host's own page already draws: (table, link) of every list, // a list with no link naming its whole table. let named: Vec<(&str, Option<&str>)> = host .page .iter() .filter_map(|section| section.list.as_ref()) .flat_map(|list| { std::iter::once((list.table.as_str(), list.link.as_deref())).chain( list.with .iter() .flatten() .map(|with| (with.table.as_str(), Some(with.link.as_str()))), ) }) .collect(); let mut found = Vec::new(); for table in tables { for column in &table.columns { if column.column_type != ColumnType::Link || column.link_to.as_deref() != Some(&host.id) { continue; } let said = match &column.contributes { Some(Contributes::Flag(false)) => continue, Some(Contributes::Said(said)) => Some(said), Some(Contributes::Flag(true)) | None => None, }; let drawn = |id: &str, key: Option<&str>| { named.iter().any(|(named_table, link)| { *named_table == id && (link.is_none() || *link == key) }) }; if drawn(&table.id, Some(&column.key)) { continue; } let label = said.and_then(|said| said.label.clone()); let Some(to) = said.and_then(|said| said.through.as_deref()) else { found.push(Contributor { table: table.id.clone(), column: Some(column.key.clone()), label: label.unwrap_or_else(|| table.plural.clone()), through: None, }); continue; }; // A join: the table its other link points at stands in its place // — when that table is here, and the host's page does not // already list it. let joined = table .column(to) .and_then(|to| to.link_to.as_deref()) .and_then(|id| tables.iter().find(|table| table.id == id)); if let Some(joined) = joined.filter(|joined| !drawn(&joined.id, None)) { found.push(Contributor { table: joined.id.clone(), column: None, label: label.unwrap_or_else(|| joined.plural.clone()), through: Some(Through { table: table.id.clone(), from: column.key.clone(), to: to.to_string(), }), }); } } } // A thought said on a record's page stands on that page (Spec 7): the // thoughts whose `said_record_id` is the record, on EVERY host — the one // list that is found by where something was said, not by a link column. let said_here = tables .iter() .find(|table| table.id == SAID_ON.0) .filter(|thoughts| thoughts.id != host.id && thoughts.column(SAID_ON.1).is_some()); if let Some(thoughts) = said_here { found.push(Contributor { table: thoughts.id.clone(), column: Some(SAID_ON.1.to_string()), label: thoughts.plural.clone(), through: None, }); } // Several columns can point at the same host (a database's logo, // wordmark and icon are all documents). They are one contributing list, // not several indistinguishable lists under the contributor's plural. let mut named_lists = std::collections::BTreeSet::new(); found.retain(|contributor| named_lists.insert((contributor.table.clone(), contributor.label.clone()))); found } /// The table and the column that say which record a thought was said on. pub const SAID_ON: (&str, &str) = ("thought", "said_record_id"); impl TableDefinition { /// `contributes` sits on a link column; what it says is not blank, and a /// `through` names ANOTHER link column of this table holding one record. pub(super) fn validate_contributes( &self, column: &super::Column, ) -> Result<(), super::DefinitionError> { use super::DefinitionError; let Some(contributes) = &column.contributes else { return Ok(()); }; let slot = |field: &str| format!("columns.{}.contributes{field}", column.key); if column.column_type != ColumnType::Link { return Err(DefinitionError::WrongColumnType { slot: slot(""), column: column.key.clone(), found: column.column_type, wanted: "a link", }); } let Contributes::Said(said) = contributes else { return Ok(()); }; if said.label.is_none() && said.through.is_none() { return Err(DefinitionError::Requires { slot: slot(""), needs: "a_label_or_a_through", }); } if said.label.as_deref().is_some_and(|label| label.trim().is_empty()) { return Err(DefinitionError::EmptyAt(slot(".label"))); } if let Some(through) = &said.through { let other = self.named(&slot(".through"), through, Some((&[ColumnType::Link], "a link")))?; if other.key == column.key || other.link_to.is_none() || other.multiple == Some(true) { return Err(DefinitionError::Requires { slot: slot(".through"), needs: "another_link_column_of_this_table_to", }); } } Ok(()) } } #[cfg(test)] #[path = "contributors_tests.rs"] mod tests;