#[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/