refactor: Rearrange object store config validation to be differently scoped

There were already too many items in the top-level match that were being
ignored in some match arms, and I'm about to add more, so switch to an
MLM (Multi-Level Match, not Multi-Level Marketing ;))
pull/24376/head
Carol (Nichols || Goulding) 2021-02-26 16:48:01 -05:00
parent daacae5edc
commit 457747c6c4
1 changed files with 41 additions and 38 deletions

View File

@ -158,52 +158,55 @@ impl TryFrom<&Config> for ObjectStore {
type Error = Error; type Error = Error;
fn try_from(config: &Config) -> Result<Self, Self::Error> { fn try_from(config: &Config) -> Result<Self, Self::Error> {
let os = match ( match config.object_store {
config.object_store, Some(ObjStoreOpt::Memory) | None => {
config.bucket.as_ref(), Ok(Self::new_in_memory(object_store::memory::InMemory::new()))
config.database_directory.as_ref(),
) {
(Some(ObjStoreOpt::Google), Some(bucket), _) => {
Self::new_google_cloud_storage(GoogleCloudStorage::new(bucket))
} }
(Some(ObjStoreOpt::Google), None, _) => {
return InvalidCloudObjectStoreConfiguration { Some(ObjStoreOpt::Google) => match config.bucket.as_ref() {
Some(bucket) => Ok(Self::new_google_cloud_storage(GoogleCloudStorage::new(
bucket,
))),
None => InvalidCloudObjectStoreConfiguration {
object_store: ObjStoreOpt::Google, object_store: ObjStoreOpt::Google,
} }
.fail(); .fail(),
} },
(Some(ObjStoreOpt::S3), Some(bucket), _) => {
// rusoto::Region's default takes the value from the AWS_DEFAULT_REGION env var. Some(ObjStoreOpt::S3) => {
Self::new_amazon_s3(AmazonS3::new(Default::default(), bucket)) match config.bucket.as_ref() {
} Some(bucket) => {
(Some(ObjStoreOpt::S3), None, _) => { // rusoto::Region's default takes the value from the AWS_DEFAULT_REGION env
return InvalidCloudObjectStoreConfiguration { // var.
object_store: ObjStoreOpt::S3, Ok(Self::new_amazon_s3(AmazonS3::new(
Default::default(),
bucket,
)))
}
None => InvalidCloudObjectStoreConfiguration {
object_store: ObjStoreOpt::S3,
}
.fail(),
} }
.fail();
} }
(Some(ObjStoreOpt::File), _, Some(ref db_dir)) => {
fs::create_dir_all(db_dir).context(CreatingDatabaseDirectory { path: db_dir })?; Some(ObjStoreOpt::Azure) => match config.bucket.as_ref() {
Self::new_file(object_store::disk::File::new(&db_dir)) Some(_bucket) => unimplemented!(),
} None => InvalidCloudObjectStoreConfiguration {
(Some(ObjStoreOpt::File), _, None) => {
return InvalidFileObjectStoreConfiguration.fail();
}
(Some(ObjStoreOpt::Azure), Some(_bucket), _) => {
unimplemented!();
}
(Some(ObjStoreOpt::Azure), None, _) => {
return InvalidCloudObjectStoreConfiguration {
object_store: ObjStoreOpt::Azure, object_store: ObjStoreOpt::Azure,
} }
.fail(); .fail(),
} },
(Some(ObjStoreOpt::Memory), _, _) | (None, _, _) => {
Self::new_in_memory(object_store::memory::InMemory::new())
}
};
Ok(os) Some(ObjStoreOpt::File) => match config.database_directory.as_ref() {
Some(db_dir) => {
fs::create_dir_all(db_dir)
.context(CreatingDatabaseDirectory { path: db_dir })?;
Ok(Self::new_file(object_store::disk::File::new(&db_dir)))
}
None => InvalidFileObjectStoreConfiguration.fail(),
},
}
} }
} }