1use std::path::PathBuf;
10
11#[cfg(test)]
12use std::collections::HashMap;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct Config {
17 pub model: PathBuf,
19 pub mmproj: PathBuf,
21 pub binary: Option<PathBuf>,
23 pub idle_secs: u64,
25 pub max_tokens: u32,
27 pub auto_download: bool,
29}
30
31fn expand_tilde(raw: &str, home: Option<String>) -> PathBuf {
33 match (raw.strip_prefix("~/"), home) {
34 (Some(rest), Some(h)) => PathBuf::from(h).join(rest),
35 _ => PathBuf::from(raw),
36 }
37}
38
39impl Config {
40 pub fn from_env(get: &dyn Fn(&str) -> Option<String>) -> Self {
45 let home = get("HOME");
46 let dir = get("OCR_MCP_MODEL_DIR").map_or_else(
47 || expand_tilde("~/.cache/ocr-mcp", home.clone()),
48 |d| expand_tilde(&d, home.clone()),
49 );
50 let path_or = |key: &str, default: &str| -> PathBuf {
51 get(key).map_or_else(|| dir.join(default), |v| expand_tilde(&v, home.clone()))
52 };
53 Self {
54 model: path_or("OCR_MCP_MODEL", "glm-ocr.gguf"),
55 mmproj: path_or("OCR_MCP_MMPROJ", "glm-ocr-mmproj.gguf"),
56 binary: get("OCR_MCP_BINARY").map(|b| expand_tilde(&b, home.clone())),
57 idle_secs: get("OCR_MCP_IDLE_SECS")
58 .and_then(|v| v.parse().ok())
59 .unwrap_or(300),
60 max_tokens: get("OCR_MCP_MAX_TOKENS")
61 .and_then(|v| v.parse().ok())
62 .unwrap_or(1024),
63 auto_download: get("OCR_MCP_AUTO_DOWNLOAD").is_some_and(|v| !v.is_empty()),
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 fn env_of(pairs: &[(&str, &str)]) -> HashMap<String, String> {
73 pairs
74 .iter()
75 .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
76 .collect()
77 }
78
79 #[test]
80 fn defaults_derive_from_model_dir() {
81 let env = env_of(&[("OCR_MCP_MODEL_DIR", "/tmp/w")]);
82 let cfg = Config::from_env(&|k| env.get(k).cloned());
83 assert_eq!(cfg.model, PathBuf::from("/tmp/w/glm-ocr.gguf"));
84 assert_eq!(cfg.mmproj, PathBuf::from("/tmp/w/glm-ocr-mmproj.gguf"));
85 assert_eq!(cfg.binary, None);
86 assert_eq!(cfg.idle_secs, 300);
87 assert_eq!(cfg.max_tokens, 1024);
88 assert!(!cfg.auto_download);
89 }
90
91 #[test]
92 fn explicit_paths_win_over_model_dir() {
93 let env = env_of(&[
94 ("OCR_MCP_MODEL_DIR", "/tmp/w"),
95 ("OCR_MCP_MODEL", "/other/m.gguf"),
96 ("OCR_MCP_MMPROJ", "/other/p.gguf"),
97 ("OCR_MCP_BINARY", "/usr/local/bin/llama-server"),
98 ("OCR_MCP_IDLE_SECS", "60"),
99 ("OCR_MCP_MAX_TOKENS", "256"),
100 ("OCR_MCP_AUTO_DOWNLOAD", "1"),
101 ]);
102 let cfg = Config::from_env(&|k| env.get(k).cloned());
103 assert_eq!(cfg.model, PathBuf::from("/other/m.gguf"));
104 assert_eq!(cfg.mmproj, PathBuf::from("/other/p.gguf"));
105 assert_eq!(
106 cfg.binary,
107 Some(PathBuf::from("/usr/local/bin/llama-server"))
108 );
109 assert_eq!(cfg.idle_secs, 60);
110 assert_eq!(cfg.max_tokens, 256);
111 assert!(cfg.auto_download);
112 }
113
114 #[test]
117 fn unparsable_numbers_fall_back_to_defaults() {
118 let env = env_of(&[("OCR_MCP_IDLE_SECS", "soon"), ("OCR_MCP_MAX_TOKENS", "")]);
119 let cfg = Config::from_env(&|k| env.get(k).cloned());
120 assert_eq!(cfg.idle_secs, 300);
121 assert_eq!(cfg.max_tokens, 1024);
122 }
123}