Diesel CLI Extension is a tool-belt that aids Diesel CLI after it built schema.rs.
It contains 4 functions at this moment.
- Generate protobuf file.(
diesel_ext proto
) - Generate model rust structs.(
diesel_ext model
) - Generate conversion implementations.(
diesel_ext into_proto
, anddiesel_ext from_proto
)
cargo install diesel_cli_ext
First of all, diesel print-schema > src/schema.rs
TL;DR:
Usage: target/debug/diesel_ext FILE [options]
Common Options:
-s, --schema-file PATH
Set schema file path
-h, --help Print this help menu
Model Options:
-m, --model Set as model output
-M, --map "FROM_TYPE TO_TYPE"
Set type mappings (can be set multiple times) e.g.
--map "BigInt iccc"
-I, --import-types "TYPE"
This field adds use statements to the top of every
table! declaration. (can be set multiple times) e.g.
--import_types "diesel::sql_types::*"
--derive-mod "TABLENAME MODIFIER"
(NOT ready)This field adds derives for certain tables.
(can be set multiple times) e.g. --derive-mod
"table_name +Debug" --derive-mod "table_name2 -Debug"
-d, --derive DERIVES
set struct derives
-r, --rust_styled_model_fields
set struct field names to be styled according to Rust guidelines
Proto Options:
-t, --add-table-name
Add #[table_name = x] before structs
-p, --proto Set as proto output
-i, --into_proto Set as into_proto output
(You can see it again by diesel_ext --help
)
Output demonstrations are as below...
e.g. diesel_ext > src/db/db_models.rs
, diesel_ext -m > src/models.rs
, diesel_ext --model > src/models.rs
(it is the default option)
Sample model output:
use chrono::NaiveDateTime;
use bigdecimal::BigDecimal;
#[derive(Queryable)]
pub struct CarryOverBalance {
pub account_id : i64,
pub debit : BigDecimal,
pub description : String,
}
#[derive(Queryable)]
pub struct Order {
pub id1 : i64,
pub time : NaiveDateTime,
pub json : String,
}
diesel_ext -p > myproto.proto
, diesel_ext --proto > myproto.proto
Sample output:
syntax = "proto3";
message CarryOverBalance {
int64 account_id = 1;
string debit = 2;
string description = 3;
}
message Order {
int64 id1 = 1;
string time = 2;
string json = 3;
}
message EnquireCarryOverBalanceRequest {
int64 id =1;
}
message EnquireOrderRequest {
int64 id =1;
}
service MessageRpc {
rpc getCarryOverBalance (EnquireCarryOverBalanceRequest) returns (CarryOverBalance) { }
rpc getOrder (EnquireOrderRequest) returns (Order) { }
}
diesel_ext -f -c class_name > proto/src/conversion/from_proto.rs
, diesel_ext -i -c class_name > proto/src/conversion/into_proto.rs
(if you omit the second parameter, names will be displayed as _name_
for your search and replace.)
Sample output(from):
use models;
use proto::client_service;
use std::str::FromStr;
use std::convert::From;
impl From<class_name::CarryOverBalance> for models::CarryOverBalance {
fn from(i: class_name::CarryOverBalance) -> Self {
models::CarryOverBalance{
account_id: i.get_account_id(),
debit: i.get_debit().to_string(),
description: i.get_description().to_string(),
}
}
}
impl From<class_name::Order> for models::Order {
fn from(i: class_name::Order) -> Self {
models::Order{
id1: i.get_id1(),
time: i.get_time().to_string(),
json: i.get_json().to_string(),
}
}
}
into:
use models;
use proto::client_service;
use std::str::FromStr;
use std::convert::From;
impl From<models::CarryOverBalance> for class_name::CarryOverBalance {
fn from(i: models::CarryOverBalance) -> Self {
let mut o = class_name::CarryOverBalance::new();
o.set_account_id(i.account_id.into());
o.set_debit(i.debit.to_string());
o.set_description(i.description.to_string());
o
}
}
impl From<models::Order> for class_name::Order {
fn from(i: models::Order) -> Self {
let mut o = class_name::Order::new();
o.set_id1(i.id1.into());
o.set_time(i.time.to_string());
o.set_json(i.json.to_string());
o
}
}