use crate::handle::Lunaris;
pub const RECALL_RERANK_ENV_VAR: &str = "LUNARIS_RECALL_RERANK";
pub const RECALL_RERANK_TOP_IN_ENV_VAR: &str = "LUNARIS_RECALL_RERANK_TOP_IN";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RecallRerankConfig {
pub enabled: bool,
pub top_in: Option<usize>,
}
impl RecallRerankConfig {
pub fn from_values(enabled_raw: Option<&str>, top_in_raw: Option<&str>) -> Self {
let enabled = matches!(enabled_raw, Some("1" | "true" | "TRUE" | "on" | "ON"));
let top_in = top_in_raw.and_then(|s| s.parse::<usize>().ok()).filter(|&n| n > 0);
Self { enabled, top_in }
}
pub fn from_env() -> Self {
Self::from_values(
std::env::var(RECALL_RERANK_ENV_VAR).ok().as_deref(),
std::env::var(RECALL_RERANK_TOP_IN_ENV_VAR).ok().as_deref(),
)
}
}
impl Lunaris {
pub fn recall_rerank(&self) -> RecallRerankConfig {
self.recall_rerank
}
pub fn with_recall_rerank(mut self, cfg: RecallRerankConfig) -> Self {
self.recall_rerank = cfg;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truthy_set_matches_graph_toggle() {
for on in ["1", "true", "TRUE", "on", "ON"] {
assert!(RecallRerankConfig::from_values(Some(on), None).enabled);
}
for off in [Some("0"), Some("off"), Some("yes"), Some(""), None] {
assert!(!RecallRerankConfig::from_values(off, None).enabled);
}
}
#[test]
fn top_in_parses_positive_integers_only() {
assert_eq!(RecallRerankConfig::from_values(None, Some("64")).top_in, Some(64));
assert_eq!(RecallRerankConfig::from_values(None, Some("0")).top_in, None);
assert_eq!(RecallRerankConfig::from_values(None, Some("-3")).top_in, None);
assert_eq!(RecallRerankConfig::from_values(None, Some("x")).top_in, None);
assert_eq!(RecallRerankConfig::from_values(None, None).top_in, None);
}
}