use crate::number_text::main::thousands::thousands;
use crate::project::models::{CheckName, TableChecks};
use crate::schema_plan::models::DesiredColumn;
pub(crate) fn check_key(check: CheckName) -> &'static str {
match check {
CheckName::Unique => "unique",
CheckName::NotNull => "not_null",
CheckName::MinRows => "min_rows",
CheckName::MaxRowDrop => "max_row_drop",
}
}
pub(crate) fn set_checks(checks: &TableChecks) -> Vec<CheckName> {
[
(CheckName::Unique, checks.unique.is_some()),
(CheckName::NotNull, !checks.not_null.is_empty()),
(CheckName::MinRows, checks.min_rows.is_some()),
(CheckName::MaxRowDrop, checks.max_row_drop.is_some()),
]
.into_iter()
.filter_map(|(check, set)| set.then_some(check))
.collect()
}
pub(crate) fn resolved(checks: &TableChecks, columns: &[DesiredColumn]) -> TableChecks {
TableChecks {
unique: checks.unique.as_ref().map(|key| loaded_names(columns, key)),
not_null: loaded_names(columns, &checks.not_null),
..checks.clone()
}
}
fn loaded_names(columns: &[DesiredColumn], names: &[String]) -> Vec<String> {
names
.iter()
.map(|name| loaded_name(columns, name))
.collect()
}
fn loaded_name(columns: &[DesiredColumn], name: &str) -> String {
columns
.iter()
.find(|column| column.name.eq_ignore_ascii_case(name))
.map_or_else(|| name.to_owned(), |column| column.name.clone())
}
pub(crate) fn not_published(committed: u64) -> String {
match committed {
0 => "nothing was published".to_owned(),
1 => "this commit's rows were not published; the 1 row earlier commits published stays"
.to_owned(),
rows => format!(
"this commit's rows were not published; the {} earlier commits published stay",
counted_rows(rows)
),
}
}
pub(crate) fn counted_rows(count: u64) -> String {
match count {
1 => "1 row".to_owned(),
_ => format!("{} rows", thousands(count)),
}
}