refactor: adapters configured from config crate
This commit is contained in:
@@ -14,13 +14,13 @@ pub struct NatsConfig {
|
||||
}
|
||||
|
||||
impl NatsConfig {
|
||||
pub fn from_env() -> anyhow::Result<Self> {
|
||||
pub fn from_config(config: &::config::NatsConfig) -> anyhow::Result<Self> {
|
||||
Self::from_vars(
|
||||
std::env::var("NATS_URL").ok().as_deref(),
|
||||
std::env::var("NATS_MODE").ok().as_deref(),
|
||||
std::env::var("NATS_SUBJECT_PREFIX").ok().as_deref(),
|
||||
std::env::var("NATS_STREAM_NAME").ok().as_deref(),
|
||||
std::env::var("NATS_CONSUMER_NAME").ok().as_deref(),
|
||||
config.url.as_deref(),
|
||||
Some(&config.mode),
|
||||
Some(&config.subject_prefix),
|
||||
Some(&config.stream_name),
|
||||
Some(&config.consumer_name),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,3 +28,20 @@ fn invalid_mode_errors() {
|
||||
NatsConfig::from_vars(Some("nats://test:4222"), Some("kafka"), None, None, None).is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_config_requires_url() {
|
||||
assert!(NatsConfig::from_config(&::config::NatsConfig::default()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_config_uses_section_values() {
|
||||
let section = ::config::NatsConfig {
|
||||
url: Some("nats://test:4222".into()),
|
||||
mode: "core".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let cfg = NatsConfig::from_config(§ion).unwrap();
|
||||
assert_eq!(cfg.url, "nats://test:4222");
|
||||
assert_eq!(cfg.mode, NatsMode::Core);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user