rust - 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
并使用 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.
}
}
推荐阅读
- javascript - Javascript - 如何检查变量之前未声明
- javascript - 在 Javascript 中使用 setter 来“覆盖”属性
- angular - 从数组构建 mat-tab-group 时关闭 mat-tab 后组件不会被破坏
- postgresql - 将数据从 AWS S3 加载到 Aurora Postgres
- php - 通过编码的随机字符串
- performance - Powershell 循环性能/内存使用情况
- javascript - .getTime 当 $(document).ready 倒计时
- java - AWS Lambda 函数无法读取我的 .bin 文件 (Java)
- arrays - bash中对话框清单中的数组
- python - 如何使用 sci-kit learn TfidfVectorizer 忽略出现超过 50% 的单词?