rust - Rust配置 crate 和多态类型

标签 rust

#[derive(Debug, Deserialize)]
struct S3StorageConfig {
    url: String,
}

#[derive(Debug, Deserialize)]
struct LocalStorageConfig {
    root: std::path::PathBuf,
}

#[derive(Debug, Deserialize)]
struct StorageConfig {
    storage_type: String
}

#[derive(Debug, Deserialize)]
pub struct Config {
    storages: Vec<StorageConfig>
}

impl Config {
    pub fn new(path:Option<std::path::PathBuf>) -> Result<Self, config::ConfigError>  {
        let mut cfg = config::Config::default();

        if let Some(file_path) = path {
            cfg.merge(config::File::from(file_path)).unwrap();
        }

        cfg.merge(config::Environment::with_prefix("datastore"))?;


        cfg.try_into()
    }
}

假设我想要一个具有
[[storages]]
type: s3
url: ...
[[storages]]
type: local
root: ...
当config执行try_into时,它可以通过type字段的宽限度找到这些结构并将其分配给正确的结构。
我需要做些什么魔术才能做到这一点?
谢谢,

最佳答案

因此,我不确定100%是否要在此处实现,但是您可以使用serde并使用enum序列化/反序列化为所需的类型。
前任:

// This enum takes the place of your 'S3StorageConfig' and 'LocalStorageConfig'.
#[derive( Serialize, Deserialize, Debug )]
#[serde( tag = "type" )]
enum Storage {
    Cloud{ url: String },
    Local{ root: PathBuf },
}

fn main( ) {
    let vec = vec![ 
        Storage::Cloud{ url: "www.youtube.com".to_string( ) },
        Storage::Local{ root: PathBuf::from( "C:\\Windows\\Fonts" ) },
    ];

    let storage = serde_json::to_string( &vec ).unwrap( );

    let vec: Vec<Storage> = serde_json::from_str( &storage ).unwrap( );
    println!( "{:#?}", vec );
}
现在,您将从Storage类返回一个enum Config变体。
如果这是您决定采取的方向,则无需impl TryInto
impl Config {
    pub fn new( ) -> Result<Storage, config::ConfigError> {
        // Read in the file here and use 'serde' to deserialize the
        // content of the file into the correct enum variant that you
        // can now return.
    }
}

关于rust - Rust配置 crate 和多态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66478420/

相关文章:

rust - 为什么 "match"与 Rust 中的确切代码不匹配?

functional-programming - 有没有更好的功能方法来处理带有错误检查的向量?

rust - 在 Rust 中创建关联数组

rust - 有没有办法在不使它成为静态mut的情况下初始化一个非平凡的静态std::collections::HashMap?

rust - chars().count() 时间复杂度

rust - 使用 actix-web 2.0 提供静态文件

rust - 如何使用Wasm-Bindgen Web_sys Wasm-pack将字符串从Js传递给通过Rust生成的Wasm

linked-list - Rust 中的单链表

rust - 如何在 Rust 中实现静态缓存?

rust - 在 Rust 中声明向量域