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
)
First of all, diesel print-schema > src/schema.rs
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;
pub struct Account { pub id : i32, pub clientid : i32, pub balancejpy : BigDecimal, pub balancebtc : BigDecimal, pub balanceeth : BigDecimal, pub created_at : NaiveDateTime, }
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, }
pub struct Contact { pub clientid : i32, pub name : String, pub position : String, pub phone : String, pub email : String, pub createdat : NaiveDateTime, }
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, }
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, } ```
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) { } } ```
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
impl From
impl From
impl From
impl From