Skip to content

Commit 713c007

Browse files
committed
refactor(config): slight adjustment to reduce conditionally compiled code branches
1 parent 415d687 commit 713c007

File tree

3 files changed

+38
-92
lines changed

3 files changed

+38
-92
lines changed

htsget-config/src/resolver.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,16 @@ impl Resolver {
293293

294294
/// Set the local resolvers from the data server config.
295295
pub fn resolvers_from_data_server_config(&mut self, config: &DataServerConfig) {
296-
if let Storage::Local(local) = self.storage() {
297-
if local.use_data_server_config() {
298-
self.storage = Storage::Local(config.into());
296+
match self.storage() {
297+
Storage::Local(local) => {
298+
if local.use_data_server_config() {
299+
self.storage = Storage::Local(config.into());
300+
}
299301
}
302+
#[cfg(feature = "s3-storage")]
303+
Storage::S3(_) => {}
304+
#[cfg(feature = "url-storage")]
305+
Storage::Url(_) => {}
300306
}
301307
}
302308

@@ -384,29 +390,21 @@ impl StorageResolver for Resolver {
384390

385391
query.set_id(resolved_id.into_inner());
386392

387-
if let Some(response) = self.storage().resolve_local_storage::<T>(query).await {
388-
return Some(response);
389-
}
390-
391-
#[cfg(feature = "s3-storage")]
392-
{
393-
let first_match = self.get_match(1, &_matched_id);
394-
395-
if let Some(response) = self
396-
.storage()
397-
.resolve_s3_storage::<T>(first_match, query)
398-
.await
399-
{
400-
return Some(response);
393+
match self.storage() {
394+
Storage::Local(local_storage) => Some(T::from_local(local_storage, query).await),
395+
#[cfg(feature = "s3-storage")]
396+
Storage::S3(s3_storage) => {
397+
let first_match = self.get_match(1, &_matched_id);
398+
let mut s3_storage = s3_storage.clone();
399+
if s3_storage.bucket.is_empty() {
400+
s3_storage.bucket = first_match?.to_string();
401+
}
402+
403+
Some(T::from_s3(&s3_storage, query).await)
401404
}
405+
#[cfg(feature = "url-storage")]
406+
Storage::Url(url_storage) => Some(T::from_url(url_storage, query).await),
402407
}
403-
404-
#[cfg(feature = "url-storage")]
405-
if let Some(response) = self.storage().resolve_url_storage::<T>(query).await {
406-
return Some(response);
407-
}
408-
409-
None
410408
}
411409
}
412410

htsget-config/src/storage/mod.rs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use crate::resolver::ResolveResponse;
21
use crate::storage::local::LocalStorage;
32
#[cfg(feature = "s3-storage")]
43
use crate::storage::s3::S3Storage;
54
#[cfg(feature = "url-storage")]
65
use crate::storage::url::UrlStorageClient;
7-
use crate::types::{Query, Response, Result};
86
use serde::{Deserialize, Serialize};
97

108
pub mod local;
@@ -43,55 +41,6 @@ pub enum Storage {
4341
#[cfg(feature = "url-storage")]
4442
#[serde(alias = "url", alias = "URL")]
4543
Url(#[serde(skip_serializing)] UrlStorageClient),
46-
#[serde(skip)]
47-
Unknown,
48-
}
49-
50-
impl Storage {
51-
/// Resolve the local component `Storage` into a type that implements `FromStorage`. Tagged
52-
/// `Local` storage is not resolved because it is resolved into untagged `Local` storage when
53-
/// `Config` is constructed.
54-
pub async fn resolve_local_storage<T: ResolveResponse>(
55-
&self,
56-
query: &Query,
57-
) -> Option<Result<Response>> {
58-
match self {
59-
Storage::Local(local_storage) => Some(T::from_local(local_storage, query).await),
60-
_ => None,
61-
}
62-
}
63-
64-
/// Resolve the s3 component of `Storage` into a type that implements `FromStorage`.
65-
#[cfg(feature = "s3-storage")]
66-
pub async fn resolve_s3_storage<T: ResolveResponse>(
67-
&self,
68-
first_match: Option<&str>,
69-
query: &Query,
70-
) -> Option<Result<Response>> {
71-
match self {
72-
Storage::S3(s3_storage) => {
73-
let mut s3_storage = s3_storage.clone();
74-
if s3_storage.bucket.is_empty() {
75-
s3_storage.bucket = first_match?.to_string();
76-
}
77-
78-
Some(T::from_s3(&s3_storage, query).await)
79-
}
80-
_ => None,
81-
}
82-
}
83-
84-
/// Resolve the url component of `Storage` into a type that implements `FromStorage`.
85-
#[cfg(feature = "url-storage")]
86-
pub async fn resolve_url_storage<T: ResolveResponse>(
87-
&self,
88-
query: &Query,
89-
) -> Option<Result<Response>> {
90-
match self {
91-
Storage::Url(url_storage) => Some(T::from_url(url_storage, query).await),
92-
_ => None,
93-
}
94-
}
9544
}
9645

9746
impl Default for Storage {

htsget-storage/src/lib.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,30 @@ impl StorageTrait for Storage {
137137
}
138138

139139
impl Storage {
140-
#[cfg(feature = "experimental")]
141-
/// Wrap an existing storage with C4GH storage.
142-
pub fn c4gh_storage(object_type: &ObjectType, storage: Storage) -> Storage {
143-
if let Some(keys) = object_type.keys() {
144-
Storage::new(C4GHStorage::new_box(
145-
keys.clone().into_inner(),
146-
storage.into_inner(),
147-
))
148-
} else {
149-
storage
150-
}
151-
}
152-
153-
/// Create from local storage config.
154-
pub async fn from_local(config: &LocalStorageConfig) -> Result<Storage> {
155-
let storage = Storage::new(LocalStorage::new(config.local_path(), config.clone())?);
140+
/// Wrap an existing storage with the object type storage.
141+
pub fn from_object_type(object_type: &ObjectType, storage: Storage) -> Storage {
156142
cfg_if! {
157143
if #[cfg(feature = "experimental")] {
158-
Ok(Self::c4gh_storage(config.object_type(), storage))
144+
if let Some(keys) = object_type.keys() {
145+
Storage::new(C4GHStorage::new_box(
146+
keys.clone().into_inner(),
147+
storage.into_inner(),
148+
))
149+
} else {
150+
storage
151+
}
159152
} else {
160153
Ok(storage)
161154
}
162155
}
163156
}
164157

158+
/// Create from local storage config.
159+
pub async fn from_local(config: &LocalStorageConfig) -> Result<Storage> {
160+
let storage = Storage::new(LocalStorage::new(config.local_path(), config.clone())?);
161+
Ok(Storage::from_object_type(config.object_type(), storage))
162+
}
163+
165164
/// Create from s3 config.
166165
#[cfg(feature = "s3-storage")]
167166
pub async fn from_s3(s3_storage: &S3StorageConfig) -> Storage {

0 commit comments

Comments
 (0)