Created
May 17, 2018 05:57
-
-
Save robotarmy/efa1f322852ffc47e8ff245ec769f1a9 to your computer and use it in GitHub Desktop.
attempting to create a new type (not an alias) so that the compiler will check the arguments that i'm passing in for two different fields that are implemented in the database as the same type.
This file contains hidden or 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
extern crate uuid; | |
extern crate chrono; | |
extern crate bigdecimal; | |
extern crate num_bigint; | |
extern crate num_integer; | |
extern crate num_traits; | |
use schema::ledger_entries; | |
use self::uuid::Uuid; | |
use self::bigdecimal::BigDecimal; | |
use self::chrono::{Utc, DateTime}; | |
// Todo: instead of making an alias | |
// make a newtype (trait?) so that | |
// the compiler doesn't allow | |
// a DebitAmount to be substituted | |
// for a CreditAmount | |
// | |
use diesel::sql_types::Numeric; | |
#[derive(Debug, FromSqlRow, AsExpression)] | |
#[sql_type = "Numeric"] | |
pub struct DebitAmount(BigDecimal); | |
pub type CreditAmount = BigDecimal; | |
use std::io::prelude::Write; | |
use diesel::serialize::{self, Output, ToSql}; | |
impl ToSql<Numeric, Pg> for DebitAmount { | |
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result { | |
} | |
} | |
use diesel::deserialize::{self, FromSql}; | |
impl FromSql<Numeric, Pg> for DebitAmount { | |
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | |
} | |
} | |
#[derive(Queryable)] | |
pub struct LedgerEntry { | |
pub id: Uuid, | |
pub title: String, | |
pub debit: Option<DebitAmount>, | |
pub credit: Option<CreditAmount>, | |
// DateTimes in UTC Timezone | |
pub created_at: DateTime<Utc>, | |
pub updated_at: Option<DateTime<Utc>> | |
} | |
#[derive(Insertable)] | |
#[table_name = "ledger_entries"] | |
pub struct NewLedgerEntry<'a> { | |
pub title: &'a String, | |
pub debit: &'a DebitAmount, | |
pub credit: &'a CreditAmount | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment