Skip to content

Commit

Permalink
Merge pull request #296 from umccr/feat/crypt4gh-location
Browse files Browse the repository at this point in the history
feat(config): add ability to specify simple locations with keys
  • Loading branch information
mmalenic authored Jan 22, 2025
2 parents 9dde560 + e4192b9 commit c259f73
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions htsget-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/umccr/htsget-rs"

[features]
aws = ["dep:aws-sdk-secretsmanager", "dep:aws-config", "dep:tempfile"]
url = ["dep:reqwest", "dep:cfg-if"]
url = ["dep:reqwest"]
experimental = ["dep:crypt4gh", "dep:tokio", "dep:futures-util"]
default = []

Expand All @@ -25,6 +25,7 @@ serde_with = "3"
serde_json = "1"
serde_regex = "1"
regex = "1"
cfg-if = { version = "1" }
figment = { version = "0.10", features = ["env", "toml"] }
clap = { version = "4", features = ["derive", "env", "cargo"] }
tracing = "0.1"
Expand All @@ -39,7 +40,6 @@ chrono = { version = "0.4", features = ["now"], default-features = false }

# url
reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false, optional = true }
cfg-if = { version = "1", optional = true }

# Crypt4GH
crypt4gh = { version = "0.4", git = "https://github.com/EGA-archive/crypt4gh-rust", optional = true }
Expand Down
4 changes: 3 additions & 1 deletion htsget-config/src/config/advanced/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ impl TryFrom<Url> for storage::url::Url {

cfg_if! {
if #[cfg(feature = "experimental")] {
Ok(url_storage.set_keys(storage.keys))
let mut url_storage = url_storage;
url_storage.set_keys(storage.keys);
Ok(url_storage)
} else {
Ok(url_storage)
}
Expand Down
26 changes: 26 additions & 0 deletions htsget-config/src/config/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
use crate::config::advanced::regex_location::RegexLocation;
use crate::error::{Error::ParseError, Result};
use crate::storage;
#[cfg(feature = "experimental")]
use crate::storage::c4gh::C4GHKeys;
use crate::storage::file::default_authority;
use crate::storage::Backend;
use crate::types::Scheme;
use cfg_if::cfg_if;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use std::result;
Expand Down Expand Up @@ -130,6 +133,17 @@ impl From<LocationsOneOrMany> for Locations {
}
}

/// Deserialize into a string location that also supports setting additional fields
/// for the backend.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(default, deny_unknown_fields)]
struct ExtendedLocation {
location: StringLocation,
#[cfg(feature = "experimental")]
#[serde(skip_serializing)]
keys: Option<C4GHKeys>,
}

/// Deserialize the location from a string with a protocol.
#[derive(Serialize, Debug, Clone, Default)]
#[serde(default, deny_unknown_fields)]
Expand Down Expand Up @@ -159,13 +173,25 @@ struct MapLocation {
enum LocationWrapper {
String(StringLocation),
Map(MapLocation),
Extended(ExtendedLocation),
}

impl From<LocationWrapper> for Location {
fn from(location: LocationWrapper) -> Self {
match location {
LocationWrapper::String(location) => Location::new(location.backend, location.prefix),
LocationWrapper::Map(location) => Location::new(location.backend, location.prefix),
LocationWrapper::Extended(location) => {
cfg_if! {
if #[cfg(feature = "experimental")] {
let mut location = location;
location.location.backend.set_keys(location.keys);
Location::new(location.location.backend, location.location.prefix)
} else {
Location::new(location.location.backend, location.location.prefix)
}
}
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions htsget-config/src/storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ impl File {

#[cfg(feature = "experimental")]
/// Set the C4GH keys.
pub fn set_keys(mut self, keys: Option<C4GHKeys>) -> Self {
pub fn set_keys(&mut self, keys: Option<C4GHKeys>) {
self.keys = keys;
self
}

#[cfg(feature = "experimental")]
Expand Down
14 changes: 14 additions & 0 deletions htsget-config/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#[cfg(any(feature = "url", feature = "aws"))]
use crate::error::Error;
use crate::error::Result;
#[cfg(feature = "experimental")]
use crate::storage::c4gh::C4GHKeys;
use crate::storage::file::File;
#[cfg(feature = "aws")]
use crate::storage::s3::S3;
Expand Down Expand Up @@ -81,6 +83,18 @@ impl Backend {
Err(Error::ParseError("not a `File` variant".to_string()))
}
}

/// Set the C4GH keys.
#[cfg(feature = "experimental")]
pub fn set_keys(&mut self, keys: Option<C4GHKeys>) {
match self {
Backend::File(file) => file.set_keys(keys),
#[cfg(feature = "aws")]
Backend::S3(s3) => s3.set_keys(keys),
#[cfg(feature = "url")]
Backend::Url(url) => url.set_keys(keys),
}
}
}

impl Default for Backend {
Expand Down
3 changes: 1 addition & 2 deletions htsget-config/src/storage/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ impl S3 {

#[cfg(feature = "experimental")]
/// Set the C4GH keys.
pub fn with_keys(mut self, keys: Option<C4GHKeys>) -> Self {
pub fn set_keys(&mut self, keys: Option<C4GHKeys>) {
self.keys = keys;
self
}

#[cfg(feature = "experimental")]
Expand Down
3 changes: 1 addition & 2 deletions htsget-config/src/storage/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ impl Url {

#[cfg(feature = "experimental")]
/// Set the C4GH keys.
pub fn set_keys(mut self, keys: Option<C4GHKeys>) -> Self {
pub fn set_keys(&mut self, keys: Option<C4GHKeys>) {
self.keys = keys;
self
}

#[cfg(feature = "experimental")]
Expand Down

0 comments on commit c259f73

Please sign in to comment.