Skip to content

[P3][maintainability] proto config 约 2,000 行样板代码 #268

Description

@XiNian-dada

现状

nodelite-proto/src/config/ 约 2,000 行样板代码:

  • raw.rs (644 行) = 10 个配置节 × (Raw struct + 手写 Default + validate)
  • defaults.rs (210 行) - 默认值转发函数
  • helpers.rs (259 行) - 验证辅助
  • raw/alerts.rs (389 行) - alerts 配置

每个配置节都是重复模式:Raw struct + Default impl + validate → Final struct。

影响

  • 新增配置项需手写大量样板
  • Default impl 与 validate 逻辑分散,易不一致
  • 代码量大,维护成本高

建议

  1. 使用 serde 默认值

    #[derive(Deserialize)]
    struct ServerConfigRaw {
        #[serde(default = "default_port")]
        port: u16,
    }
    
    fn default_port() -> u16 { 8080 }
  2. 宏生成样板(可选):

    config_section! {
        ServerSection {
            port: u16 = 8080,
            host: String = "0.0.0.0",
        }
    }
  3. 合并 validate

    • 在 deserialize 时用 #[serde(validate)] 钩子
    • 或统一的 validated() builder

验收

  • 代码量减少 40%+
  • 新增配置项无需手写 Default impl
  • cargo test -p nodelite-proto 通过
  • 现有 server.example.toml 加载正常

来源

  • report/project-report-2026-06-11.md 可维护性

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Nice to have / cleanupmaintainabilityCode health, CI, dependencies

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions