Skip to content

Commit c8d624c

Browse files
committed
refactor(config)!: remove object type and specify keys directly
1 parent ded4d9d commit c8d624c

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

htsget-config/examples/config-files/c4gh.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ regex = ".*"
99
substitution_string = "$0"
1010

1111
[resolvers.storage]
12-
object_type = { private_key = "data/c4gh/keys/bob.sec", recipient_public_key = "data/c4gh/keys/alice.pub" } # pragma: allowlist secret
12+
private_key = "data/c4gh/keys/bob.sec" # pragma: allowlist secret
13+
recipient_public_key = "data/c4gh/keys/alice.pub"

htsget-config/src/storage/local.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct LocalStorage {
2424
authority: Authority,
2525
local_path: String,
2626
path_prefix: String,
27+
#[serde(flatten)]
2728
object_type: ObjectType,
2829
}
2930

htsget-config/src/storage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl ResolvedId {
4949

5050
/// Specify the storage backend to use as config values.
5151
#[derive(Serialize, Deserialize, Debug, Clone)]
52-
#[serde(untagged, deny_unknown_fields)]
52+
#[serde(untagged)]
5353
#[non_exhaustive]
5454
pub enum Storage {
5555
Tagged(TaggedStorageTypes),

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,53 @@ impl From<Crypt4GHError> for Error {
6060
ParseError(err.to_string())
6161
}
6262
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use crate::config::tests::test_config_from_file;
67+
use crate::storage::Storage;
68+
use std::fs::copy;
69+
use std::path::PathBuf;
70+
use tempfile::TempDir;
71+
72+
#[test]
73+
fn config_storage_c4gh() {
74+
let tmp = TempDir::new().unwrap();
75+
let private_key = tmp.path().join("bob.sec");
76+
let recipient_public_key = tmp.path().join("alice.pub");
77+
78+
let parent = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
79+
.parent()
80+
.unwrap()
81+
.to_path_buf();
82+
83+
copy(parent.join("data/c4gh/keys/bob.sec"), &private_key).unwrap();
84+
copy(
85+
parent.join("data/c4gh/keys/alice.pub"),
86+
&recipient_public_key,
87+
)
88+
.unwrap();
89+
90+
test_config_from_file(
91+
&format!(
92+
r#"
93+
[[resolvers]]
94+
regex = "regex"
95+
96+
[resolvers.storage]
97+
private_key = "{}"
98+
recipient_public_key = "{}"
99+
"#,
100+
private_key.to_string_lossy(),
101+
recipient_public_key.to_string_lossy()
102+
),
103+
|config| {
104+
println!("{:?}", config.resolvers().first().unwrap().storage());
105+
assert!(matches!(
106+
config.resolvers().first().unwrap().storage(),
107+
Storage::Local { local_storage } if local_storage.object_type().keys().is_some()
108+
));
109+
},
110+
);
111+
}
112+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use serde::{Deserialize, Serialize};
1010

1111
/// An object type, can be regular or Crypt4GH encrypted.
1212
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
13-
#[serde(untagged, deny_unknown_fields)]
14-
#[non_exhaustive]
15-
pub enum ObjectType {
16-
#[default]
17-
Regular,
13+
pub struct ObjectType {
14+
#[serde(skip_serializing, flatten)]
1815
#[cfg(feature = "experimental")]
19-
C4GH {
20-
#[serde(flatten, skip_serializing)]
21-
keys: C4GHKeys,
22-
},
16+
keys: Option<C4GHKeys>,
17+
}
18+
19+
impl ObjectType {
20+
/// Get the C4GH keys.
21+
pub fn keys(&self) -> Option<&C4GHKeys> {
22+
self.keys.as_ref()
23+
}
2324
}

htsget-storage/src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use crate::s3::S3Storage;
3535
use crate::types::{BytesPositionOptions, DataBlock, GetOptions, HeadOptions, RangeUrlOptions};
3636
#[cfg(feature = "url-storage")]
3737
use crate::url::UrlStorage;
38-
use htsget_config::storage::object::ObjectType;
3938
use htsget_config::types::Scheme;
4039

4140
#[cfg(feature = "experimental")]
@@ -132,16 +131,14 @@ impl Storage {
132131
/// Create from local storage config.
133132
pub async fn from_local(config: &LocalStorageConfig) -> Result<Storage> {
134133
let storage = LocalStorage::new(config.local_path(), config.clone())?;
135-
match config.object_type() {
136-
ObjectType::Regular => Ok(Storage::new(storage)),
134+
135+
match config.object_type().keys() {
136+
None => Ok(Storage::new(storage)),
137137
#[cfg(feature = "experimental")]
138-
ObjectType::C4GH { keys } => Ok(Storage::new(C4GHStorage::new(
138+
Some(keys) => Ok(Storage::new(C4GHStorage::new(
139139
keys.clone().into_inner(),
140140
storage,
141141
))),
142-
_ => Err(StorageError::InternalError(
143-
"invalid object type".to_string(),
144-
)),
145142
}
146143
}
147144

0 commit comments

Comments
 (0)