Skip to content

feat(config): TOML 配置加载模块(解析 + 校验 + 测试) #6

Description

@argszero

背景

项目已有 config/config.example.toml(定义 [points] / [[providers]] / [[plans]] / [[price_overrides]] 结构与设计约定,见文件头注释),但 src/ 中还没有任何配置加载代码——main.rs 目前仅输出 banner。下一个自然的功能步骤是让程序能读取这份配置。

建议方案

新增 src/config.rs 模块(feat 级改动),包含:

  1. TOML 解析:添加 toml crate 依赖(serde 已具备 derive feature),用 serde 反序列化 config.example.toml 中的结构:
    • PointsConfig(anchor_currency / points_per_unit / display_name / symbol)
    • Provider(id / name / country / has_plan)
    • Plan(id / provider / type / key_prefix / endpoints[] / interactive_only)
    • PriceOverride(provider / model / currency / input_per_m / output_per_m / cache_hit_input_per_m / source)
    • 顶层 AppConfig { points, providers, plans, price_overrides }
  2. 加载与校验
    • Config::load(path) 读取文件 → 解析 → 校验(如 points_per_unit > 0、plan 引用的 provider 必须存在、endpoints 至少 1 个、protocol 枚举合法)
    • 错误信息友好(行号/字段名)
  3. 单元测试(CONTRIBUTING 要求新功能必须有测试):
    • 用与 config.example.toml 同构的样例 TOML 测试解析
    • 测试校验失败分支(非法 protocol、缺失 provider 引用等)
  4. 示例文件同步:保持 config.example.toml 为单一事实源,如结构有调整需同步(CONTRIBUTING 约定)

范围与节奏

  • 这是第一个功能模块,建议先在 issue 讨论确认结构命名与校验规则,认可后再实现(Ask before doing)
  • 可拆分:先做「解析 + 基础校验」PR,再做「校验规则细化」后续 PR
  • 不影响既有 banner/测试;cargo test / fmt --check / clippy -D warnings 须全绿

备选

若主开发会话计划自己实现 config 模块,可关闭本 issue 或标注由谁认领;本提案仅为对齐方向。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions