Cotton code
103 lines · 6.3 KB · RustRaw
//! A record refusal's words: each [`RecordError`] said by its line of set//! `refusal` (`records.*`), the database's row first and the kit's for the//! rest; `Display` says the kit's.use super::{Changed, Found, Malformation, RecordError, RevealRefusal, WhereRefusal};impl RecordError {    /// The refusal in `rows`' words, the kit's for a line they have none of.    #[must_use]    pub fn say(&self, rows: &[crate::rules::shell_lines::LineRow]) -> String {        let say = |line: &str, fills: &[(&str, &str)]| {            crate::rules::shell_lines::resolve("refusal", &format!("records.{line}"), rows, fills)                .unwrap_or_else(|| format!("refusal.records.{line}"))        };        let quoted = |text: &str| format!("{text:?}");        match self {            RecordError::NotAnObject => say("not_an_object", &[]),            RecordError::UnknownColumn { plural, column } => say("unknown_column", &[("plural", plural), ("column", &quoted(column))]),            RecordError::Required { column } => say("required", &[("column", &quoted(column))]),            RecordError::Readonly { column } => say("readonly", &[("column", &quoted(column))]),            RecordError::Computed { column } => say("computed", &[("column", &quoted(column))]),            RecordError::WrongType { column, wanted, found } => {                let wanted = say(&format!("wanted.{}", wanted.line()), &[]);                let found = match found {                    Found::Value(found) => found.clone(),                    Found::AnotherType => say("found.another_type", &[]),                };                say("wrong_type", &[("column", &quoted(column)), ("wanted", &wanted), ("found", &found)])            }            RecordError::NotWholeAmount { column, found } => say("not_whole_amount", &[("column", &quoted(column)), ("found", found)]),            RecordError::NotAChoice { column, found, choices } => {                say("not_a_choice", &[("column", &quoted(column)), ("found", &quoted(found)), ("choices", choices)])            }            RecordError::NothingToChange => say("nothing_to_change", &[]),            RecordError::NoOrder { plural } => say("no_order", &[("plural", plural)]),            RecordError::BadReorder => say("bad_reorder", &[]),            RecordError::NotALink { column } => say("not_a_link", &[("column", &quoted(column))]),            RecordError::AlreadyUndone => say("already_undone", &[]),            RecordError::ChangedSince { what } => {                let what = match what {                    Changed::Record { id } => say("changed.record", &[("id", id)]),                    Changed::Order => say("changed.order", &[]),                    Changed::Said(what) => what.clone(),                };                say("changed_since", &[("what", &what)])            }            RecordError::Malformed(why) => {                let why = match why {                    Malformation::NoRecord => say("malformed_why.no_record", &[]),                    Malformation::NotAnObject { side } => say("malformed_why.not_an_object", &[("side", side)]),                };                say("malformed", &[("why", &why)])            }            RecordError::MoveUndoNeedsCoordinator => say("move_undo_needs_coordinator", &[]),            RecordError::RevealRefused(why) => say(                match why {                    RevealRefusal::InvalidCount => "reveal.invalid_count",                    RevealRefusal::AlreadyCopied => "reveal.already_copied",                    RevealRefusal::Incomplete => "reveal.incomplete",                },                &[],            ),            RecordError::BadWhere { column, reason } => {                let (line, fills): (&str, Vec<(&str, &str)>) = match reason {                    WhereRefusal::NotAnObject { found } => ("not_an_object", vec![("found", found)]),                    WhereRefusal::OneRecordId { column, found } => ("one_record_id", vec![("key", column), ("found", found)]),                    WhereRefusal::Null => ("null", vec![]),                    WhereRefusal::OneShape => ("one_shape", vec![]),                    WhereRefusal::ValuesEmpty => ("values_empty", vec![]),                    WhereRefusal::NotEmpty => ("not_empty", vec![]),                    WhereRefusal::MonthRange { found } => ("month_range", vec![("found", found)]),                    WhereRefusal::MonthOnlyDate => ("month_only_date", vec![]),                    WhereRefusal::WithinDaysRange { found } => ("within_days_range", vec![("found", found)]),                    WhereRefusal::WithinDaysOnlyDate => ("within_days_only_date", vec![]),                    WhereRefusal::BoundNumber { found } => ("bound_number", vec![("found", found)]),                    WhereRefusal::BoundOnlyNumber => ("bound_only_number", vec![]),                    WhereRefusal::OnWord { found } => ("on_word", vec![("found", found)]),                    WhereRefusal::OnOnlyDate => ("on_only_date", vec![]),                    WhereRefusal::PresentBool { found } => ("present_bool", vec![("found", found)]),                    WhereRefusal::NotACondition { said } => ("not_a_condition", vec![("said", said)]),                    WhereRefusal::EachOneValue { found } => ("each_one_value", vec![("found", found)]),                    WhereRefusal::AddableToOneAlbum => ("addable_to_one_album", vec![]),                    WhereRefusal::NoNot { plural } => ("no_not", vec![("plural", plural)]),                    WhereRefusal::NoMonth { plural } => ("no_month", vec![("plural", plural)]),                    WhereRefusal::NoWithinDays { plural } => ("no_within_days", vec![("plural", plural)]),                    WhereRefusal::NoBound { plural } => ("no_bound", vec![("plural", plural)]),                    WhereRefusal::NoOn { plural } => ("no_on", vec![("plural", plural)]),                };                let reason = say(&format!("where.{line}"), &fills);                say("bad_where", &[("column", &quoted(column)), ("reason", &reason)])            }            RecordError::CannotNarrow { plural, column } => say("cannot_narrow", &[("plural", plural), ("column", &quoted(column))]),        }    }}impl std::fmt::Display for RecordError {    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {        f.write_str(&self.say(&[]))    }}impl std::error::Error for RecordError {}