use chrono_tz::Tz;
use crate::column_types::models::ColumnType;
use crate::loader::models::TablePlan;
use crate::project::models::{NullCursor, OnCursorOutOfRange};
pub(crate) fn null_rule(table: &TablePlan) -> Option<&'static str> {
let spec = &table.spec;
let cursor = spec.cursor.as_ref().filter(|_| spec.files.is_none())?;
let nullable = cursor.columns().all(|name| {
spec.source_table
.column(name)
.is_some_and(|column| column.nullable)
});
match cursor.null_cursor {
NullCursor::Include => Some("NULL cursors: included"),
NullCursor::Ignore => Some("NULL cursors: skipped"),
NullCursor::Fail if nullable => Some("NULL cursors: fail the load"),
NullCursor::Fail => None,
}
}
pub(crate) fn range_rule(table: &TablePlan) -> Option<&'static str> {
let spec = &table.spec;
let cursor = spec.cursor.as_ref()?;
if spec.python.is_none() || table.logged.is_some() {
return None;
}
Some(match cursor.out_of_range {
OnCursorOutOfRange::Fail => "rows outside a window fail the table",
OnCursorOutOfRange::Skip => "rows outside a window are skipped",
OnCursorOutOfRange::Warn => "rows outside a window load, with a warning",
})
}
pub(crate) fn zone_rule(table: &TablePlan) -> Option<(Tz, String)> {
let spec = &table.spec;
let cursor = spec.cursor.as_ref()?;
let timed = table
.read_columns
.iter()
.find(|read| read.name.eq_ignore_ascii_case(&cursor.column))
.is_none_or(|read| {
matches!(
read.output,
ColumnType::TimestampTz | ColumnType::TimestampNtz | ColumnType::Text
)
});
let python = spec.python.is_some() && table.logged.is_none();
match (timed, spec.timezone) {
(false, _) => None,
(true, Some((zone, at))) => Some((zone, format!("the {at}'s timezone"))),
(true, None) => python.then(|| (Tz::UTC, "the default, as timezone is not set".to_owned())),
}
}