#[derive(Debug)]
pub struct RowError(String);
impl RowError {
pub fn new(msg: impl Into<String>) -> Self {
Self(msg.into())
}
}
impl std::fmt::Display for RowError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for RowError {}
pub trait RangoRow {
fn get_bool(&self, col: &str) -> Result<bool, RowError>;
fn get_i16(&self, col: &str) -> Result<i16, RowError>;
fn get_i32(&self, col: &str) -> Result<i32, RowError>;
fn get_i64(&self, col: &str) -> Result<i64, RowError>;
fn get_f32(&self, col: &str) -> Result<f32, RowError>;
fn get_f64(&self, col: &str) -> Result<f64, RowError>;
fn get_decimal(&self, col: &str) -> Result<rust_decimal::Decimal, RowError>;
fn get_string(&self, col: &str) -> Result<String, RowError>;
fn get_bytes(&self, col: &str) -> Result<Vec<u8>, RowError>;
fn get_uuid(&self, col: &str) -> Result<uuid::Uuid, RowError>;
fn get_datetime(&self, col: &str) -> Result<chrono::DateTime<chrono::Utc>, RowError>;
fn get_date(&self, col: &str) -> Result<chrono::NaiveDate, RowError>;
fn get_time(&self, col: &str) -> Result<chrono::NaiveTime, RowError>;
fn get_json(&self, col: &str) -> Result<serde_json::Value, RowError>;
fn is_null(&self, col: &str) -> bool;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be read from a database row",
note = "add `#[derive(Model)]` (or `#[rango::model]`, or `#[derive(Partial)]` for a column subset) to `{Self}`"
)]
pub trait FromRow: Sized {
fn from_row(row: &dyn RangoRow) -> Result<Self, RowError>;
}
pub trait Partial: FromRow {
type Model: crate::Model;
fn columns() -> &'static [&'static str];
}