refactor: adapters configured from config crate

This commit is contained in:
2026-10-25 21:07:19 +01:00
parent 1f724bb064
commit 04f9c13625
45 changed files with 277 additions and 177 deletions

View File

@@ -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),
)
}

View File

@@ -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(&section).unwrap();
assert_eq!(cfg.url, "nats://test:4222");
assert_eq!(cfg.mode, NatsMode::Core);
}