style: apply cargo fmt
This commit is contained in:
parent
534ed83419
commit
d205d722aa
17 changed files with 390 additions and 315 deletions
|
|
@ -12,23 +12,20 @@ use sqlx_core::error::BoxDynError;
|
|||
use sqlx_core::types::Type;
|
||||
use sqlx_sqlite::{Sqlite, SqliteArgumentValue};
|
||||
|
||||
|
||||
#[fully_pub]
|
||||
trait DatabaseLine {
|
||||
fn id(&self) -> String;
|
||||
}
|
||||
|
||||
|
||||
/// Wrapper to mark a model field as foreign
|
||||
/// You can use a generic argument inside ForeignRef to point to the target model
|
||||
#[derive(Clone, Debug)]
|
||||
#[fully_pub]
|
||||
struct ForeignRef<T: Sized + DatabaseLine> {
|
||||
pub target_type: PhantomData<T>,
|
||||
pub target_id: String
|
||||
pub target_id: String,
|
||||
}
|
||||
|
||||
|
||||
// Implement serde Serialize for ForeignRef
|
||||
impl<T: Sized + DatabaseLine> Serialize for ForeignRef<T> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
|
|
@ -40,22 +37,20 @@ impl<T: Sized + DatabaseLine> Serialize for ForeignRef<T> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T: Sized + DatabaseLine> ForeignRef<T> {
|
||||
pub fn new(entity: &T) -> ForeignRef<T> {
|
||||
pub fn new(entity: &T) -> ForeignRef<T> {
|
||||
ForeignRef {
|
||||
target_type: PhantomData,
|
||||
target_id: entity.id()
|
||||
target_id: entity.id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'r, DB: Database, T: Sized + DatabaseLine> Decode<'r, DB> for ForeignRef<T>
|
||||
where
|
||||
// we want to delegate some of the work to string decoding so let's make sure strings
|
||||
// are supported by the database
|
||||
&'r str: Decode<'r, DB>
|
||||
&'r str: Decode<'r, DB>,
|
||||
{
|
||||
fn decode(
|
||||
value: <DB as Database>::ValueRef<'r>,
|
||||
|
|
@ -66,7 +61,7 @@ where
|
|||
|
||||
Ok(ForeignRef::<T> {
|
||||
target_type: PhantomData,
|
||||
target_id: ref_val
|
||||
target_id: ref_val,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -84,9 +79,11 @@ impl<T: DatabaseLine + Sized> Type<Sqlite> for ForeignRef<T> {
|
|||
}
|
||||
|
||||
impl<T: DatabaseLine + Sized> Encode<'_, Sqlite> for ForeignRef<T> {
|
||||
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
|
||||
fn encode_by_ref(
|
||||
&self,
|
||||
args: &mut Vec<SqliteArgumentValue<'_>>,
|
||||
) -> Result<IsNull, BoxDynError> {
|
||||
args.push(SqliteArgumentValue::Text(self.target_id.clone().into()));
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue