use std::path::{Component, Path, PathBuf};
use crate::domain::{self, Error, limits};
const DEFAULT_TOML: &str = include_str!("../config/default.toml");
const MAX_CONFIG_BYTES: u64 = limits::MAX_CONFIG_FILE_BYTES as u64;
const SUMMARY_METHODS: [&str; 2] = ["tfidf", "textrank"];
const KEYPHRASE_METHODS: [&str; 2] = ["rake", "yake"];
const RENAMED_KEYS: [(&str, &str); 6] = [
("summarize", "`[summarize]` is now `[summary]`"),
(
"summarize.algorithm",
"`summarize.algorithm` is now `summary.method`",
),
(
"summary.algorithm",
"`summary.algorithm` is now `summary.method`",
),
(
"keyphrases.algorithm",
"`keyphrases.algorithm` is now `keyphrases.method`",
),
(
"semantic",
"`[semantic]` is now `[clusters]`, and `semantic.threshold` is `clusters.threshold`",
),
(
"models.udpipe",
"`models.udpipe` is gone: the UDPipe model is pinned in matra, so delete the line",
),
];
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValueSource {
Argument,
Environment(String),
File(PathBuf),
Default,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Config {
data_dir: PathBuf,
model_dir: PathBuf,
embedding_model: String,
summary_n: usize,
summary_method: String,
keyphrases_n: usize,
keyphrases_method: String,
clusters_threshold: f64,
sources: Vec<(&'static str, ValueSource)>,
config_file: Option<PathBuf>,
}
impl Config {
pub fn resolve() -> domain::Result<Config> {
let env = |key: &str| std::env::var(key).ok();
let contents = match config_file_path_from(&env) {
Some(path) => read_config_file(&path)?,
None => None,
};
Config::from_sources(env, contents.as_deref())
}
pub fn from_file(path: impl AsRef<Path>) -> domain::Result<Config> {
let path = path.as_ref();
let contents = match read_config_file(path)? {
Some(contents) => contents,
None => {
let missing = std::fs::metadata(path)
.err()
.unwrap_or_else(|| std::io::Error::from(std::io::ErrorKind::NotFound));
return Err(Error::Io(domain::IoContext::cannot("read", path, &missing)));
}
};
let named = path.as_os_str().to_string_lossy().into_owned();
let env = |key: &str| {
if key == "MATRA_CONFIG_FILE" {
Some(named.clone())
} else {
std::env::var(key).ok()
}
};
let mut cfg = Config::from_sources(env, Some(&contents))?;
cfg.config_file = Some(path.to_path_buf());
Ok(cfg)
}
pub fn from_sources(
env: impl Fn(&str) -> Option<String>,
file: Option<&str>,
) -> domain::Result<Config> {
let env: &dyn Fn(&str) -> Option<String> = &env;
let config_file = config_file_path_from(env);
let file_path = config_file
.clone()
.unwrap_or_else(|| PathBuf::from("config.toml"));
let from_file = match file {
Some(text) => parse_toml(text, &file_path)?,
None => FileConfig::default(),
};
let from_default = parse_toml(DEFAULT_TOML, Path::new("config/default.toml"))?;
let mut sources: Vec<(&'static str, ValueSource)> = Vec::new();
let (data_dir, data_dir_source) = resolve_data_dir(env)?;
sources.push(("data_dir", data_dir_source));
let (model_dir, model_dir_source) = resolve_model_dir(env, &data_dir);
sources.push(("model_dir", model_dir_source));
let models_file = from_file.models.unwrap_or_default();
let models_default = from_default.models.unwrap_or_default();
let summary_file = from_file.summary.unwrap_or_default();
let summary_default = from_default.summary.unwrap_or_default();
let keyphrases_file = from_file.keyphrases.unwrap_or_default();
let keyphrases_default = from_default.keyphrases.unwrap_or_default();
let clusters_file = from_file.clusters.unwrap_or_default();
let clusters_default = from_default.clusters.unwrap_or_default();
let embedding_model = choose(
"models.embedding",
models_file.embedding,
models_default.embedding,
&file_path,
&mut sources,
)?;
let summary_n = choose(
"summary.n",
summary_file.n,
summary_default.n,
&file_path,
&mut sources,
)?;
let summary_method = choose(
"summary.method",
summary_file.method,
summary_default.method,
&file_path,
&mut sources,
)?;
let keyphrases_n = choose(
"keyphrases.n",
keyphrases_file.n,
keyphrases_default.n,
&file_path,
&mut sources,
)?;
let keyphrases_method = choose(
"keyphrases.method",
keyphrases_file.method,
keyphrases_default.method,
&file_path,
&mut sources,
)?;
let clusters_threshold = choose(
"clusters.threshold",
clusters_file.threshold,
clusters_default.threshold,
&file_path,
&mut sources,
)?;
check_method(
"summary.method",
&summary_method,
&SUMMARY_METHODS,
&sources,
&file_path,
)?;
check_method(
"keyphrases.method",
&keyphrases_method,
&KEYPHRASE_METHODS,
&sources,
&file_path,
)?;
if !clusters_threshold.is_finite() {
return Err(Error::InvalidInput {
reason: "config_threshold_not_finite",
message: format!(
"{}: clusters.threshold must be finite, got {clusters_threshold}",
origin_of("clusters.threshold", &sources, &file_path),
),
});
}
check_path_component("models.embedding", &embedding_model, &sources, &file_path)?;
Ok(Config {
data_dir,
model_dir,
embedding_model,
summary_n,
summary_method,
keyphrases_n,
keyphrases_method,
clusters_threshold,
sources,
config_file,
})
}
#[must_use]
pub fn with_model_dir(mut self, dir: impl AsRef<Path>) -> Config {
self.model_dir = dir.as_ref().to_path_buf();
for (key, source) in &mut self.sources {
if *key == "model_dir" {
*source = ValueSource::Argument;
}
}
self
}
pub fn config_file_path() -> Option<PathBuf> {
config_file_path_from(&|key: &str| std::env::var(key).ok())
}
pub fn config_file(&self) -> Option<&Path> {
self.config_file.as_deref()
}
pub fn data_dir(&self) -> &Path {
&self.data_dir
}
pub fn model_dir(&self) -> &Path {
&self.model_dir
}
pub fn embedding_model(&self) -> &str {
&self.embedding_model
}
pub fn summary_n(&self) -> usize {
self.summary_n
}
pub fn summary_method(&self) -> &str {
&self.summary_method
}
pub fn keyphrases_n(&self) -> usize {
self.keyphrases_n
}
pub fn keyphrases_method(&self) -> &str {
&self.keyphrases_method
}
pub fn clusters_threshold(&self) -> f64 {
self.clusters_threshold
}
pub fn sources(&self) -> impl Iterator<Item = (&'static str, ValueSource)> {
self.sources
.iter()
.map(|(key, source)| (*key, source.clone()))
}
}
#[derive(Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct FileConfig {
models: Option<ModelsSection>,
summary: Option<CountAndMethod>,
keyphrases: Option<CountAndMethod>,
clusters: Option<ClustersSection>,
}
#[derive(Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct ModelsSection {
embedding: Option<String>,
}
#[derive(Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct CountAndMethod {
n: Option<usize>,
method: Option<String>,
}
#[derive(Debug, Default, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct ClustersSection {
threshold: Option<f64>,
}
fn parse_toml(text: &str, path: &Path) -> domain::Result<FileConfig> {
let invalid = |e: toml::de::Error| Error::InvalidInput {
reason: "config_malformed",
message: format!("{}: {}", path.display(), e.to_string().trim_end()),
};
let table: toml::Table = toml::from_str(text).map_err(invalid)?;
let renamed: Vec<&str> = RENAMED_KEYS
.iter()
.filter(|(key, _)| sets(&table, key))
.map(|(_, replaced_by)| *replaced_by)
.collect();
if !renamed.is_empty() {
return Err(Error::InvalidInput {
reason: "config_key_renamed",
message: format!(
"{}: matra 0.3.0 renamed or removed {} this file sets: {}",
path.display(),
if renamed.len() == 1 { "a key" } else { "keys" },
renamed.join("; "),
),
});
}
toml::from_str(text).map_err(invalid)
}
fn sets(table: &toml::Table, key: &str) -> bool {
let mut parts = key.split('.');
let Some(first) = parts.next().and_then(|part| table.get(part)) else {
return false;
};
parts
.try_fold(first, |value, part| value.as_table()?.get(part))
.is_some()
}
fn choose<T>(
key: &'static str,
from_file: Option<T>,
from_default: Option<T>,
file_path: &Path,
sources: &mut Vec<(&'static str, ValueSource)>,
) -> domain::Result<T> {
if let Some(value) = from_file {
sources.push((key, ValueSource::File(file_path.to_path_buf())));
return Ok(value);
}
if let Some(value) = from_default {
sources.push((key, ValueSource::Default));
return Ok(value);
}
Err(Error::InvalidInput {
reason: "config_default_missing",
message: format!("built-in defaults (config/default.toml) are missing `{key}`"),
})
}
fn origin_of(key: &str, sources: &[(&'static str, ValueSource)], file_path: &Path) -> String {
match sources.iter().find(|(k, _)| *k == key) {
Some((_, ValueSource::File(path))) => path.display().to_string(),
_ => format!("built-in defaults ({})", file_path.display()),
}
}
fn check_method(
key: &'static str,
value: &str,
known: &[&str],
sources: &[(&'static str, ValueSource)],
file_path: &Path,
) -> domain::Result<()> {
if known.contains(&value) {
return Ok(());
}
Err(Error::InvalidInput {
reason: "config_method_unknown",
message: format!(
"{}: {key} is `{value}`, which is not one of {}",
origin_of(key, sources, file_path),
known.join(", "),
),
})
}
fn check_path_component(
key: &'static str,
value: &str,
sources: &[(&'static str, ValueSource)],
file_path: &Path,
) -> domain::Result<()> {
let mut components = Path::new(value).components();
let single =
matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none();
if single && !value.contains('\\') {
return Ok(());
}
Err(Error::InvalidInput {
reason: "config_not_path_component",
message: format!(
"{}: {key} is `{value}`, which is not a single path component. It names a directory \
inside the model directory, so it cannot be empty, absolute, or contain `/`, `\\`, \
`.` or `..`",
origin_of(key, sources, file_path),
),
})
}
fn config_file_path_from(env: &dyn Fn(&str) -> Option<String>) -> Option<PathBuf> {
if let Some(path) = non_empty(env("MATRA_CONFIG_FILE")) {
return Some(PathBuf::from(path));
}
if let Some(dir) = non_empty(env("XDG_CONFIG_HOME")) {
return Some(PathBuf::from(dir).join("matra").join("config.toml"));
}
let home = non_empty(env("HOME"))?;
Some(
PathBuf::from(home)
.join(".config")
.join("matra")
.join("config.toml"),
)
}
fn resolve_data_dir(
env: &dyn Fn(&str) -> Option<String>,
) -> domain::Result<(PathBuf, ValueSource)> {
if let Some(dir) = non_empty(env("MATRA_DATA_DIR")) {
return Ok((
PathBuf::from(dir),
ValueSource::Environment("MATRA_DATA_DIR".to_string()),
));
}
if let Some(dir) = non_empty(env("XDG_DATA_HOME")) {
return Ok((
PathBuf::from(dir).join("matra"),
ValueSource::Environment("XDG_DATA_HOME".to_string()),
));
}
if let Some(home) = non_empty(env("HOME")) {
return Ok((
PathBuf::from(home)
.join(".local")
.join("share")
.join("matra"),
ValueSource::Default,
));
}
Err(Error::InvalidInput {
reason: "no_data_directory",
message: "cannot locate the data directory: set MATRA_DATA_DIR, XDG_DATA_HOME, or HOME"
.to_string(),
})
}
fn resolve_model_dir(
env: &dyn Fn(&str) -> Option<String>,
data_dir: &Path,
) -> (PathBuf, ValueSource) {
if let Some(dir) = non_empty(env("MATRA_MODEL_DIR")) {
return (
PathBuf::from(dir),
ValueSource::Environment("MATRA_MODEL_DIR".to_string()),
);
}
let current = data_dir.join("models");
if !current.exists()
&& let Some(home) = non_empty(env("HOME"))
{
let legacy = PathBuf::from(home).join(".matra").join("models");
if holds_something(&legacy) {
return (legacy, ValueSource::Default);
}
}
(current, ValueSource::Default)
}
fn holds_something(dir: &Path) -> bool {
std::fs::read_dir(dir).is_ok_and(|mut entries| entries.next().is_some())
}
fn non_empty(value: Option<String>) -> Option<String> {
value.filter(|v| !v.is_empty())
}
fn read_config_file(path: &Path) -> domain::Result<Option<String>> {
let metadata = match std::fs::metadata(path) {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(Error::Io(domain::IoContext::cannot("read", path, &e))),
};
if metadata.len() > MAX_CONFIG_BYTES {
return Err(Error::InputTooLarge {
limit: limits::MAX_CONFIG_FILE_BYTES,
actual: metadata.len() as usize,
what: "config_file_read",
});
}
match std::fs::read_to_string(path) {
Ok(text) => Ok(Some(text)),
Err(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(Error::InvalidInput {
reason: "config_not_utf8",
message: format!("{}: not valid UTF-8", path.display()),
}),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Error::Io(domain::IoContext::cannot("read", path, &e))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn env_of(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> + use<> {
let map: HashMap<String, String> = pairs
.iter()
.map(|(k, v)| ((*k).to_string(), (*v).to_string()))
.collect();
move |key: &str| map.get(key).cloned()
}
fn source_of(cfg: &Config, key: &str) -> ValueSource {
cfg.sources()
.find(|(k, _)| *k == key)
.map(|(_, s)| s)
.unwrap_or_else(|| panic!("no source recorded for {key}"))
}
#[test]
fn embedded_defaults_parse_and_carry_the_documented_values() {
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), None).unwrap();
assert_eq!(cfg.embedding_model(), "potion-base-8M");
assert_eq!(cfg.summary_n(), 3);
assert_eq!(cfg.summary_method(), "tfidf");
assert_eq!(cfg.keyphrases_n(), 10);
assert_eq!(cfg.keyphrases_method(), "rake");
assert_eq!(cfg.clusters_threshold(), 0.85);
}
#[test]
fn every_key_reports_the_default_rung_when_nothing_overrides() {
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), None).unwrap();
let keys: Vec<&'static str> = cfg.sources().map(|(k, _)| k).collect();
assert_eq!(
keys,
vec![
"data_dir",
"model_dir",
"models.embedding",
"summary.n",
"summary.method",
"keyphrases.n",
"keyphrases.method",
"clusters.threshold",
]
);
for (key, source) in cfg.sources() {
assert_eq!(source, ValueSource::Default, "{key} did not default");
}
}
#[test]
fn default_paths_follow_xdg_when_only_home_is_set() {
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), None).unwrap();
assert_eq!(cfg.data_dir(), Path::new("/home/tester/.local/share/matra"));
assert_eq!(
cfg.model_dir(),
Path::new("/home/tester/.local/share/matra/models")
);
}
#[test]
fn file_beats_the_built_in_default() {
let file = r#"
[keyphrases]
n = 5
[summary]
method = "textrank"
[clusters]
threshold = 0.7
"#;
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), Some(file)).unwrap();
assert_eq!(cfg.keyphrases_n(), 5);
assert_eq!(cfg.summary_method(), "textrank");
assert_eq!(cfg.clusters_threshold(), 0.7);
assert_eq!(cfg.summary_n(), 3);
assert_eq!(cfg.keyphrases_method(), "rake");
}
#[test]
fn sources_name_the_file_for_keys_the_file_set() {
let file = "[keyphrases]\nn = 5\n";
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), Some(file)).unwrap();
assert_eq!(
source_of(&cfg, "keyphrases.n"),
ValueSource::File(PathBuf::from("/home/tester/.config/matra/config.toml")),
);
assert_eq!(source_of(&cfg, "summary.n"), ValueSource::Default);
}
const CONFIG_FROM_0_2: &str = r#"[models]
# Model directory defaults to $XDG_DATA_HOME/matra/models (~/.local/share/matra/models).
# Override with MATRA_MODEL_DIR, or MATRA_DATA_DIR for the whole data root.
udpipe = "english-ewt-ud-2.5-191206"
embedding = "potion-base-8M"
[semantic]
threshold = 0.85
[summarize]
n = 3
algorithm = "tfidf"
[keyphrases]
n = 10
algorithm = "rake"
"#;
#[test]
fn a_config_file_from_0_2_fails_naming_every_replacement() {
let err = Config::from_sources(env_of(&[("HOME", "/home/tester")]), Some(CONFIG_FROM_0_2))
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
for named in [
"/home/tester/.config/matra/config.toml",
"`[summary]`",
"`summary.method`",
"`keyphrases.method`",
"`clusters.threshold`",
"`models.udpipe` is gone",
] {
assert!(message.contains(named), "{named} not in: {message}");
}
}
#[test]
fn each_old_key_alone_names_its_replacement() {
for (file, replacement) in [
("[summarize]\nn = 3\n", "`[summary]`"),
("[summary]\nalgorithm = \"tfidf\"\n", "`summary.method`"),
(
"[keyphrases]\nalgorithm = \"rake\"\n",
"`keyphrases.method`",
),
("[semantic]\nthreshold = 0.8\n", "`clusters.threshold`"),
("[models]\nudpipe = \"x\"\n", "delete the line"),
] {
let err =
Config::from_sources(env_of(&[("HOME", "/home/tester")]), Some(file)).unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput for {file:?}, got {err:?}");
};
assert!(message.contains(replacement), "{file:?}: {message}");
}
}
#[test]
fn the_shipped_defaults_set_every_key_and_no_old_one() {
let shipped = parse_toml(DEFAULT_TOML, Path::new("config/default.toml")).unwrap();
assert!(
shipped
.summary
.is_some_and(|s| s.n.is_some() && s.method.is_some())
);
assert!(
shipped
.keyphrases
.is_some_and(|s| s.n.is_some() && s.method.is_some())
);
assert!(shipped.clusters.is_some_and(|s| s.threshold.is_some()));
assert!(shipped.models.is_some_and(|s| s.embedding.is_some()));
}
#[test]
fn matra_data_dir_beats_xdg_and_home() {
let cfg = Config::from_sources(
env_of(&[
("MATRA_DATA_DIR", "/srv/matra"),
("XDG_DATA_HOME", "/home/tester/.local/share"),
("HOME", "/home/tester"),
]),
None,
)
.unwrap();
assert_eq!(cfg.data_dir(), Path::new("/srv/matra"));
assert_eq!(
source_of(&cfg, "data_dir"),
ValueSource::Environment("MATRA_DATA_DIR".to_string())
);
}
#[test]
fn xdg_data_home_beats_home() {
let cfg = Config::from_sources(
env_of(&[
("XDG_DATA_HOME", "/elsewhere/share"),
("HOME", "/home/tester"),
]),
None,
)
.unwrap();
assert_eq!(cfg.data_dir(), Path::new("/elsewhere/share/matra"));
assert_eq!(
source_of(&cfg, "data_dir"),
ValueSource::Environment("XDG_DATA_HOME".to_string())
);
}
#[test]
fn matra_model_dir_beats_the_derived_model_directory() {
let cfg = Config::from_sources(
env_of(&[("MATRA_MODEL_DIR", "/models"), ("HOME", "/home/tester")]),
None,
)
.unwrap();
assert_eq!(cfg.model_dir(), Path::new("/models"));
assert_eq!(
source_of(&cfg, "model_dir"),
ValueSource::Environment("MATRA_MODEL_DIR".to_string())
);
assert_eq!(cfg.data_dir(), Path::new("/home/tester/.local/share/matra"));
}
#[test]
fn an_environment_variable_set_to_empty_is_treated_as_unset() {
let cfg = Config::from_sources(
env_of(&[("MATRA_DATA_DIR", ""), ("HOME", "/home/tester")]),
None,
)
.unwrap();
assert_eq!(cfg.data_dir(), Path::new("/home/tester/.local/share/matra"));
}
#[test]
fn an_environment_with_no_home_at_all_is_an_error() {
let err = Config::from_sources(env_of(&[]), None).unwrap_err();
assert!(
matches!(err, Error::InvalidInput { message: ref m, .. } if m.contains("MATRA_DATA_DIR")),
"unexpected error: {err}"
);
}
fn legacy_cache_in(home: &Path) -> PathBuf {
let legacy = home.join(".matra").join("models");
std::fs::create_dir_all(&legacy).unwrap();
std::fs::write(legacy.join("english-ewt.udpipe"), b"not a real model").unwrap();
legacy
}
#[test]
fn model_dir_falls_back_to_a_non_empty_dot_matra_when_only_that_exists() {
let home = tempfile::tempdir().unwrap();
let legacy = legacy_cache_in(home.path());
let cfg =
Config::from_sources(env_of(&[("HOME", home.path().to_str().unwrap())]), None).unwrap();
assert_eq!(cfg.model_dir(), legacy);
}
#[test]
fn model_dir_ignores_an_empty_dot_matra_directory() {
let home = tempfile::tempdir().unwrap();
std::fs::create_dir_all(home.path().join(".matra").join("models")).unwrap();
let cfg =
Config::from_sources(env_of(&[("HOME", home.path().to_str().unwrap())]), None).unwrap();
assert_eq!(
cfg.model_dir(),
home.path()
.join(".local")
.join("share")
.join("matra")
.join("models")
);
}
#[test]
fn model_dir_prefers_the_data_directory_when_both_exist() {
let home = tempfile::tempdir().unwrap();
legacy_cache_in(home.path());
let current = home
.path()
.join(".local")
.join("share")
.join("matra")
.join("models");
std::fs::create_dir_all(¤t).unwrap();
let cfg =
Config::from_sources(env_of(&[("HOME", home.path().to_str().unwrap())]), None).unwrap();
assert_eq!(cfg.model_dir(), current);
}
#[test]
fn model_dir_ignores_dot_matra_when_the_environment_names_a_directory() {
let home = tempfile::tempdir().unwrap();
legacy_cache_in(home.path());
let cfg = Config::from_sources(
env_of(&[
("HOME", home.path().to_str().unwrap()),
("MATRA_MODEL_DIR", "/models"),
]),
None,
)
.unwrap();
assert_eq!(cfg.model_dir(), Path::new("/models"));
}
#[test]
fn with_model_dir_puts_the_model_directory_on_the_argument_rung() {
let cfg = Config::from_sources(env_of(&[("HOME", "/home/tester")]), None)
.unwrap()
.with_model_dir("/opt/models");
assert_eq!(cfg.model_dir(), Path::new("/opt/models"));
assert_eq!(source_of(&cfg, "model_dir"), ValueSource::Argument);
}
#[test]
fn with_model_dir_beats_the_environment_rung() {
let cfg = Config::from_sources(
env_of(&[("MATRA_MODEL_DIR", "/models"), ("HOME", "/home/tester")]),
None,
)
.unwrap()
.with_model_dir("/opt/models");
assert_eq!(cfg.model_dir(), Path::new("/opt/models"));
assert_eq!(source_of(&cfg, "model_dir"), ValueSource::Argument);
}
#[test]
fn with_model_dir_leaves_every_other_key_and_its_source_alone() {
let file = "[keyphrases]\nn = 5\n";
let before = Config::from_sources(env_of(&[("HOME", "/home/tester")]), Some(file)).unwrap();
let before_sources: Vec<(&'static str, ValueSource)> = before.sources().collect();
let after = before.clone().with_model_dir("/opt/models");
assert_eq!(
after.data_dir(),
Path::new("/home/tester/.local/share/matra")
);
assert_eq!(after.keyphrases_n(), 5);
assert_eq!(after.summary_method(), "tfidf");
let after_sources: Vec<(&'static str, ValueSource)> = after.sources().collect();
assert_eq!(after_sources.len(), before_sources.len());
for ((key, before), (after_key, after)) in before_sources.iter().zip(&after_sources) {
assert_eq!(key, after_key, "key order changed");
if *key == "model_dir" {
assert_eq!(*after, ValueSource::Argument);
} else {
assert_eq!(before, after, "{key} changed rung");
}
}
}
#[test]
fn config_file_path_prefers_matra_config_file() {
let path = config_file_path_from(&env_of(&[
("MATRA_CONFIG_FILE", "/etc/matra.toml"),
("XDG_CONFIG_HOME", "/home/tester/.config"),
("HOME", "/home/tester"),
]));
assert_eq!(path, Some(PathBuf::from("/etc/matra.toml")));
}
#[test]
fn config_file_path_then_xdg_config_home_then_home() {
let xdg = config_file_path_from(&env_of(&[
("XDG_CONFIG_HOME", "/elsewhere/config"),
("HOME", "/home/tester"),
]));
assert_eq!(
xdg,
Some(PathBuf::from("/elsewhere/config/matra/config.toml"))
);
let home = config_file_path_from(&env_of(&[("HOME", "/home/tester")]));
assert_eq!(
home,
Some(PathBuf::from("/home/tester/.config/matra/config.toml"))
);
assert_eq!(config_file_path_from(&env_of(&[])), None);
}
#[test]
fn malformed_toml_is_invalid_input_naming_the_file_and_the_line() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[clusters\nthreshold = 0.5\n"),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
assert!(
message.contains("/home/tester/.config/matra/config.toml"),
"message does not name the file: {message}"
);
assert!(
message.contains("line 1"),
"message does not name the line: {message}"
);
}
#[test]
fn a_value_of_the_wrong_type_is_invalid_input_naming_the_key() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[clusters]\nthreshold = \"high\"\n"),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
assert!(
message.contains("/home/tester/.config/matra/config.toml")
&& message.contains("line 2"),
"message does not locate the fault: {message}"
);
}
#[test]
fn an_unknown_key_is_rejected_rather_than_ignored() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[clusters]\ntreshold = 0.5\n"),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
assert!(
message.contains("treshold"),
"message does not name the unknown key: {message}"
);
}
#[test]
fn an_unknown_summary_method_is_rejected_at_resolve_time() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[summary]\nmethod = \"lexrank\"\n"),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
assert!(
message.contains("summary.method")
&& message.contains("lexrank")
&& message.contains("textrank")
&& message.contains("/home/tester/.config/matra/config.toml"),
"message is not actionable: {message}"
);
}
#[test]
fn an_unknown_keyphrase_method_is_rejected_at_resolve_time() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[keyphrases]\nmethod = \"textrank\"\n"),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput, got {err:?}");
};
assert!(
message.contains("keyphrases.method") && message.contains("rake, yake"),
"message is not actionable: {message}"
);
}
#[test]
fn an_embedding_name_that_is_not_a_path_component_is_rejected() {
for value in ["../..", "..", ".", "", "/etc", "a/b", "a\\b"] {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some(&format!("[models]\nembedding = '{value}'\n")),
)
.unwrap_err();
let Error::InvalidInput { message, .. } = err else {
panic!("expected InvalidInput for {value:?}, got {err:?}");
};
assert!(
message.contains("models.embedding")
&& message.contains("/home/tester/.config/matra/config.toml"),
"message names neither the key nor the file for {value:?}: {message}"
);
}
}
#[test]
fn an_ordinary_embedding_name_resolves() {
for value in ["potion-base-8M", "potion.base.8M", "my_model", "..leading"] {
let cfg = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some(&format!("[models]\nembedding = '{value}'\n")),
)
.unwrap_or_else(|e| panic!("{value:?} should resolve, got {e:?}"));
assert_eq!(cfg.embedding_model(), value);
}
}
#[test]
fn a_non_finite_threshold_is_rejected() {
let err = Config::from_sources(
env_of(&[("HOME", "/home/tester")]),
Some("[clusters]\nthreshold = nan\n"),
)
.unwrap_err();
assert!(
matches!(err, Error::InvalidInput { message: ref m, .. } if m.contains("clusters.threshold")),
"unexpected error: {err}"
);
}
#[test]
fn a_missing_config_file_is_not_an_error() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("nothing").join("config.toml");
assert_eq!(read_config_file(&missing).unwrap(), None);
}
#[test]
fn an_oversized_config_file_is_rejected_before_it_is_read() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(&path, vec![b'#'; (MAX_CONFIG_BYTES + 1) as usize]).unwrap();
let err = read_config_file(&path).unwrap_err();
assert!(
matches!(
err,
Error::InputTooLarge {
what: "config_file_read",
..
}
),
"unexpected error: {err}"
);
}
#[test]
fn a_named_config_file_is_the_one_read_and_named() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("matra.toml");
std::fs::write(&path, "[summary]\nn = 7\n").unwrap();
let cfg = Config::from_file(&path).unwrap();
assert_eq!(cfg.summary_n(), 7);
assert_eq!(cfg.config_file(), Some(path.as_path()));
assert!(
cfg.sources().any(
|(key, source)| key == "summary.n" && source == ValueSource::File(path.clone())
),
"summary.n is attributed to the named file"
);
}
#[test]
fn a_named_config_file_that_is_missing_is_an_error_naming_it() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("absent.toml");
let err = Config::from_file(&missing).unwrap_err();
match &err {
Error::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound),
other => panic!("expected an I/O failure, got {other}"),
}
assert!(
err.to_string().contains(&missing.display().to_string()),
"{err}"
);
let os = std::fs::metadata(&missing).unwrap_err().to_string();
assert!(err.to_string().contains(&os), "{err} lacks {os}");
assert!(!err.to_string().contains("entity not found"), "{err}");
}
}