Skip to main content

rango_orm_core/
row.rs

1/// Error when reading a row.
2#[derive(Debug)]
3pub struct RowError(String);
4
5impl RowError {
6    pub fn new(msg: impl Into<String>) -> Self {
7        Self(msg.into())
8    }
9}
10
11impl std::fmt::Display for RowError {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        write!(f, "{}", self.0)
14    }
15}
16impl std::error::Error for RowError {}
17
18/// Backend-agnostic row abstraction.
19/// Implemented by rango-orm-postgres (and future backends) for their native row type.
20pub trait RangoRow {
21    fn get_bool(&self, col: &str) -> Result<bool, RowError>;
22    fn get_i16(&self, col: &str) -> Result<i16, RowError>;
23    fn get_i32(&self, col: &str) -> Result<i32, RowError>;
24    fn get_i64(&self, col: &str) -> Result<i64, RowError>;
25    fn get_f32(&self, col: &str) -> Result<f32, RowError>;
26    fn get_f64(&self, col: &str) -> Result<f64, RowError>;
27    fn get_decimal(&self, col: &str) -> Result<rust_decimal::Decimal, RowError>;
28    fn get_string(&self, col: &str) -> Result<String, RowError>;
29    fn get_bytes(&self, col: &str) -> Result<Vec<u8>, RowError>;
30    fn get_uuid(&self, col: &str) -> Result<uuid::Uuid, RowError>;
31    fn get_datetime(&self, col: &str) -> Result<chrono::DateTime<chrono::Utc>, RowError>;
32    fn get_date(&self, col: &str) -> Result<chrono::NaiveDate, RowError>;
33    fn get_time(&self, col: &str) -> Result<chrono::NaiveTime, RowError>;
34    fn get_json(&self, col: &str) -> Result<serde_json::Value, RowError>;
35    fn is_null(&self, col: &str) -> bool;
36}
37
38/// Implemented by `#[derive(Model)]` — reads a model from a RangoRow.
39#[diagnostic::on_unimplemented(
40    message = "`{Self}` cannot be read from a database row",
41    note = "add `#[derive(Model)]` (or `#[rango::model]`, or `#[derive(Partial)]` for a column subset) to `{Self}`"
42)]
43pub trait FromRow: Sized {
44    fn from_row(row: &dyn RangoRow) -> Result<Self, RowError>;
45}
46
47/// Implemented by `#[derive(Partial)]` — a subset of a model's columns, read with
48/// `QueryBuilder::select_as::<P>()`.
49pub trait Partial: FromRow {
50    type Model: crate::Model;
51    /// Column names to select, taken from the model's typed column constants.
52    fn columns() -> &'static [&'static str];
53}