use ciborium::Value as CborValue;
use vantage_core::{VantageError, error};
macro_rules! sql_declared_types {
($then:ident ! $args:tt) => {
$then! { $args
yaml {
["int", "integer", "i64", "i32", "bytes"] => i64 as "int",
["float", "double", "f64", "f32"] => f64 as "float",
["bool", "boolean"] => bool as "bool",
["decimal", "numeric"] => rust_decimal::Decimal as "decimal",
["date"] => chrono::NaiveDate as "date",
["time"] => chrono::NaiveTime as "time",
["datetime"] => chrono::NaiveDateTime as "datetime",
["timestamp"] => chrono::DateTime<chrono::Utc> as "timestamp",
}
rust {
Vec<u8> as "binary",
i32 as "int", i16 as "int", i8 as "int",
u32 as "int", u16 as "int", u8 as "int",
f32 as "float",
}
}
};
}
pub(crate) use sql_declared_types;
pub(crate) const UNSUPPORTED: &[&str] = &["duration"];
#[cfg(feature = "vista")]
macro_rules! column_for_type_fn {
(() yaml { $( [$($alias:literal),+] => $ty:ty as $name:literal ),* $(,)? }
rust { $( $rty:ty as $rname:literal ),* $(,)? }) => {
pub(crate) fn column_for_type<A: ColumnType>(name: &str, ty: &str) -> Result<Column<A>> {
let col = match ty {
"string" | "text" | "str" => Column::from_column(Column::<String>::new(name)),
$( $($alias)|+ => Column::from_column(Column::<$ty>::new(name)), )*
other if $crate::declared::UNSUPPORTED.contains(&other) => {
return Err(error!(
"YAML column type not supported by SQL drivers",
column = name,
ty = other.to_string()
));
}
other => {
return Err(error!(
"Unknown YAML column type",
column = name,
ty = other.to_string()
));
}
};
Ok(col)
}
};
}
#[cfg(feature = "vista")]
pub(crate) use column_for_type_fn;
macro_rules! sql_coerce_fn {
(($db:ty, $trait:ident, $any:ident)
yaml { $( [$($alias:literal),+] => $ty:ty as $name:literal ),* $(,)? }
rust { $( $rty:ty as $rname:literal ),* $(,)? }) => {
pub(crate) fn coerce_column<E: vantage_types::Entity<$any>>(
table: &vantage_table::table::Table<$db, E>,
column: &str,
value: $any,
) -> vantage_core::Result<$any> {
use vantage_table::traits::column_like::ColumnLike as _;
let Some(declared) = table.columns().get(column) else {
return Ok(value);
};
let type_name = $crate::declared::without_option(declared.get_type());
$( if type_name == std::any::type_name::<$ty>() {
return typed::<$ty>(column, $name, value);
} )*
$( if type_name == std::any::type_name::<$rty>() {
return typed::<$rty>(column, $rname, value);
} )*
Ok(value)
}
fn typed<T: $trait>(column: &str, expected: &str, value: $any) -> vantage_core::Result<$any> {
if $crate::declared::is_absent(value.value()) {
return Ok($any::new(None::<T>));
}
match T::coerce_cbor(value.value().clone()) {
Some(decoded) => Ok($any::new(decoded)),
None => Err($crate::declared::invalid(column, expected, &value.to_string())),
}
}
};
}
pub(crate) use sql_coerce_fn;
pub(crate) fn without_option(type_name: &str) -> &str {
type_name
.strip_prefix("core::option::Option<")
.and_then(|t| t.strip_suffix('>'))
.unwrap_or(type_name)
}
pub(crate) fn is_absent(value: &CborValue) -> bool {
match value {
CborValue::Null => true,
CborValue::Text(s) => s.is_empty(),
_ => false,
}
}
pub(crate) fn invalid(column: &str, expected: &str, value: &str) -> VantageError {
error!(
format!("`{column}`: {value} doesn't fit type `{expected}`"),
column = column,
expected = expected,
value = value
)
.mark_invalid_value()
}