22 个版本 (11 个稳定版)

2.0.0 2024年2月26日
1.2.2 2024年2月26日
1.2.0 2023年10月21日
1.0.0 2023年5月24日
0.1.10 2023年5月24日

45模板引擎 中排名

Download history 6/week @ 2024-07-03 176/week @ 2024-07-24 47/week @ 2024-07-31

每月下载 223

MIT/Apache

56KB
1K SLoC

rtile

rtile 提供了一种将文本矩形区域作为原子单元处理的方法,可用于代码生成

示例请参考 GitHub 仓库中的测试文件夹

如何使用

use rtile::prelude::*;
use std::collections::BTreeMap;

fn main() {
    codegen();
}

fn codegen() {
    tp!(
        struct_def,
        "
            #[derive(Default, Debug)]
            @{s_vis}struct @{s_name}{
                @{s_members}
            }
        "
    );
    let mut input: BTreeMap<&str, (bool, Vec<&str>, Vec<&str>)> = BTreeMap::new();
    input.insert(
        "Person",
        (
            true,
            vec!["name", "age", "address", "properties"],
            vec!["String", "u32", "Vec<Address>", "Properties"],
        ),
    );
    input.insert(
        "Address",
        (
            true,
            vec!["street", "city", "state", "zip"],
            vec!["String", "String", "String", "String"],
        ),
    );
    input.insert(
        "Properties",
        (
            true,
            vec!["gender", "kids", "other_details"],
            vec!["Gender", "Option<u8>", "OtherDetails"],
        ),
    );
    let mut struct_codes = vec![];
    for (key, value) in &input {
        if value.0 {
            kp!(s_vis, "pub ");
        } else {
            tp!(s_vis);
        }
        tp!(s_name, "{}", key);
        let val: Vec<_> = value
            .1
            .iter()
            .zip(&value.2)
            .map(|(k, v)| {
                if value.0 {
                    format!("pub {}: {},", k, v)
                } else {
                    format!("{}: {},", k, v)
                }
            })
            .collect();
        tp!(s_members, t!(val));
        struct_codes.push(ts!("@{struct_def}"));
    }
    tp!(
        enum_def,
        "
            #[derive(Debug)]
            @{e_vis}enum @{e_name}{
                @{e_members}
            }
        "
    );
    let mut input: BTreeMap<&str, (bool, Vec<&str>, Vec<&str>)> = BTreeMap::new();
    input.insert(
        "EmploymentStatus",
        (
            true,
            vec![
                "Employed",
                "Unemployed",
                "Employer",
                "Retired",
                "NotApplicable",
            ],
            vec![""; 5],
        ),
    );
    input.insert(
        "Gender",
        (true, vec!["Unknown", "Male", "Female"], vec![""; 3]),
    );
    input.insert(
        "OtherDetails",
        (
            true,
            vec!["Miscellaneous"],
            vec!["{education: Option<String>, employment_status: EmploymentStatus,}"],
        ),
    );
    let mut enum_codes = vec![];
    for (key, value) in &input {
        if value.0 {
            kp!(e_vis, "pub ");
        } else {
            tp!(e_vis);
        }
        tp!(e_name, "{}", key);
        let val: Vec<_> = value
            .1
            .iter()
            .zip(&value.2)
            .map(|(k, v)| format!("{}{},", k, v))
            .collect();
        tp!(e_members, t!(val));
        enum_codes.push(ts!("@{enum_def}"));
    }
    struct_codes
        .into_iter()
        .for_each(|code_item| println!("{code_item}\n"));
    enum_codes
        .into_iter()
        .for_each(|code_item| println!("{code_item}\n"));
}

格式化输出

#[derive(Default, Debug)]
pub struct Address {
    pub street: String,
    pub city: String,
    pub state: String,
    pub zip: String,
}

#[derive(Default, Debug)]
pub struct Person {
    pub name: String,
    pub age: u32,
    pub address: Vec<Address>,
    pub properties: Properties,
}

#[derive(Default, Debug)]
pub struct Properties {
    pub gender: Gender,
    pub kids: Option<u8>,
    pub other_details: OtherDetails,
}

#[derive(Debug)]
pub enum EmploymentStatus {
    Employed,
    Unemployed,
    Employer,
    Retired,
    NotApplicable,
}

#[derive(Debug)]
pub enum Gender {
    Unknown,
    Male,
    Female,
}

#[derive(Debug)]
pub enum OtherDetails {
    Miscellaneous {
        education: Option<String>,
        employment_status: EmploymentStatus,
    },
}

无运行时依赖