Diesel CLI Extension

Diesel CLI Extension is a tool-belt that aids Diesel CLI after it built schema.rs .

It contains 4 functions at this moment. 1. Generate protobuf file.(diesel_ext proto) 2. Generate model rust structs.(diesel_ext model) 3. Generate conversion implementations.(diesel_ext into_proto, and diesel_ext from_proto)

How to use

First of all, diesel print-schema > src/schema.rs

To generate model structs:

diesel_ext > src/models.rs, diesel_ext model > src/models.rs (it is the default option)

Sample output: ``` rust use chrono::NaiveDateTime; use bigdecimal::BigDecimal;

[derive(Queryable)]

pub struct Account { pub id : i32, pub clientid : i32, pub balancejpy : BigDecimal, pub balancebtc : BigDecimal, pub balanceeth : BigDecimal, pub created_at : NaiveDateTime, }

[derive(Queryable)]

pub struct Client { pub id : i32, pub openedat : NaiveDateTime, pub businessname : String, pub businessaddress : String, pub apiusagecount : i32, pub apiusagesecond : i32, pub commissionfixedcurrency : i16, pub commissionfixedamount : f32, pub commissionfixed_bp : f32, }

[derive(Queryable)]

pub struct Contact { pub clientid : i32, pub name : String, pub position : String, pub phone : String, pub email : String, pub createdat : NaiveDateTime, }

[derive(Queryable)]

pub struct FiatInstitution { pub id : i32, pub clientid : i32, pub name : String, pub code : String, pub routingcode : String, pub accountname : String, pub accountcode : String, pub linkcode : String, pub createdat : NaiveDateTime, }

[derive(Queryable)]

pub struct Order { pub id : i64, pub accountid : i64, pub placedat : NaiveDateTime, pub initialprocessat : NaiveDateTime, pub assetfrom : i16, pub assetto : i16, pub side : i16, pub type_ : i16, pub amount : BigDecimal, pub filled : BigDecimal, pub status : i16, pub closedat : NaiveDateTime, pub commissioncurrency : i16, pub commissionfixed : f32, pub commissionbp : f32, pub tracking_code : String, } ```

To generate prelimitive proto file:

diesel_ext proto > myproto.proto

Sample output: ``` r syntax = "proto3";

message Account { int32 id = 1; int32 clientid = 2; string balancejpy = 3; string balancebtc = 4; string balanceeth = 5; string createdat = 6; } message Client { int32 id = 1; string openedat = 2; string businessname = 3; string businessaddress = 4; int32 apiusagecount = 5; int32 apiusagesecond = 6; int32 commissionfixedcurrency = 7; float commissionfixedamount = 8; float commissionfixedbp = 9; } message Contact { int32 clientid = 1; string name = 2; string position = 3; string phone = 4; string email = 5; string createdat = 6; } message FiatInstitution { int32 id = 1; int32 clientid = 2; string name = 3; string code = 4; string routingcode = 5; string accountname = 6; string accountcode = 7; string linkcode = 8; string createdat = 9; } message Order { int64 id = 1; int64 accountid = 2; string placedat = 3; string initialprocessat = 4; int32 assetfrom = 5; int32 assetto = 6; int32 side = 7; int32 type_ = 8; string amount = 9; string filled = 10; int32 status = 11; string closedat = 12; int32 commissioncurrency = 13; float commissionfixed = 14; float commissionbp = 15; string tracking_code = 16; }

message EnquireAccountRequest { int64 id =1; } message EnquireClientRequest { int64 id =1; } message EnquireContactRequest { int64 id =1; } message EnquireFiatInstitutionRequest { int64 id =1; } message EnquireOrderRequest { int64 id =1; }

service MessageRpc { rpc getAccount (EnquireAccountRequest) returns (Account) { } rpc getClient (EnquireClientRequest) returns (Client) { } rpc getContact (EnquireContactRequest) returns (Contact) { } rpc getFiatInstitution (EnquireFiatInstitutionRequest) returns (FiatInstitution) { } rpc getOrder (EnquireOrderRequest) returns (Order) { } } ```

To generate proto conversions:

diesel_ext from_proto > proto/src/conversion/from_proto.rs, diesel_ext into_proto > proto/src/conversion/into_proto.rs

Sample output:

``` rust use bigdecimal::BigDecimal; use chrono::{DateTime, NaiveDateTime, TimeZone, Utc}; use models; use proto::name; use std::str::FromStr; use std::convert::From;

fn str2Ndt(str: &str) -> NaiveDateTime { NaiveDateTime::parsefromstr(str, "%Y-%m-%d %H:%M:%S").unwrap() }

impl From<_name_::Account> for models::Account { fn from(i: name::Account) -> Self { models::Account{ id: i.getid(), clientid: i.getclientid(), balancejpy: i.getbalancejpy().tostring(), balancebtc: i.getbalancebtc().tostring(), balanceeth: i.getbalanceeth().tostring(), createdat: i.getcreatedat().tostring(), } } }

impl From<_name_::Client> for models::Client { fn from(i: name::Client) -> Self { models::Client{ id: i.getid(), openedat: i.getopenedat().tostring(), businessname: i.getbusinessname().tostring(), businessaddress: i.getbusinessaddress().tostring(), apiusagecount: i.getapiusagecount(), apiusagesecond: i.getapiusagesecond(), commissionfixedcurrency: i.getcommissionfixedcurrency(), commissionfixedamount: i.getcommissionfixedamount(), commissionfixedbp: i.getcommissionfixedbp(), } } }

impl From<_name_::Contact> for models::Contact { fn from(i: name::Contact) -> Self { models::Contact{ clientid: i.getclientid(), name: i.getname().tostring(), position: i.getposition().tostring(), phone: i.getphone().tostring(), email: i.getemail().tostring(), createdat: i.getcreatedat().to_string(), } } }

impl From<_name_::FiatInstitution> for models::FiatInstitution { fn from(i: name::FiatInstitution) -> Self { models::FiatInstitution{ id: i.getid(), clientid: i.getclientid(), name: i.getname().tostring(), code: i.getcode().tostring(), routingcode: i.getroutingcode().tostring(), accountname: i.getaccountname().tostring(), accountcode: i.getaccountcode().tostring(), linkcode: i.getlinkcode().tostring(), createdat: i.getcreatedat().tostring(), } } }

impl From<_name_::Order> for models::Order { fn from(i: name::Order) -> Self { models::Order{ id: i.getid(), accountid: i.getaccountid(), placedat: i.getplacedat().tostring(), initialprocessat: i.getinitialprocessat().tostring(), assetfrom: i.getassetfrom(), assetto: i.getassetto(), side: i.getside(), type: i.gettype(), amount: i.getamount().tostring(), filled: i.getfilled().tostring(), status: i.getstatus(), closedat: i.getclosedat().tostring(), commissioncurrency: i.getcommissioncurrency(), commissionfixed: i.getcommissionfixed(), commissionbp: i.getcommissionbp(), trackingcode: i.gettrackingcode().tostring(), } } } ```

``` rust use bigdecimal::BigDecimal; use models; use proto::name; use protobuf::RepeatedField; use std::collections::HashMap; use std::convert::From;

impl From for name::Account { fn from(i: models::Account) -> Self { let mut o = name::Account::new(); o.setid(i.id.into()); o.setclientid(i.clientid.into()); o.setbalancejpy(i.balancejpy.tostring()); o.setbalancebtc(i.balancebtc.tostring()); o.setbalanceeth(i.balanceeth.tostring()); o.setcreatedat(i.createdat.tostring()); o } }

impl From for name::Client { fn from(i: models::Client) -> Self { let mut o = name::Client::new(); o.setid(i.id.into()); o.setopenedat(i.openedat.tostring()); o.setbusinessname(i.businessname.tostring()); o.setbusinessaddress(i.businessaddress.tostring()); o.setapiusagecount(i.apiusagecount.into()); o.setapiusagesecond(i.apiusagesecond.into()); o.setcommissionfixedcurrency(i.commissionfixedcurrency.into()); o.setcommissionfixedamount(i.commissionfixedamount.into()); o.setcommissionfixedbp(i.commissionfixedbp.into()); o } }

impl From for name::Contact { fn from(i: models::Contact) -> Self { let mut o = name::Contact::new(); o.setclientid(i.clientid.into()); o.setname(i.name.tostring()); o.setposition(i.position.tostring()); o.setphone(i.phone.tostring()); o.setemail(i.email.tostring()); o.setcreatedat(i.createdat.to_string()); o } }

impl From for name::FiatInstitution { fn from(i: models::FiatInstitution) -> Self { let mut o = name::FiatInstitution::new(); o.setid(i.id.into()); o.setclientid(i.clientid.into()); o.setname(i.name.tostring()); o.setcode(i.code.tostring()); o.setroutingcode(i.routingcode.tostring()); o.setaccountname(i.accountname.tostring()); o.setaccountcode(i.accountcode.tostring()); o.setlinkcode(i.linkcode.tostring()); o.setcreatedat(i.createdat.tostring()); o } }

impl From for name::Order { fn from(i: models::Order) -> Self { let mut o = name::Order::new(); o.setid(i.id.into()); o.setaccountid(i.accountid.into()); o.setplacedat(i.placedat.tostring()); o.setinitialprocessat(i.initialprocessat.tostring()); o.setassetfrom(i.assetfrom.into()); o.setassetto(i.assetto.into()); o.setside(i.side.into()); o.settype(i.type.into()); o.setamount(i.amount.tostring()); o.setfilled(i.filled.tostring()); o.setstatus(i.status.into()); o.setclosedat(i.closedat.tostring()); o.setcommissioncurrency(i.commissioncurrency.into()); o.setcommissionfixed(i.commissionfixed.into()); o.setcommissionbp(i.commissionbp.into()); o.settrackingcode(i.trackingcode.tostring()); o } } ```