-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: add trait ArelAttributeFromRow
- Loading branch information
1 parent
67c17b6
commit a5d2f60
Showing
9 changed files
with
289 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
use sqlx::Row; | ||
|
||
pub trait ArelAttributeFromRow { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>; | ||
} | ||
|
||
impl ArelAttributeFromRow for bool { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for i8 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for i16 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for i32 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for i64 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for u8 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for u16 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for u32 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for u64 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
match row.try_get::<u32, _>(index) { | ||
Ok(v) => Ok(v as u64), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for f32 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for f64 { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for char { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
match row.try_get::<u8, _>(index) { | ||
Ok(v) => Ok(v as char), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for String { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for bytes::Bytes { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
match row.try_get::<Vec<u8>, _>(index) { | ||
Ok(v) => Ok(bytes::Bytes::from(v)), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for serde_json::Value { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for chrono::DateTime<chrono::Utc> { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl ArelAttributeFromRow for chrono::DateTime<chrono::FixedOffset> { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
row.try_get(index) | ||
} | ||
} | ||
|
||
impl<T: ArelAttributeFromRow> ArelAttributeFromRow for Option<T> { | ||
fn from_row<'r, I>(row: &'r crate::DatabaseRow, index: I) -> sqlx::Result<Self, sqlx::Error> | ||
where | ||
Self: Sized, | ||
I: sqlx::ColumnIndex<crate::DatabaseRow>, | ||
{ | ||
match <T as ArelAttributeFromRow>::from_row(row, index) { | ||
Ok(v) => Ok(Some(v)), | ||
Err(_) => Ok(None), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub trait ArelPersisted { | ||
fn set_persisted(&mut self, persisted: bool); | ||
fn persited(&self) -> bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters