#diesel #codegen #cli #diesel-cli

app diesel_cli_ext

为使用 diesel_cli 的项目提供不同的工具

41 个版本

0.3.14 2024年5月23日
0.3.13 2023年7月6日
0.3.10 2022年11月11日
0.3.6 2020年9月9日
0.1.11 2018年6月29日

数据库接口 中排名第 58

Download history 31/week @ 2024-04-27 45/week @ 2024-05-04 39/week @ 2024-05-11 192/week @ 2024-05-18 64/week @ 2024-05-25 44/week @ 2024-06-01 45/week @ 2024-06-08 29/week @ 2024-06-15 36/week @ 2024-06-22 32/week @ 2024-06-29 44/week @ 2024-07-06 43/week @ 2024-07-13 58/week @ 2024-07-20 412/week @ 2024-07-27 94/week @ 2024-08-03 20/week @ 2024-08-10

589 每月下载量

MIT/Apache

59KB
1.5K SLoC

柴油 CLI 扩展

柴油 CLI 扩展是在构建 schema.rs 之后帮助柴油 CLI 的工具集。

Build Status Crates.io

目前包含 4 个函数。

  1. 生成 protobuf 文件。(diesel_ext proto
  2. 生成模型 Rust 结构。(diesel_ext model
  3. 生成转换实现。(diesel_ext into_protodiesel_ext from_proto

安装

cargo安装 diesel_cli_ext

如何使用

首先,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

(您可以通过 diesel_ext --help 再次查看)

以下为输出示例...

生成模型结构

例如,diesel_ext > src/db/db_models.rsdiesel_ext -m > src/models.rsdiesel_ext --model > src/models.rs(这是默认选项)

模型输出示例

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,
}

生成预定义的 proto 文件

diesel_ext -> myproto.protodiesel_ext --proto > myproto.proto

输出示例

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) { }
}

生成 proto 转换

diesel_ext -f -c class_name > proto/src/conversion/from_proto.rsdiesel_ext -i -c class_name > proto/src/conversion/into_proto.rs

(如果您省略第二个参数,名称将显示为 _name_ 以供您查找和替换。)

输出示例(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
    }
}

依赖

~1.5MB
~22K SLoC