buddhabrot
buddhabrot

Reputation: 1586

Rust config crate and polymorphic types

#[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()
    }
}

Suppose I want to have a config that has

[[storages]]
type: s3
url: ...

and

[[storages]]
type: local
root: ...

And when config does try_into, it is able to find these structs and assign them to the correct structs, by grace of the type field.

What magic would I need to do to make this happen?

Thanks,

Upvotes: 0

Views: 336

Answers (1)

WBuck
WBuck

Reputation: 5503

So, I'm not 100% sure what you're trying to achieve here but you can serialize/deserialize into the types that you want with serde and using an enum instead.

Ex:

// 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 );
}

Now you will return a Storage enum variant from your Config class. You wont need to impl TryInto if this is the direction you decide to take.

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

Upvotes: 2

Related Questions