use std::time::Duration;
use uuid::Uuid;
pub const DEFAULT_TAP_ENDPOINT: &str = "https://simbad.cds.unistra.fr/simbad/sim-tap/sync";
pub const DEFAULT_USER_AGENT: &str = concat!(
"simbad-resolver/",
env!("CARGO_PKG_VERSION"),
" (+https://github.com/nightwatch-astro/simbad-resolver)"
);
#[derive(Clone, Debug)]
pub struct SimbadConfig {
pub endpoint: String,
pub timeout: Duration,
pub user_agent: String,
}
impl Default for SimbadConfig {
fn default() -> Self {
Self {
endpoint: DEFAULT_TAP_ENDPOINT.to_owned(),
timeout: Duration::from_secs(10),
user_agent: DEFAULT_USER_AGENT.to_owned(),
}
}
}
impl SimbadConfig {
#[must_use]
pub fn from_settings(endpoint: impl Into<String>, timeout_secs: u64) -> Self {
Self {
endpoint: endpoint.into(),
timeout: Duration::from_secs(timeout_secs.max(1)),
user_agent: DEFAULT_USER_AGENT.to_owned(),
}
}
}
#[derive(Clone, Debug)]
pub struct ResolverConfig {
pub online_enabled: bool,
pub namespace: Uuid,
pub fuzzy_min_score: Option<f32>,
}
impl ResolverConfig {
#[must_use]
pub fn new(namespace_seed: &str) -> Self {
Self {
online_enabled: true,
namespace: crate::identity::namespace(namespace_seed),
fuzzy_min_score: None,
}
}
#[must_use]
pub fn with_online(mut self, online_enabled: bool) -> Self {
self.online_enabled = online_enabled;
self
}
#[must_use]
pub fn with_namespace(mut self, namespace: Uuid) -> Self {
self.namespace = namespace;
self
}
#[must_use]
pub fn with_fuzzy(mut self, min_score: f32) -> Self {
self.fuzzy_min_score = Some(min_score.clamp(0.0, 1.0));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_uses_cds_tap_endpoint() {
let c = SimbadConfig::default();
assert_eq!(c.endpoint, DEFAULT_TAP_ENDPOINT);
assert_eq!(c.timeout, Duration::from_secs(10));
assert!(c.user_agent.starts_with("simbad-resolver/"));
}
#[test]
fn from_settings_clamps_zero_timeout_to_one_second() {
let c = SimbadConfig::from_settings("https://example/tap", 0);
assert_eq!(c.timeout, Duration::from_secs(1));
assert_eq!(c.endpoint, "https://example/tap");
}
#[test]
fn from_settings_preserves_larger_timeout() {
let c = SimbadConfig::from_settings("https://example/tap", 30);
assert_eq!(c.timeout, Duration::from_secs(30));
}
#[test]
fn new_enables_online_and_derives_a_stable_namespace() {
let a = ResolverConfig::new("example.targets");
let b = ResolverConfig::new("example.targets");
let c = ResolverConfig::new("other.targets");
assert!(a.online_enabled);
assert_eq!(a.namespace, b.namespace, "same seed derives the same namespace");
assert_ne!(a.namespace, c.namespace, "a different seed derives a different namespace");
}
#[test]
fn with_online_toggles_the_flag() {
assert!(!ResolverConfig::new("x").with_online(false).online_enabled);
assert!(ResolverConfig::new("x").with_online(false).with_online(true).online_enabled);
}
#[test]
fn with_namespace_overrides_the_derived_namespace() {
let explicit = Uuid::from_u128(0x1234_5678_90ab_cdef_1234_5678_90ab_cdef);
assert_eq!(ResolverConfig::new("x").with_namespace(explicit).namespace, explicit);
}
#[test]
fn fuzzy_is_disabled_by_default() {
assert_eq!(ResolverConfig::new("x").fuzzy_min_score, None);
}
#[test]
fn with_fuzzy_sets_and_clamps_the_threshold() {
assert_eq!(ResolverConfig::new("x").with_fuzzy(0.5).fuzzy_min_score, Some(0.5));
assert_eq!(ResolverConfig::new("x").with_fuzzy(2.0).fuzzy_min_score, Some(1.0));
assert_eq!(ResolverConfig::new("x").with_fuzzy(-1.0).fuzzy_min_score, Some(0.0));
}
}