use crate::engine::Engine;
use crate::merge::MergeOptions;
use crate::provider::SubprocessProvider;
use crate::search::{Options, SearchProvider};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
#[cfg(all(feature = "fetch", feature = "ipfs"))]
use crate::fetch::IpfsGatewayFetcher;
#[cfg(feature = "fetch")]
use crate::fetch::{CommandFetcher, FetchRegistry, HttpFetcher, ProxyFetcher};
pub const CONFIG_VERSION: u32 = 1;
fn default_version() -> u32 {
CONFIG_VERSION
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Config {
#[serde(default = "default_version")]
pub version: u32,
#[serde(default, rename = "provider", skip_serializing_if = "Vec::is_empty")]
pub providers: Vec<ProviderConfig>,
#[serde(default)]
pub fetch: FetchConfig,
#[serde(default)]
pub ranking: RankingConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProviderConfig {
pub name: String,
pub program: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub args: Vec<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub env: BTreeMap<String, String>,
}
impl ProviderConfig {
pub fn to_provider(&self) -> SubprocessProvider {
let mut provider = SubprocessProvider::new(self.name.as_str(), self.program.as_str())
.args(self.args.clone());
if !self.env.is_empty() {
provider = provider.envs(self.env.clone());
}
provider
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FetchConfig {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub commands: BTreeMap<String, CommandConfig>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub proxies: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_bytes: Option<u64>,
#[serde(default)]
pub deny_link_local: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandConfig {
pub program: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub args: Vec<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RankingConfig {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub weights: BTreeMap<String, f64>,
}
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("cannot read config at {path}: {source}")]
Read {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("invalid config: {0}")]
Parse(#[from] toml::de::Error),
#[error("cannot serialize config: {0}")]
Serialize(#[from] toml::ser::Error),
#[error("unsupported config version {found} (this build supports up to {supported})")]
UnsupportedVersion {
found: u32,
supported: u32,
},
#[error("invalid config: {0}")]
Invalid(String),
#[error("environment variable `{name}` is referenced but not set")]
MissingEnv {
name: String,
},
#[error("malformed environment reference in `{value}`")]
InvalidEnvSyntax {
value: String,
},
}
impl Default for Config {
fn default() -> Self {
Self {
version: CONFIG_VERSION,
providers: Vec::new(),
fetch: FetchConfig::default(),
ranking: RankingConfig::default(),
}
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn from_toml_str(text: &str) -> Result<Self, ConfigError> {
let config: Config = toml::from_str(text)?;
config.validate()?;
Ok(config)
}
pub fn load(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
let path = path.as_ref();
let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read {
path: path.to_path_buf(),
source,
})?;
let config = Self::from_toml_str(&text)?.expand_env()?;
config.validate()?;
Ok(config)
}
pub fn to_toml_string(&self) -> Result<String, ConfigError> {
Ok(toml::to_string_pretty(self)?)
}
pub fn expand_env(&self) -> Result<Config, ConfigError> {
self.expand_env_with(|name| std::env::var(name).ok())
}
pub fn expand_env_with<F>(&self, lookup: F) -> Result<Config, ConfigError>
where
F: Fn(&str) -> Option<String>,
{
let mut config = self.clone();
for provider in &mut config.providers {
provider.program = expand(&provider.program, &lookup)?;
for arg in &mut provider.args {
*arg = expand(arg, &lookup)?;
}
for value in provider.env.values_mut() {
*value = expand(value, &lookup)?;
}
}
for command in config.fetch.commands.values_mut() {
command.program = expand(&command.program, &lookup)?;
for arg in &mut command.args {
*arg = expand(arg, &lookup)?;
}
}
Ok(config)
}
pub fn validate(&self) -> Result<(), ConfigError> {
if self.version == 0 || self.version > CONFIG_VERSION {
return Err(ConfigError::UnsupportedVersion {
found: self.version,
supported: CONFIG_VERSION,
});
}
let mut names = BTreeSet::new();
for provider in &self.providers {
if provider.name.trim().is_empty() {
return Err(ConfigError::Invalid(
"provider name must not be empty".to_owned(),
));
}
if !names.insert(provider.name.as_str()) {
return Err(ConfigError::Invalid(format!(
"duplicate provider name `{}`",
provider.name
)));
}
if provider.program.trim().is_empty() {
return Err(ConfigError::Invalid(format!(
"provider `{}` must set a program",
provider.name
)));
}
if provider.env.keys().any(|key| key.trim().is_empty()) {
return Err(ConfigError::Invalid(format!(
"provider `{}` has an empty environment variable name",
provider.name
)));
}
}
for (scheme, command) in &self.fetch.commands {
if scheme.trim().is_empty() {
return Err(ConfigError::Invalid(
"fetch command scheme must not be empty".to_owned(),
));
}
if command.program.trim().is_empty() {
return Err(ConfigError::Invalid(format!(
"fetch command `{scheme}` must set a program"
)));
}
}
for (scheme, proxy) in &self.fetch.proxies {
if scheme.trim().is_empty() {
return Err(ConfigError::Invalid(
"fetch proxy scheme must not be empty".to_owned(),
));
}
if proxy.trim().is_empty() {
return Err(ConfigError::Invalid(format!(
"fetch proxy for `{scheme}` must not be empty"
)));
}
}
for (provider, weight) in &self.ranking.weights {
if !weight.is_finite() || *weight < 0.0 {
return Err(ConfigError::Invalid(format!(
"ranking weight for `{provider}` must be a finite, non-negative number"
)));
}
}
Ok(())
}
pub fn providers(&self) -> Vec<SubprocessProvider> {
self.providers
.iter()
.map(ProviderConfig::to_provider)
.collect()
}
pub fn merge_options(&self) -> MergeOptions {
MergeOptions {
provider_weights: self
.ranking
.weights
.iter()
.map(|(provider, weight)| (provider.clone(), *weight))
.collect(),
}
}
pub fn engine(&self) -> Engine {
self.engine_with_options(Options::default())
}
pub fn engine_with_options(&self, options: Options) -> Engine {
let providers: Vec<Arc<dyn SearchProvider>> = self
.providers()
.into_iter()
.map(|provider| Arc::new(provider) as Arc<dyn SearchProvider>)
.collect();
Engine::new(providers)
.with_options(options)
.with_weights(self.merge_options().provider_weights)
.with_fetchers(self.fetch_registry())
}
#[cfg(feature = "fetch")]
pub fn fetch_registry(&self) -> FetchRegistry {
let mut registry = FetchRegistry::new();
for (scheme, command) in &self.fetch.commands {
registry.register(std::sync::Arc::new(CommandFetcher::new(
scheme.clone(),
vec![scheme.clone()],
command.program.clone(),
command.args.clone(),
)));
}
for (scheme, proxy) in &self.fetch.proxies {
registry.register(std::sync::Arc::new(ProxyFetcher::new(
scheme.clone(),
proxy.clone(),
)));
}
registry.register(std::sync::Arc::new({
let mut http = HttpFetcher::new();
if let Some(max_bytes) = self.fetch.max_bytes {
http = http.with_max_bytes(max_bytes);
}
if self.fetch.deny_link_local {
http = http.with_deny_link_local(true);
}
http
}));
#[cfg(feature = "ipfs")]
registry.register(std::sync::Arc::new(
IpfsGatewayFetcher::with_default_gateway(),
));
#[cfg(feature = "file")]
registry.register(std::sync::Arc::new(crate::fetch::FileFetcher::new()));
registry
}
}
fn expand(value: &str, lookup: &dyn Fn(&str) -> Option<String>) -> Result<String, ConfigError> {
if !value.contains('$') {
return Ok(value.to_owned());
}
let mut out = String::with_capacity(value.len());
let mut chars = value.chars().peekable();
while let Some(character) = chars.next() {
if character != '$' {
out.push(character);
continue;
}
match chars.peek().copied() {
Some('$') => {
chars.next();
out.push('$');
}
Some('{') => {
chars.next();
let mut name = String::new();
let mut closed = false;
for inner in chars.by_ref() {
if inner == '}' {
closed = true;
break;
}
name.push(inner);
}
if !closed || !is_valid_env_name(&name) {
return Err(ConfigError::InvalidEnvSyntax {
value: value.to_owned(),
});
}
out.push_str(&resolve(&name, lookup)?);
}
Some(start) if start.is_ascii_alphabetic() || start == '_' => {
let mut name = String::new();
while let Some(&next) = chars.peek() {
if next.is_ascii_alphanumeric() || next == '_' {
name.push(next);
chars.next();
} else {
break;
}
}
out.push_str(&resolve(&name, lookup)?);
}
_ => out.push('$'),
}
}
Ok(out)
}
fn resolve(name: &str, lookup: &dyn Fn(&str) -> Option<String>) -> Result<String, ConfigError> {
lookup(name).ok_or_else(|| ConfigError::MissingEnv {
name: name.to_owned(),
})
}
fn is_valid_env_name(name: &str) -> bool {
let mut chars = name.chars();
match chars.next() {
Some(first) if first.is_ascii_alphabetic() || first == '_' => {}
_ => return false,
}
chars.all(|character| character.is_ascii_alphanumeric() || character == '_')
}
#[cfg(test)]
mod tests {
use super::*;
use crate::search::SearchProvider;
fn sample() -> Config {
let mut env = BTreeMap::new();
env.insert("MOCKNET_URL".to_owned(), "http://127.0.0.1:8642".to_owned());
let mut weights = BTreeMap::new();
weights.insert("local".to_owned(), 1.5);
let mut commands = BTreeMap::new();
commands.insert(
"magnet".to_owned(),
CommandConfig {
program: "aria2c".to_owned(),
args: vec!["{uri}".to_owned(), "{dest}".to_owned()],
},
);
Config {
version: CONFIG_VERSION,
providers: vec![ProviderConfig {
name: "local".to_owned(),
program: "node".to_owned(),
args: vec!["adapter.js".to_owned()],
env,
}],
fetch: FetchConfig {
commands,
proxies: BTreeMap::new(),
..FetchConfig::default()
},
ranking: RankingConfig { weights },
}
}
#[test]
fn round_trips_through_toml() {
let config = sample();
let text = config.to_toml_string().expect("serialize");
let parsed = Config::from_toml_str(&text).expect("parse");
assert_eq!(parsed, config);
}
#[test]
fn version_defaults_to_current() {
let config = Config::from_toml_str("[[provider]]\nname = \"a\"\nprogram = \"p\"").unwrap();
assert_eq!(config.version, CONFIG_VERSION);
}
#[test]
fn parses_the_canonical_shape() {
let config = Config::from_toml_str(
r#"
version = 1
[[provider]]
name = "local"
program = "node"
args = ["adapter.js"]
env = { MOCKNET_URL = "http://localhost:8642" }
[ranking]
weights = { local = 1.5 }
[fetch.commands.magnet]
program = "aria2c"
args = ["-d", "{dir}", "{uri}"]
"#,
)
.expect("valid config");
assert_eq!(config.providers.len(), 1);
let provider = &config.providers[0];
assert_eq!(provider.name, "local");
assert_eq!(provider.args, vec!["adapter.js"]);
assert_eq!(provider.env["MOCKNET_URL"], "http://localhost:8642");
assert_eq!(config.ranking.weights["local"], 1.5);
assert_eq!(config.fetch.commands["magnet"].program, "aria2c");
}
#[test]
fn rejects_a_newer_version() {
let error = Config::from_toml_str("version = 99").unwrap_err();
assert!(matches!(
error,
ConfigError::UnsupportedVersion { found: 99, .. }
));
}
#[test]
fn rejects_duplicate_provider_names() {
let error = Config::from_toml_str(
"[[provider]]\nname = \"a\"\nprogram = \"p\"\n\n[[provider]]\nname = \"a\"\nprogram = \"q\"",
)
.unwrap_err();
assert!(matches!(error, ConfigError::Invalid(_)));
}
#[test]
fn rejects_empty_program() {
let error =
Config::from_toml_str("[[provider]]\nname = \"a\"\nprogram = \" \"").unwrap_err();
assert!(matches!(error, ConfigError::Invalid(_)));
}
#[test]
fn rejects_invalid_weights() {
for weight in ["-1.0", "nan", "inf"] {
let text = format!("[ranking]\nweights = {{ a = {weight} }}");
let error = Config::from_toml_str(&text).unwrap_err();
assert!(matches!(error, ConfigError::Invalid(_)), "{weight}");
}
}
#[test]
fn unknown_fields_are_ignored_for_forward_compatibility() {
let config = Config::from_toml_str(
"future = true\n[[provider]]\nname = \"a\"\nprogram = \"p\"\nextra = 3",
)
.expect("unknown fields ignored");
assert_eq!(config.providers[0].name, "a");
}
#[test]
fn builds_providers_and_merge_options() {
let config = sample();
let providers = config.providers();
assert_eq!(providers.len(), 1);
assert_eq!(providers[0].name(), "local");
assert_eq!(config.merge_options().provider_weights["local"], 1.5);
}
#[cfg(feature = "fetch")]
#[test]
fn builds_a_fetch_registry() {
let registry = sample().fetch_registry();
assert!(registry.fetcher_for("magnet").is_some());
assert!(registry.fetcher_for("https").is_some());
}
#[test]
fn engine_wires_configured_providers() {
let engine = sample().engine();
let names: Vec<&str> = engine
.providers()
.iter()
.map(|provider| provider.name())
.collect();
assert_eq!(names, vec!["local"]);
}
#[test]
fn engine_with_options_keeps_configured_providers() {
let options = Options {
limit: Some(3),
..Options::default()
};
let engine = sample().engine_with_options(options);
assert_eq!(engine.providers().len(), 1);
}
#[test]
fn engine_of_empty_config_has_no_providers() {
let engine = Config::new().engine();
assert!(engine.providers().is_empty());
}
fn provider_with(program: &str, args: Vec<&str>, env: BTreeMap<String, String>) -> Config {
Config {
version: CONFIG_VERSION,
providers: vec![ProviderConfig {
name: "p".to_owned(),
program: program.to_owned(),
args: args.into_iter().map(str::to_owned).collect(),
env,
}],
fetch: FetchConfig::default(),
ranking: RankingConfig::default(),
}
}
#[test]
fn expands_variables_in_program_args_and_env() {
let mut env = BTreeMap::new();
env.insert("API_TOKEN".to_owned(), "${TOKEN}".to_owned());
let config = provider_with("${BIN}", vec!["--dir=$DATA_DIR", "literal$$dollar"], env);
let expanded = config
.expand_env_with(|name| match name {
"BIN" => Some("/usr/bin/provider".to_owned()),
"DATA_DIR" => Some("/srv/data".to_owned()),
"TOKEN" => Some("s3cr3t".to_owned()),
_ => None,
})
.expect("expand");
let provider = &expanded.providers[0];
assert_eq!(provider.program, "/usr/bin/provider");
assert_eq!(provider.args, vec!["--dir=/srv/data", "literal$dollar"]);
assert_eq!(provider.env["API_TOKEN"], "s3cr3t");
}
#[test]
fn expands_fetch_command_program_and_args() {
let mut commands = BTreeMap::new();
commands.insert(
"magnet".to_owned(),
CommandConfig {
program: "${ARIA}".to_owned(),
args: vec!["-d".to_owned(), "${DIR}".to_owned(), "{uri}".to_owned()],
},
);
let config = Config {
version: CONFIG_VERSION,
providers: Vec::new(),
fetch: FetchConfig {
commands,
proxies: BTreeMap::new(),
..FetchConfig::default()
},
ranking: RankingConfig::default(),
};
let expanded = config
.expand_env_with(|name| match name {
"ARIA" => Some("aria2c".to_owned()),
"DIR" => Some("/tmp".to_owned()),
_ => None,
})
.expect("expand");
let command = &expanded.fetch.commands["magnet"];
assert_eq!(command.program, "aria2c");
assert_eq!(command.args, vec!["-d", "/tmp", "{uri}"]);
}
#[test]
fn errors_on_a_missing_variable() {
let error = provider_with("$NOPE", vec![], BTreeMap::new())
.expand_env_with(|_| None)
.unwrap_err();
assert!(matches!(error, ConfigError::MissingEnv { name } if name == "NOPE"));
}
#[test]
fn rejects_malformed_environment_references() {
for bad in ["${UNCLOSED", "${1BAD}", "${}", "${BAD-NAME}"] {
let error = provider_with(bad, vec![], BTreeMap::new())
.expand_env_with(|_| Some("x".to_owned()))
.unwrap_err();
assert!(
matches!(error, ConfigError::InvalidEnvSyntax { .. }),
"{bad}"
);
}
}
#[test]
fn treats_a_lone_dollar_as_literal() {
let expanded = provider_with("cost$5", vec![], BTreeMap::new())
.expand_env_with(|_| Some("x".to_owned()))
.expect("expand");
assert_eq!(expanded.providers[0].program, "cost$5");
}
#[test]
fn load_expands_the_process_environment() {
let var = format!("PTOX_CONFIG_TEST_{}", std::process::id());
std::env::set_var(&var, "/opt/bin");
let path =
std::env::temp_dir().join(format!("ptox-config-expand-{}.toml", std::process::id()));
std::fs::write(
&path,
format!("[[provider]]\nname = \"p\"\nprogram = \"${{{var}}}/provider\"\n"),
)
.expect("write temp config");
let config = Config::load(&path).expect("load");
assert_eq!(config.providers[0].program, "/opt/bin/provider");
let _ = std::fs::remove_file(&path);
std::env::remove_var(&var);
}
#[test]
fn parses_and_registers_fetch_proxies() {
let config = Config::from_toml_str(
r#"
[[provider]]
name = "tor"
program = "node"
args = ["tor.js"]
[fetch.proxies]
tor = "socks5h://127.0.0.1:9050"
i2p = "http://127.0.0.1:4444"
"#,
)
.expect("valid config");
assert_eq!(config.fetch.proxies["tor"], "socks5h://127.0.0.1:9050");
let registry = config.fetch_registry();
assert!(registry.fetcher_for("tor").is_some());
assert!(registry.fetcher_for("i2p").is_some());
}
#[test]
fn rejects_an_empty_fetch_proxy() {
let error = Config::from_toml_str("[fetch.proxies]\ntor = \" \"").unwrap_err();
assert!(matches!(error, ConfigError::Invalid(_)));
}
#[test]
fn parses_the_fetch_policy() {
let config =
Config::from_toml_str("[fetch]\nmax_bytes = 1048576\ndeny_link_local = true\n")
.expect("valid config");
assert_eq!(config.fetch.max_bytes, Some(1_048_576));
assert!(config.fetch.deny_link_local);
assert_eq!(FetchConfig::default().max_bytes, None);
assert!(!FetchConfig::default().deny_link_local);
}
}