Skip to main content

toolu_orm_core/row/
traits.rs

1//! Feature-gated FromRow trait definitions for each database backend.
2
3#[cfg(any(feature = "postgres", feature = "libsql", feature = "rusqlite"))]
4use crate::error::DbCoreError;
5
6/// Trait for converting a database row into a Rust struct.
7/// Implemented via `#[derive(FromRow)]` in orm-macros or manually.
8///
9/// When Cargo unifies **multiple** backend features on `toolu-orm-core`, this
10/// trait exposes one method per active backend (`from_pg_row`, `from_libsql_row`,
11/// `from_rusqlite_row`). With exactly one backend enabled, a single
12/// `from_row` method is used for that backend's row type.
13#[cfg(all(
14  feature = "postgres",
15  not(feature = "libsql"),
16  not(feature = "rusqlite"),
17))]
18pub trait FromRow: Sized {
19  const REQUIRED_COLUMNS: &'static [&'static str];
20
21  /// # Errors
22  ///
23  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
24  fn from_row(row: &tokio_postgres::Row) -> Result<Self, DbCoreError>;
25}
26
27#[cfg(all(
28  feature = "libsql",
29  not(feature = "postgres"),
30  not(feature = "rusqlite"),
31))]
32pub trait FromRow: Sized {
33  const REQUIRED_COLUMNS: &'static [&'static str];
34
35  /// # Errors
36  ///
37  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
38  fn from_row(row: &libsql::Row) -> Result<Self, DbCoreError>;
39}
40
41#[cfg(all(
42  feature = "rusqlite",
43  not(feature = "postgres"),
44  not(feature = "libsql"),
45))]
46pub trait FromRow: Sized {
47  const REQUIRED_COLUMNS: &'static [&'static str];
48
49  /// # Errors
50  ///
51  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
52  fn from_row(row: &rusqlite::Row<'_>) -> Result<Self, DbCoreError>;
53}
54
55#[cfg(all(feature = "postgres", feature = "libsql", not(feature = "rusqlite"),))]
56pub trait FromRow: Sized {
57  const REQUIRED_COLUMNS: &'static [&'static str];
58
59  /// # Errors
60  ///
61  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
62  fn from_pg_row(row: &tokio_postgres::Row) -> Result<Self, DbCoreError>;
63
64  /// # Errors
65  ///
66  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
67  fn from_libsql_row(row: &libsql::Row) -> Result<Self, DbCoreError>;
68}
69
70#[cfg(all(feature = "postgres", feature = "rusqlite", not(feature = "libsql"),))]
71pub trait FromRow: Sized {
72  const REQUIRED_COLUMNS: &'static [&'static str];
73
74  /// # Errors
75  ///
76  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
77  fn from_pg_row(row: &tokio_postgres::Row) -> Result<Self, DbCoreError>;
78
79  /// # Errors
80  ///
81  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
82  fn from_rusqlite_row(row: &rusqlite::Row<'_>) -> Result<Self, DbCoreError>;
83}
84
85#[cfg(all(feature = "libsql", feature = "rusqlite", not(feature = "postgres"),))]
86pub trait FromRow: Sized {
87  const REQUIRED_COLUMNS: &'static [&'static str];
88
89  /// # Errors
90  ///
91  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
92  fn from_libsql_row(row: &libsql::Row) -> Result<Self, DbCoreError>;
93
94  /// # Errors
95  ///
96  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
97  fn from_rusqlite_row(row: &rusqlite::Row<'_>) -> Result<Self, DbCoreError>;
98}
99
100#[cfg(all(feature = "postgres", feature = "libsql", feature = "rusqlite",))]
101pub trait FromRow: Sized {
102  const REQUIRED_COLUMNS: &'static [&'static str];
103
104  /// # Errors
105  ///
106  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
107  fn from_pg_row(row: &tokio_postgres::Row) -> Result<Self, DbCoreError>;
108
109  /// # Errors
110  ///
111  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
112  fn from_libsql_row(row: &libsql::Row) -> Result<Self, DbCoreError>;
113
114  /// # Errors
115  ///
116  /// Returns [`DbCoreError::RowMapping`] if a column cannot be read or converted.
117  fn from_rusqlite_row(row: &rusqlite::Row<'_>) -> Result<Self, DbCoreError>;
118}
119
120/// Fallback when no row backend feature is enabled on this crate build.
121#[cfg(not(any(
122  all(
123    feature = "postgres",
124    not(feature = "libsql"),
125    not(feature = "rusqlite")
126  ),
127  all(
128    feature = "libsql",
129    not(feature = "postgres"),
130    not(feature = "rusqlite")
131  ),
132  all(
133    feature = "rusqlite",
134    not(feature = "postgres"),
135    not(feature = "libsql")
136  ),
137  all(feature = "postgres", feature = "libsql", not(feature = "rusqlite")),
138  all(feature = "postgres", feature = "rusqlite", not(feature = "libsql")),
139  all(feature = "libsql", feature = "rusqlite", not(feature = "postgres")),
140  all(feature = "postgres", feature = "libsql", feature = "rusqlite"),
141)))]
142pub trait FromRow: Sized {
143  const REQUIRED_COLUMNS: &'static [&'static str];
144}