Skip to content

Commit

Permalink
Removed AsDbType trait
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 12, 2024
1 parent a429500 commit bbba768
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 103 deletions.
10 changes: 5 additions & 5 deletions src/fields/types/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use rorm_db::sql::value::NullType;

use crate::conditions::Value;
use crate::{impl_AsDbType, impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd};
use crate::{impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd, impl_FieldType};

impl_AsDbType!(NaiveTime, ChronoNaiveTime, Value::ChronoNaiveTime);
impl_FieldType!(NaiveTime, ChronoNaiveTime, Value::ChronoNaiveTime);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, NaiveTime> for NaiveTime { Value::ChronoNaiveTime });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<NaiveTime>> for Option<NaiveTime> { |option: Self| option.map(Value::ChronoNaiveTime).unwrap_or(Value::Null(NullType::ChronoNaiveTime)) });
impl_FieldOrd!(NaiveTime, NaiveTime, Value::ChronoNaiveTime);
Expand All @@ -13,7 +13,7 @@ impl_FieldOrd!(Option<NaiveTime>, Option<NaiveTime>, |option: Self| option
.unwrap_or(Value::Null(NullType::ChronoNaiveTime)));
impl_FieldMin_FieldMax!(NaiveTime);

impl_AsDbType!(NaiveDate, ChronoNaiveDate, Value::ChronoNaiveDate);
impl_FieldType!(NaiveDate, ChronoNaiveDate, Value::ChronoNaiveDate);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, NaiveDate> for NaiveDate { Value::ChronoNaiveDate });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<NaiveDate>> for Option<NaiveDate> { |option: Self| option.map(Value::ChronoNaiveDate).unwrap_or(Value::Null(NullType::ChronoNaiveDate)) });
impl_FieldOrd!(NaiveDate, NaiveDate, Value::ChronoNaiveDate);
Expand All @@ -22,7 +22,7 @@ impl_FieldOrd!(Option<NaiveDate>, Option<NaiveDate>, |option: Self| option
.unwrap_or(Value::Null(NullType::ChronoNaiveDate)));
impl_FieldMin_FieldMax!(NaiveDate);

impl_AsDbType!(
impl_FieldType!(
NaiveDateTime,
ChronoNaiveDateTime,
Value::ChronoNaiveDateTime
Expand All @@ -39,7 +39,7 @@ impl_FieldOrd!(
);
impl_FieldMin_FieldMax!(NaiveDateTime);

impl_AsDbType!(DateTime<Utc>, ChronoDateTime, Value::ChronoDateTime);
impl_FieldType!(DateTime<Utc>, ChronoDateTime, Value::ChronoDateTime);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, DateTime<Utc>> for DateTime<Utc> { Value::ChronoDateTime });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<DateTime<Utc>>> for Option<DateTime<Utc>> { |option: Self| option.map(Value::ChronoDateTime).unwrap_or(Value::Null(NullType::ChronoDateTime)) });
impl_FieldOrd!(DateTime<Utc>, DateTime<Utc>, Value::ChronoDateTime);
Expand Down
7 changes: 0 additions & 7 deletions src/fields/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::fields::traits::{Array, FieldColumns, FieldType};
use crate::fields::utils::check::shared_linter_check;
use crate::fields::utils::get_annotations::{forward_annotations, set_null_annotations};
use crate::fields::utils::get_names::single_column_name;
use crate::internal::field::as_db_type::AsDbType;
use crate::new_converting_decoder;
use crate::Error::DecodeError;

Expand Down Expand Up @@ -76,9 +75,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Json<T> {

type GetNames = single_column_name;
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Json<T> {
type Primitive = Vec<u8>;
}

new_converting_decoder!(
pub OptionJsonDecoder<T: Serialize + DeserializeOwned>,
Expand Down Expand Up @@ -116,9 +112,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Option<Json<T>> {

type GetNames = single_column_name;
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Option<Json<T>> {
type Primitive = Option<Vec<u8>>;
}

// From
impl<T: Serialize + DeserializeOwned> From<T> for Json<T> {
Expand Down
15 changes: 0 additions & 15 deletions src/fields/types/max_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::fields::utils::const_fn::Contains;
use crate::fields::utils::get_annotations::merge_annotations;
use crate::fields::utils::get_names::single_column_name;
use crate::impl_FieldEq;
use crate::internal::field::as_db_type::AsDbType;
use crate::internal::field::decoder::FieldDecoder;
use crate::internal::field::{Field, FieldProxy};
use crate::internal::hmr::annotations::{Annotations, MaxLength};
Expand Down Expand Up @@ -154,20 +153,6 @@ where
}
}

impl<const MAX_LEN: usize, Impl> AsDbType for MaxStr<MAX_LEN, Impl, String>
where
Impl: LenImpl + Default + 'static,
{
type Primitive = String;
}

impl<const MAX_LEN: usize, Impl> AsDbType for Option<MaxStr<MAX_LEN, Impl, String>>
where
Impl: LenImpl + Default + 'static,
{
type Primitive = String;
}

impl<const MAX_LEN: usize, Impl> FieldType for MaxStr<MAX_LEN, Impl, String>
where
Impl: LenImpl + Default + 'static,
Expand Down
7 changes: 0 additions & 7 deletions src/fields/types/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::fields::traits::{Array, FieldColumns, FieldType};
use crate::fields::utils::check::shared_linter_check;
use crate::fields::utils::get_annotations::{forward_annotations, set_null_annotations};
use crate::fields::utils::get_names::single_column_name;
use crate::internal::field::as_db_type::AsDbType;
use crate::new_converting_decoder;

/// Stores data by serializing it to message pack.
Expand Down Expand Up @@ -74,9 +73,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for MsgPack<T> {
type Check = shared_linter_check<1>;
type GetNames = single_column_name;
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for MsgPack<T> {
type Primitive = Vec<u8>;
}

new_converting_decoder!(
pub OptionMsgPackDecoder<T: Serialize + DeserializeOwned>,
Expand Down Expand Up @@ -112,9 +108,6 @@ impl<T: Serialize + DeserializeOwned + 'static> FieldType for Option<MsgPack<T>>
type Check = shared_linter_check<1>;
type GetNames = single_column_name;
}
impl<T: Serialize + DeserializeOwned + 'static> AsDbType for Option<MsgPack<T>> {
type Primitive = Option<Vec<u8>>;
}

// From
impl<T: Serialize + DeserializeOwned> From<T> for MsgPack<T> {
Expand Down
8 changes: 4 additions & 4 deletions src/fields/types/postgres_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use ipnetwork::IpNetwork;
use mac_address::MacAddress;

use crate::conditions::Value;
use crate::{impl_AsDbType, impl_FieldEq};
use crate::{impl_FieldEq, impl_FieldType};

impl_AsDbType!(MacAddress, MacAddress, Value::MacAddress);
impl_FieldType!(MacAddress, MacAddress, Value::MacAddress);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, MacAddress> for MacAddress { Value::MacAddress });

impl_AsDbType!(IpNetwork, IpNetwork, Value::IpNetwork);
impl_FieldType!(IpNetwork, IpNetwork, Value::IpNetwork);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, IpNetwork> for IpNetwork { Value::IpNetwork });

impl_AsDbType!(
impl_FieldType!(
BitVec,
BitVec,
|vec| Value::BitVec(BitCow::Owned(vec)),
Expand Down
18 changes: 9 additions & 9 deletions src/fields/types/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::borrow::Cow;
use crate::conditions::Value;
use crate::db::sql::value::NullType;
use crate::{
impl_AsDbType, impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd, impl_FieldSum_FieldAvg,
impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd, impl_FieldSum_FieldAvg, impl_FieldType,
};

impl_AsDbType!(bool, Bool, Value::Bool);
impl_FieldType!(bool, Bool, Value::Bool);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, bool> for bool { Value::Bool });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<bool>> for Option<bool> { |option: Self| option.map(Value::Bool).unwrap_or(Value::Null(NullType::Bool)) });

impl_AsDbType!(i16, I16, Value::I16);
impl_FieldType!(i16, I16, Value::I16);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, i16> for i16 { Value::I16 });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<i16>> for Option<i16> { |option: Self| option.map(Value::I16).unwrap_or(Value::Null(NullType::I16)) });
impl_FieldOrd!(i16, i16, Value::I16);
Expand All @@ -20,7 +20,7 @@ impl_FieldOrd!(Option<i16>, Option<i16>, |option: Self| option
impl_FieldSum_FieldAvg!(i16, sum_result: i64);
impl_FieldMin_FieldMax!(i16);

impl_AsDbType!(i32, I32, Value::I32);
impl_FieldType!(i32, I32, Value::I32);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, i32> for i32 { Value::I32 });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<i32>> for Option<i32> { |option: Self| option.map(Value::I32).unwrap_or(Value::Null(NullType::I32)) });
impl_FieldOrd!(i32, i32, Value::I32);
Expand All @@ -30,7 +30,7 @@ impl_FieldOrd!(Option<i32>, Option<i32>, |option: Self| option
impl_FieldSum_FieldAvg!(i32, sum_result: i64);
impl_FieldMin_FieldMax!(i32);

impl_AsDbType!(i64, I64, Value::I64);
impl_FieldType!(i64, I64, Value::I64);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, i64> for i64 { Value::I64 });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<i64>> for Option<i64> { |option: Self| option.map(Value::I64).unwrap_or(Value::Null(NullType::I64)) });
impl_FieldOrd!(i64, i64, Value::I64);
Expand All @@ -40,7 +40,7 @@ impl_FieldOrd!(Option<i64>, Option<i64>, |option: Self| option
impl_FieldSum_FieldAvg!(i64, sum_result: f64);
impl_FieldMin_FieldMax!(i64);

impl_AsDbType!(f32, F32, Value::F32);
impl_FieldType!(f32, F32, Value::F32);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, f32> for f32 { Value::F32 });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<f32>> for Option<f32> { |option: Self| option.map(Value::F32).unwrap_or(Value::Null(NullType::F32)) });
impl_FieldOrd!(f32, f32, Value::F32);
Expand All @@ -50,7 +50,7 @@ impl_FieldOrd!(Option<f32>, Option<f32>, |option: Self| option
impl_FieldSum_FieldAvg!(f32, sum_result: f32);
impl_FieldMin_FieldMax!(f32);

impl_AsDbType!(f64, F64, Value::F64);
impl_FieldType!(f64, F64, Value::F64);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, f64> for f64 { Value::F64 });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<f64>> for Option<f64> { |option: Self| option.map(Value::F64).unwrap_or(Value::Null(NullType::F64)) });
impl_FieldOrd!(f64, f64, Value::F64);
Expand All @@ -60,7 +60,7 @@ impl_FieldOrd!(Option<f64>, Option<f64>, |option: Self| option
impl_FieldSum_FieldAvg!(f64, sum_result: f64);
impl_FieldMin_FieldMax!(f64);

impl_AsDbType!(String, String, conv_string, conv_string);
impl_FieldType!(String, String, conv_string, conv_string);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs str> for String { conv_string });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs String> for String { conv_string });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, String> for String { conv_string });
Expand All @@ -78,7 +78,7 @@ fn conv_string<'a>(value: impl Into<Cow<'a, str>>) -> Value<'a> {
Value::String(value.into())
}

impl_AsDbType!(Vec<u8>, Binary, conv_bytes, conv_bytes);
impl_FieldType!(Vec<u8>, Binary, conv_bytes, conv_bytes);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs [u8]> for Vec<u8> { conv_bytes });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs Vec<u8>> for Vec<u8> { conv_bytes });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Vec<u8>> for Vec<u8> { conv_bytes });
Expand Down
10 changes: 5 additions & 5 deletions src/fields/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use rorm_db::sql::value::NullType;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};

use crate::conditions::Value;
use crate::{impl_AsDbType, impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd};
use crate::{impl_FieldEq, impl_FieldMin_FieldMax, impl_FieldOrd, impl_FieldType};

impl_AsDbType!(Time, TimeTime, Value::TimeTime);
impl_FieldType!(Time, TimeTime, Value::TimeTime);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Time> for Time { Value::TimeTime });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<Time>> for Option<Time> { |option: Self| option.map(Value::TimeTime).unwrap_or(Value::Null(NullType::TimeTime)) });
impl_FieldOrd!(Time, Time, Value::TimeTime);
Expand All @@ -13,7 +13,7 @@ impl_FieldOrd!(Option<Time>, Option<Time>, |option: Self| option
.unwrap_or(Value::Null(NullType::TimeTime)));
impl_FieldMin_FieldMax!(Time);

impl_AsDbType!(Date, TimeDate, Value::TimeDate);
impl_FieldType!(Date, TimeDate, Value::TimeDate);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Date> for Date { Value::TimeDate });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<Date>> for Option<Date> { |option: Self| option.map(Value::TimeDate).unwrap_or(Value::Null(NullType::TimeDate)) });
impl_FieldOrd!(Date, Date, Value::TimeDate);
Expand All @@ -22,7 +22,7 @@ impl_FieldOrd!(Option<Date>, Option<Date>, |option: Self| option
.unwrap_or(Value::Null(NullType::TimeDate)));
impl_FieldMin_FieldMax!(Date);

impl_AsDbType!(
impl_FieldType!(
OffsetDateTime,
TimeOffsetDateTime,
Value::TimeOffsetDateTime
Expand All @@ -39,7 +39,7 @@ impl_FieldOrd!(
);
impl_FieldMin_FieldMax!(OffsetDateTime);

impl_AsDbType!(
impl_FieldType!(
PrimitiveDateTime,
TimePrimitiveDateTime,
Value::TimePrimitiveDateTime
Expand Down
7 changes: 0 additions & 7 deletions src/fields/types/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::fields::traits::{Array, FieldColumns, FieldType};
use crate::fields::utils::check::string_check;
use crate::fields::utils::get_annotations::{forward_annotations, set_null_annotations};
use crate::fields::utils::get_names::single_column_name;
use crate::internal::field::as_db_type::AsDbType;
use crate::{impl_FieldEq, new_converting_decoder, Error};

impl_FieldEq!(impl<'rhs> FieldEq<'rhs, &'rhs Url> for Url {|url: &'rhs Url| Value::String(Cow::Borrowed(url.as_str()))});
Expand Down Expand Up @@ -36,9 +35,6 @@ impl FieldType for Url {

type GetNames = single_column_name;
}
impl AsDbType for Url {
type Primitive = String;
}
new_converting_decoder!(
pub UrlDecoder,
|value: String| -> Url {
Expand Down Expand Up @@ -70,9 +66,6 @@ impl FieldType for Option<Url> {

type GetNames = single_column_name;
}
impl AsDbType for Option<Url> {
type Primitive = Option<<Url as AsDbType>::Primitive>;
}
new_converting_decoder!(
pub OptionUrlDecoder,
|value: Option<String>| -> Option<Url> {
Expand Down
4 changes: 2 additions & 2 deletions src/fields/types/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use rorm_db::sql::value::NullType;
use uuid::Uuid;

use crate::conditions::Value;
use crate::{impl_AsDbType, impl_FieldEq};
use crate::{impl_FieldEq, impl_FieldType};

impl_AsDbType!(Uuid, Uuid, Value::Uuid);
impl_FieldType!(Uuid, Uuid, Value::Uuid);
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Uuid> for Uuid { Value::Uuid });
impl_FieldEq!(impl<'rhs> FieldEq<'rhs, Option<Uuid>> for Option<Uuid> { |option: Option<_>| option.map(Value::Uuid).unwrap_or(Value::Null(NullType::Uuid)) });
35 changes: 6 additions & 29 deletions src/internal/field/as_db_type.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
//! defines and implements the [`AsDbType`] trait.

use rorm_db::row::DecodeOwned;

use crate::internal::field::FieldType;

/// This trait maps rust types to database types
///
/// I.e. it specifies which datatypes are allowed on model's fields.
pub trait AsDbType: FieldType + Sized {
/// A type which can be retrieved from the db and then converted into Self.
type Primitive: DecodeOwned;
}

/// Provides the "default" implementation of [`AsDbType`] and [`FieldType`] of kind `AsDbType`.
/// Provides the "default" implementation of [`FieldType`].
///
/// ## Usages
/// - `impl_as_db_type!(RustType, NullType, into_value, as_value);`
/// - `impl_FieldType!(RustType, NullType, into_value, as_value);`
/// - `RustType` is the type to implement the traits on.
/// - `NullType` is the database type to associate with (variant of [`NullType`](crate::db::sql::value::NullType)).
/// - `into_value` is used to convert `RustType` into a [`Value<'static>`] (must implement `Fn(RustType) -> Value<'static>`).
Expand All @@ -24,7 +10,7 @@ pub trait AsDbType: FieldType + Sized {
#[doc(hidden)]
#[allow(non_snake_case)] // makes it clearer that a trait and which trait is meant
#[macro_export]
macro_rules! impl_AsDbType {
macro_rules! impl_FieldType {
(Option<$type:ty>, $decoder:ty) => {
impl $crate::fields::traits::FieldType for Option<$type> {
type Columns = $crate::fields::traits::Array<1>;
Expand Down Expand Up @@ -58,17 +44,12 @@ macro_rules! impl_AsDbType {

type GetNames = $crate::fields::utils::get_names::single_column_name;
}

impl $crate::internal::field::as_db_type::AsDbType for Option<$type> {
type Primitive =
Option<<$type as $crate::internal::field::as_db_type::AsDbType>::Primitive>;
}
};
($type:ty, $null_type:ident, $into_value:expr) => {
impl_AsDbType!($type, $null_type, $into_value, |&value| $into_value(value));
impl_FieldType!($type, $null_type, $into_value, |&value| $into_value(value));
};
($type:ty, $null_type:ident, $into_value:expr, $as_value:expr) => {
impl_AsDbType!(
impl_FieldType!(
$type,
$null_type,
$into_value,
Expand Down Expand Up @@ -110,10 +91,6 @@ macro_rules! impl_AsDbType {
type GetNames = $crate::fields::utils::get_names::single_column_name;
}

impl $crate::internal::field::as_db_type::AsDbType for $type {
type Primitive = Self;
}

impl_AsDbType!(Option<$type>, $crate::crud::decoder::DirectDecoder<Self>);
impl_FieldType!(Option<$type>, $crate::crud::decoder::DirectDecoder<Self>);
};
}
Loading

0 comments on commit bbba768

Please sign in to comment.