首页 > 解决方案 > 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领域。

我需要做什么魔法才能做到这一点?

谢谢,

标签: rust

解决方案


所以,我不是 100% 确定你想在这里实现什么,但你可以序列化/反序列化为你想要的类型,serde并使用 anenum来代替。

前任:

// 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.
    }
}

推荐阅读