Skip to content

Commit 20c4672

Browse files
committed
refactor: remove ObjectType in favour of a more flattened config hierarchy
1 parent aae1b2c commit 20c4672

File tree

20 files changed

+184
-168
lines changed

20 files changed

+184
-168
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

htsget-config/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repository = "https://github.com/umccr/htsget-rs"
1212

1313
[features]
1414
s3-storage = []
15-
url-storage = ["dep:reqwest"]
15+
url-storage = ["dep:reqwest", "dep:cfg-if"]
1616
experimental = ["dep:crypt4gh"]
1717
default = []
1818

@@ -37,6 +37,7 @@ rustls-pki-types = "1"
3737

3838
# url-storage
3939
reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false, optional = true }
40+
cfg-if = { version = "1", optional = true }
4041

4142
# Crypt4GH
4243
crypt4gh = { version = "0.4", git = "https://github.com/EGA-archive/crypt4gh-rust", optional = true }

htsget-config/src/resolver.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use serde_with::with_prefix;
88
use tracing::instrument;
99

1010
use crate::config::DataServerConfig;
11-
use crate::storage::local::LocalStorage;
11+
use crate::storage::local::Local;
1212
#[cfg(feature = "s3-storage")]
13-
use crate::storage::s3::S3Storage;
13+
use crate::storage::s3::S3;
1414
#[cfg(feature = "url-storage")]
1515
use crate::storage::url::UrlStorageClient;
1616
use crate::storage::{ResolvedId, Storage};
@@ -27,11 +27,11 @@ pub trait IdResolver {
2727
#[async_trait]
2828
pub trait ResolveResponse {
2929
/// Convert from `LocalStorage`.
30-
async fn from_local(local_storage: &LocalStorage, query: &Query) -> Result<Response>;
30+
async fn from_local(local_storage: &Local, query: &Query) -> Result<Response>;
3131

3232
/// Convert from `S3Storage`.
3333
#[cfg(feature = "s3-storage")]
34-
async fn from_s3(s3_storage: &S3Storage, query: &Query) -> Result<Response>;
34+
async fn from_s3(s3_storage: &S3, query: &Query) -> Result<Response>;
3535

3636
/// Convert from `UrlStorage`.
3737
#[cfg(feature = "url-storage")]
@@ -444,7 +444,7 @@ mod tests {
444444

445445
use crate::config::tests::{test_config_from_env, test_config_from_file};
446446
#[cfg(feature = "s3-storage")]
447-
use crate::storage::s3::S3Storage;
447+
use crate::storage::s3::S3;
448448
use crate::types::Scheme::Http;
449449
use crate::types::Url;
450450

@@ -454,15 +454,15 @@ mod tests {
454454

455455
#[async_trait]
456456
impl ResolveResponse for TestResolveResponse {
457-
async fn from_local(local_storage: &LocalStorage, _: &Query) -> Result<Response> {
457+
async fn from_local(local_storage: &Local, _: &Query) -> Result<Response> {
458458
Ok(Response::new(
459459
Bam,
460460
vec![Url::new(local_storage.authority().to_string())],
461461
))
462462
}
463463

464464
#[cfg(feature = "s3-storage")]
465-
async fn from_s3(s3_storage: &S3Storage, _: &Query) -> Result<Response> {
465+
async fn from_s3(s3_storage: &S3, _: &Query) -> Result<Response> {
466466
Ok(Response::new(Bam, vec![Url::new(s3_storage.bucket())]))
467467
}
468468

@@ -477,12 +477,11 @@ mod tests {
477477

478478
#[tokio::test]
479479
async fn resolver_resolve_local_request() {
480-
let local_storage = LocalStorage::new(
480+
let local_storage = Local::new(
481481
Http,
482482
Authority::from_static("127.0.0.1:8080"),
483483
"data".to_string(),
484484
"/data".to_string(),
485-
Default::default(),
486485
false,
487486
);
488487
let resolver = Resolver::new(
@@ -499,7 +498,7 @@ mod tests {
499498
#[cfg(feature = "s3-storage")]
500499
#[tokio::test]
501500
async fn resolver_resolve_s3_request_tagged() {
502-
let s3_storage = S3Storage::new("id".to_string(), None, false, Default::default());
501+
let s3_storage = S3::new("id".to_string(), None, false);
503502
let resolver = Resolver::new(
504503
Storage::S3(s3_storage),
505504
"(id)-1",
@@ -515,7 +514,7 @@ mod tests {
515514
#[tokio::test]
516515
async fn resolver_resolve_s3_request() {
517516
let resolver = Resolver::new(
518-
Storage::S3(S3Storage::default()),
517+
Storage::S3(S3::default()),
519518
"(id)-1",
520519
"$1-test",
521520
AllowGuard::default(),
@@ -538,7 +537,6 @@ mod tests {
538537
}),
539538
true,
540539
vec![],
541-
Default::default(),
542540
client,
543541
);
544542

@@ -693,8 +691,7 @@ mod tests {
693691
Interval::new(Some(100), Some(1000)),
694692
);
695693
let resolver = config.resolvers().first().unwrap();
696-
let expected_storage =
697-
S3Storage::new("bucket".to_string(), None, false, Default::default());
694+
let expected_storage = S3::new("bucket".to_string(), None, false);
698695

699696
assert_eq!(resolver.regex().to_string(), "regex");
700697
assert_eq!(resolver.substitution_string(), "substitution_string");

htsget-config/src/storage/object/c4gh.rs renamed to htsget-config/src/storage/c4gh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ mod tests {
117117
test_c4gh_storage_config(r#"backend = "Local""#, |config| {
118118
assert!(matches!(
119119
config.resolvers().first().unwrap().storage(),
120-
Storage::Local(local_storage) if local_storage.object_type().keys().is_some()
120+
Storage::Local(local_storage) if local_storage.keys().is_some()
121121
));
122122
});
123123
}
@@ -133,7 +133,7 @@ mod tests {
133133
|config| {
134134
assert!(matches!(
135135
config.resolvers().first().unwrap().storage(),
136-
Storage::S3(s3_storage) if s3_storage.object_type().keys().is_some()
136+
Storage::S3(s3_storage) if s3_storage.keys().is_some()
137137
));
138138
},
139139
);
@@ -152,7 +152,7 @@ mod tests {
152152
|config| {
153153
assert!(matches!(
154154
config.resolvers().first().unwrap().storage(),
155-
Storage::Url(url_storage) if url_storage.object_type().keys().is_some()
155+
Storage::Url(url_storage) if url_storage.keys().is_some()
156156
));
157157
},
158158
);

htsget-config/src/storage/local.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use http::uri::Authority;
44
use serde::{Deserialize, Serialize};
55

66
use crate::config::{default_localstorage_addr, default_path, DataServerConfig};
7-
use crate::storage::object::ObjectType;
7+
#[cfg(feature = "experimental")]
8+
use crate::storage::c4gh::C4GHKeys;
89
use crate::tls::KeyPairScheme;
910
use crate::types::Scheme;
1011

@@ -18,34 +19,35 @@ fn default_local_path() -> String {
1819

1920
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
2021
#[serde(default)]
21-
pub struct LocalStorage {
22+
pub struct Local {
2223
scheme: Scheme,
2324
#[serde(with = "http_serde::authority")]
2425
authority: Authority,
2526
local_path: String,
2627
path_prefix: String,
27-
#[serde(flatten)]
28-
object_type: ObjectType,
2928
use_data_server_config: bool,
29+
#[serde(skip_serializing, flatten)]
30+
#[cfg(feature = "experimental")]
31+
keys: Option<C4GHKeys>,
3032
}
3133

32-
impl LocalStorage {
34+
impl Local {
3335
/// Create a new local storage.
3436
pub fn new(
3537
scheme: Scheme,
3638
authority: Authority,
3739
local_path: String,
3840
path_prefix: String,
39-
object_type: ObjectType,
4041
use_data_server_config: bool,
4142
) -> Self {
4243
Self {
4344
scheme,
4445
authority,
4546
local_path,
4647
path_prefix,
47-
object_type,
4848
use_data_server_config,
49+
#[cfg(feature = "experimental")]
50+
keys: None,
4951
}
5052
}
5153

@@ -69,38 +71,44 @@ impl LocalStorage {
6971
&self.path_prefix
7072
}
7173

72-
/// Get the object type.
73-
pub fn object_type(&self) -> &ObjectType {
74-
&self.object_type
75-
}
76-
7774
/// Get whether config should be inherited from the data server config.
7875
pub fn use_data_server_config(&self) -> bool {
7976
self.use_data_server_config
8077
}
78+
79+
#[cfg(feature = "experimental")]
80+
/// Set the C4GH keys.
81+
pub fn set_keys(mut self, keys: Option<C4GHKeys>) -> Self {
82+
self.keys = keys;
83+
self
84+
}
85+
86+
#[cfg(feature = "experimental")]
87+
/// Get the C4GH keys.
88+
pub fn keys(&self) -> Option<&C4GHKeys> {
89+
self.keys.as_ref()
90+
}
8191
}
8292

83-
impl Default for LocalStorage {
93+
impl Default for Local {
8494
fn default() -> Self {
85-
Self {
86-
scheme: Scheme::Http,
87-
authority: default_authority(),
88-
local_path: default_local_path(),
89-
path_prefix: Default::default(),
90-
object_type: Default::default(),
91-
use_data_server_config: false,
92-
}
95+
Self::new(
96+
Scheme::Http,
97+
default_authority(),
98+
default_local_path(),
99+
Default::default(),
100+
false,
101+
)
93102
}
94103
}
95104

96-
impl From<&DataServerConfig> for LocalStorage {
105+
impl From<&DataServerConfig> for Local {
97106
fn from(config: &DataServerConfig) -> Self {
98-
LocalStorage::new(
107+
Self::new(
99108
config.tls().get_scheme(),
100109
Authority::from_str(&config.addr().to_string()).expect("expected valid authority"),
101110
config.local_path().to_string_lossy().to_string(),
102111
config.serve_at().to_string(),
103-
Default::default(),
104112
true,
105113
)
106114
}
@@ -151,13 +159,12 @@ mod tests {
151159
None,
152160
CorsConfig::default(),
153161
);
154-
let result: LocalStorage = (&data_server_config).into();
155-
let expected = LocalStorage::new(
162+
let result: Local = (&data_server_config).into();
163+
let expected = Local::new(
156164
Http,
157165
Authority::from_static("127.0.0.1:8080"),
158166
"data".to_string(),
159167
"/data".to_string(),
160-
Default::default(),
161168
true,
162169
);
163170

htsget-config/src/storage/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use crate::storage::local::LocalStorage;
1+
use crate::storage::local::Local;
22
#[cfg(feature = "s3-storage")]
3-
use crate::storage::s3::S3Storage;
3+
use crate::storage::s3::S3;
44
#[cfg(feature = "url-storage")]
55
use crate::storage::url::UrlStorageClient;
66
use serde::{Deserialize, Serialize};
77

8+
#[cfg(feature = "experimental")]
9+
pub mod c4gh;
810
pub mod local;
9-
pub mod object;
1011
#[cfg(feature = "s3-storage")]
1112
pub mod s3;
1213
#[cfg(feature = "url-storage")]
@@ -34,10 +35,10 @@ impl ResolvedId {
3435
#[non_exhaustive]
3536
pub enum Storage {
3637
#[serde(alias = "local", alias = "LOCAL")]
37-
Local(LocalStorage),
38+
Local(Local),
3839
#[cfg(feature = "s3-storage")]
3940
#[serde(alias = "s3")]
40-
S3(S3Storage),
41+
S3(S3),
4142
#[cfg(feature = "url-storage")]
4243
#[serde(alias = "url", alias = "URL")]
4344
Url(#[serde(skip_serializing)] UrlStorageClient),

htsget-config/src/storage/object/mod.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)