V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
steins2628
V2EX  ›  问与答

rust 写的程序,当配置数据在版本间存在不同的时候,怎么样存储比较妥善?

  •  
  •   steins2628 · 362 天前 · 753 次点击
    这是一个创建于 362 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如两个版本间数据是这样的

    // For v0.0.1
    pub struct AppConfig {
        pub auto_start: bool,
        pub version: String,
    }
    
    // For v0.0.2
    pub struct AppConfig {
        pub auto_start: bool,
        pub language: String,
        pub version: String,
    }
    

    我当前的方案是选择用 toml 存配置,代码如下

    if let Some(proj_dirs) = ProjectDirs::from(
            "dev",
            "",
            "MyApp"
        ) {
            let config_dir = proj_dirs.config_dir();
    
            let config_file = fs::read_to_string(
                config_dir.join("config.toml")
            );
    
            let config: AppConfig = match config_file {
                // 如果这里读到的是 v0.0.1 的数据,直接报错
                Ok(file) => toml::from_str(&file).unwrap(), 
                Err(_) => AppConfig {
                    auto_start: true,
                    version: "v0.0.2".to_string(),
                }
            };
    
            debug!("Initialize app config from {}", config_dir.join("config.toml").display());
            return Ok(config);
        } else {
            return Err("failed to get app config".to_string());
        }
    

    也有问过别人,有说用 Option 的,或者用 serde 的 field attributes 的

    Option 的话,多版本好像还是会很痛苦,field attributes 估计更甚

    所以有没有什么好一点的存取和解析的方法,能够在读写不同版本配置文件的时候,稍微方便点?

    3 条回复    2023-04-02 16:58:32 +08:00
    crysislinux
        1
    crysislinux  
       362 天前 via Android
    没写过 rust 。一般做法是确定要支持多少个版本的配置,每两个相邻版本写好 migration ,因此每个 migration 的起点和终点类型是确定的。然后读 version ,version 不在支持范围直接报错。否则就一个一个的 migrate 上来知道最新版本
    israinbow
        2
    israinbow  
       362 天前
    这样处理没问题, 要做兼容就 #1 说的那样加 migration 逻辑, 或者给新配置设默认值允许配置覆盖, 配置字段发生更改也做一个大版本的 deprecate 支持; 还有其他常见的作法就直接把配置写成 template, 初始化的时候生成, 这样你的版本管理工具也能顺带管理配置文件了.
    steins2628
        3
    steins2628  
    OP
       362 天前
    @crysislinux 我就是觉得这样太痛苦了,作为一个前端,js 不太在乎类型我还可以直接 json 转 obj ,ts 我也可以用 any ,rust 这个类型安全我是实在没办法了, 谢谢指点


    @israinbow 一开始的方案就是 migration ,但配置项一多就写的痛苦了,deprecate 是个好方法,谢谢指点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2784 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:55 · PVG 19:55 · LAX 04:55 · JFK 07:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.