mod analyzer_configs;
mod processor_configs;
mod variables;
#[cfg(test)]
mod tests;
pub(crate) use analyzer_configs::*;
pub(crate) use processor_configs::*;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use crate::errors;
use variables::substitute_variables;
const CONFIG_FILE: &str = "rsconstruct.toml";
pub(crate) const SCAN_CONFIG_FIELDS: &[&str] = &[
"scan_dir", "extensions", "exclude_dirs", "exclude_files", "exclude_paths",
];
pub(crate) trait KnownFields {
fn known_fields() -> &'static [&'static str];
}
pub(crate) fn resolve_extra_inputs(extra_inputs: &[String]) -> Result<Vec<PathBuf>> {
let mut resolved = Vec::new();
for p in extra_inputs {
let path = PathBuf::from(p);
if !path.exists() {
anyhow::bail!("extra_inputs file not found: {}", p);
}
resolved.push(path);
}
Ok(resolved)
}
pub(crate) fn config_hash(value: &impl Serialize) -> String {
let json = serde_json::to_string(value).expect(errors::CONFIG_SERIALIZE);
let hash = Sha256::digest(json.as_bytes());
hex::encode(hash)
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub(crate) struct ScanConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scan_dir: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exclude_dirs: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exclude_files: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exclude_paths: Option<Vec<String>>,
}
impl ScanConfig {
pub(crate) fn resolve(&mut self, scan_dir: &str, extensions: &[&str], exclude_dirs: &[&str]) {
if self.scan_dir.is_none() {
self.scan_dir = Some(scan_dir.to_string());
}
if self.extensions.is_none() {
self.extensions = Some(extensions.iter().map(|s| s.to_string()).collect());
}
if self.exclude_dirs.is_none() {
self.exclude_dirs = Some(exclude_dirs.iter().map(|s| s.to_string()).collect());
}
if self.exclude_files.is_none() {
self.exclude_files = Some(Vec::new());
}
if self.exclude_paths.is_none() {
self.exclude_paths = Some(Vec::new());
}
}
pub(crate) fn scan_dir(&self) -> &str {
self.scan_dir.as_deref().expect(errors::SCAN_CONFIG_NOT_RESOLVED)
}
pub(crate) fn extensions(&self) -> &[String] {
self.extensions.as_deref().expect(errors::SCAN_CONFIG_NOT_RESOLVED)
}
pub(crate) fn exclude_dirs(&self) -> &[String] {
self.exclude_dirs.as_deref().expect(errors::SCAN_CONFIG_NOT_RESOLVED)
}
pub(crate) fn exclude_files(&self) -> &[String] {
self.exclude_files.as_deref().expect(errors::SCAN_CONFIG_NOT_RESOLVED)
}
pub(crate) fn exclude_paths(&self) -> &[String] {
self.exclude_paths.as_deref().expect(errors::SCAN_CONFIG_NOT_RESOLVED)
}
}
const COMMON_EXCLUDE_DIRS: &[&str] = &["/.git/", "/out/", "/build/", "/dist/"];
const BUILD_TOOL_EXCLUDES: &[&str] = &[
"/.git/", "/out/", "/build/", "/dist/",
"/.rsconstruct/", "/node_modules/", "/target/", "/gems/",
];
const PYTHON_EXCLUDE_DIRS: &[&str] = &[
"/.git/", "/out/", "/build/", "/dist/",
"/.venv/", "/__pycache__/", "/node_modules/", "/.tox/", "/.eggs/",
];
const CC_EXCLUDE_DIRS: &[&str] = COMMON_EXCLUDE_DIRS;
const SPELLCHECK_EXCLUDE_DIRS: &[&str] = BUILD_TOOL_EXCLUDES;
const SHELL_EXCLUDE_DIRS: &[&str] = BUILD_TOOL_EXCLUDES;
const MARKDOWN_EXCLUDE_DIRS: &[&str] = BUILD_TOOL_EXCLUDES;
const MAKE_CARGO_EXCLUDES: &[&str] = &[
"/.git/", "/out/", "/build/", "/dist/",
"/.rsconstruct/", "/target/",
];
const DEFAULT_PLUGINS_DIR: &str = "plugins";
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct PluginsConfig {
#[serde(default = "default_plugins_dir")]
pub dir: String,
}
fn default_plugins_dir() -> String {
DEFAULT_PLUGINS_DIR.into()
}
impl Default for PluginsConfig {
fn default() -> Self {
Self { dir: DEFAULT_PLUGINS_DIR.into() }
}
}
#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(deny_unknown_fields)]
pub(crate) struct Config {
#[serde(default)]
pub build: BuildConfig,
#[serde(default)]
pub cache: CacheConfig,
#[serde(default)]
pub processor: ProcessorConfig,
#[serde(default)]
pub analyzer: AnalyzerConfig,
#[serde(default)]
pub completions: CompletionsConfig,
#[serde(default)]
pub graph: GraphConfig,
#[serde(default)]
pub plugins: PluginsConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct BuildConfig {
#[serde(default = "default_parallel")]
pub parallel: usize,
#[serde(default)]
pub batch_size: Option<usize>,
}
fn default_parallel() -> usize {
1
}
impl Default for BuildConfig {
fn default() -> Self {
Self {
parallel: 1,
batch_size: Some(0), }
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub(crate) enum RestoreMethod {
#[default]
Hardlink,
Copy,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct CacheConfig {
#[serde(default)]
pub restore_method: RestoreMethod,
#[serde(default)]
pub compression: bool,
#[serde(default)]
pub remote: Option<String>,
#[serde(default = "default_true")]
pub remote_push: bool,
#[serde(default = "default_true")]
pub remote_pull: bool,
#[serde(default = "default_true")]
pub mtime_check: bool,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
restore_method: RestoreMethod::default(),
compression: false,
remote: None,
remote_push: true,
remote_pull: true,
mtime_check: true,
}
}
}
pub(crate) fn default_true() -> bool {
true
}
macro_rules! gen_processor_config {
( $( $const_name:ident, $field:ident, $config_type:ty, $proc_type:ty,
($scan_dir:expr, $exts:expr, $excl:expr); )* ) => {
pub(crate) fn default_processors() -> Vec<String> {
vec![ $( crate::processors::names::$const_name.into(), )* ]
}
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ProcessorConfig {
#[serde(default = "default_true")]
pub auto_detect: bool,
#[serde(default = "default_processors")]
pub enabled: Vec<String>,
$(
#[serde(default)]
pub $field: $config_type,
)*
#[serde(flatten)]
pub extra: HashMap<String, toml::Value>,
}
impl Default for ProcessorConfig {
fn default() -> Self {
Self {
auto_detect: true,
enabled: default_processors(),
$( $field: <$config_type>::default(), )*
extra: HashMap::new(),
}
}
}
impl ProcessorConfig {
fn processor_enabled_field(&self, name: &str) -> bool {
match name {
$( stringify!($field) => self.$field.enabled, )*
_ => true, }
}
pub(crate) fn is_enabled(&self, name: &str) -> bool {
self.enabled.iter().any(|p| p == name) && self.processor_enabled_field(name)
}
pub(crate) fn scan_dirs(&self) -> Vec<String> {
let scans: &[&ScanConfig] = &[ $( &self.$field.scan, )* ];
let mut dirs: Vec<String> = scans.iter()
.filter_map(|s| s.scan_dir.as_deref())
.filter(|d| !d.is_empty())
.map(|d| d.to_string())
.collect();
dirs.sort();
dirs.dedup();
dirs
}
pub(crate) fn resolve_scan_defaults(&mut self) {
$( self.$field.scan.resolve($scan_dir, $exts, $excl); )*
}
pub(crate) fn known_fields_for(name: &str) -> Option<&'static [&'static str]> {
match name {
$( stringify!($field) => Some(<$config_type as KnownFields>::known_fields()), )*
_ => None,
}
}
pub(crate) fn defconfig_json(name: &str) -> Option<String> {
let json: serde_json::Value = match name {
$( stringify!($field) => {
let mut cfg = <$config_type>::default();
cfg.scan.resolve($scan_dir, $exts, $excl);
serde_json::to_value(cfg).ok()?
}, )*
_ => return None,
};
serde_json::to_string_pretty(&json).ok()
}
}
};
}
for_each_processor!(gen_processor_config);
pub(crate) fn default_script_check_linter() -> String {
"true".into()
}
pub(crate) fn default_cc_compiler() -> String {
"gcc".into()
}
pub(crate) fn default_cxx_compiler() -> String {
"g++".into()
}
pub(crate) fn default_output_suffix() -> String {
".elf".into()
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct CompletionsConfig {
#[serde(default = "default_shells")]
pub shells: Vec<String>,
}
fn default_shells() -> Vec<String> {
vec!["bash".into()]
}
impl Default for CompletionsConfig {
fn default() -> Self {
Self { shells: vec!["bash".into()] }
}
}
fn default_analyzers() -> Vec<String> {
vec!["cpp".into(), "python".into()]
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct AnalyzerConfig {
#[serde(default = "default_true")]
pub auto_detect: bool,
#[serde(default = "default_analyzers")]
pub enabled: Vec<String>,
#[serde(default)]
pub cpp: CppAnalyzerConfig,
#[serde(default)]
pub python: PythonAnalyzerConfig,
}
impl Default for AnalyzerConfig {
fn default() -> Self {
Self {
auto_detect: true,
enabled: default_analyzers(),
cpp: CppAnalyzerConfig::default(),
python: PythonAnalyzerConfig::default(),
}
}
}
impl AnalyzerConfig {
pub(crate) fn is_enabled(&self, name: &str) -> bool {
self.enabled.iter().any(|a| a == name)
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(deny_unknown_fields)]
pub(crate) struct GraphConfig {
#[serde(default)]
pub viewer: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FieldType {
String,
Bool,
Integer,
StringArray,
TableArray,
}
impl FieldType {
fn label(self) -> &'static str {
match self {
FieldType::String => "a string",
FieldType::Bool => "a boolean",
FieldType::Integer => "an integer",
FieldType::StringArray => "an array of strings",
FieldType::TableArray => "an array of tables",
}
}
fn matches(self, value: &toml::Value) -> bool {
match self {
FieldType::String => value.is_str(),
FieldType::Bool => value.as_bool().is_some(),
FieldType::Integer => value.is_integer(),
FieldType::StringArray => value.as_array()
.is_some_and(|arr| arr.iter().all(|v| v.is_str())),
FieldType::TableArray => value.as_array()
.is_some_and(|arr| arr.iter().all(|v| v.is_table())),
}
}
fn describe_value(value: &toml::Value) -> &'static str {
match value {
toml::Value::String(_) => "a string",
toml::Value::Integer(_) => "an integer",
toml::Value::Float(_) => "a float",
toml::Value::Boolean(_) => "a boolean",
toml::Value::Datetime(_) => "a datetime",
toml::Value::Array(_) => "an array",
toml::Value::Table(_) => "a table",
}
}
}
fn expected_field_type(processor: &str, field: &str) -> Option<FieldType> {
match field {
"scan_dir" => return Some(FieldType::String),
"extensions" => return Some(FieldType::StringArray),
"exclude_dirs" => return Some(FieldType::StringArray),
"exclude_files" => return Some(FieldType::StringArray),
"exclude_paths" => return Some(FieldType::StringArray),
"enabled" => return Some(FieldType::Bool),
"args" => return Some(FieldType::StringArray),
"extra_inputs" => return Some(FieldType::StringArray),
"auto_inputs" => return Some(FieldType::StringArray),
_ => {}
}
match (processor, field) {
("tera", "strict" | "trim_blocks") => Some(FieldType::Bool),
("ruff", "linter") => Some(FieldType::String),
("cc_single_file", "cc" | "cxx" | "output_suffix") => Some(FieldType::String),
("cc_single_file", "cflags" | "cxxflags" | "ldflags" | "include_paths") => Some(FieldType::StringArray),
("cc_single_file", "include_scanner") => Some(FieldType::String),
("cc_single_file", "compilers") => Some(FieldType::TableArray),
("cc", "cc" | "cxx") => Some(FieldType::String),
("cc", "cflags" | "cxxflags" | "ldflags" | "include_dirs") => Some(FieldType::StringArray),
("cc", "single_invocation" | "cache_output_dir") => Some(FieldType::Bool),
("clang_tidy", "compiler_args") => Some(FieldType::StringArray),
("spellcheck", "language" | "words_file") => Some(FieldType::String),
("spellcheck", "auto_add_words") => Some(FieldType::Bool),
("make", "make" | "target") => Some(FieldType::String),
("cargo", "profiles") => Some(FieldType::StringArray),
("cargo" | "clippy", "cargo" | "command") => Some(FieldType::String),
("mypy" | "pyrefly" | "shellcheck" | "luacheck" | "script_check", "checker") => Some(FieldType::String),
("rumdl" | "yamllint" | "jsonlint" | "taplo", "linter") => Some(FieldType::String),
("jq", "checker") => Some(FieldType::String),
("tags", "output" | "tags_file") => Some(FieldType::String),
("tags", "tags_file_strict") => Some(FieldType::Bool),
("pip", "pip") => Some(FieldType::String),
("sphinx", "sphinx_build" | "output_dir" | "working_dir") => Some(FieldType::String),
("mdbook", "mdbook" | "output_dir") => Some(FieldType::String),
("npm", "npm" | "command") => Some(FieldType::String),
("gem", "bundler" | "command" | "gem_home") => Some(FieldType::String),
("mdl", "gem_home" | "mdl_bin" | "gem_stamp") => Some(FieldType::String),
("mdl", "local_repo") => Some(FieldType::Bool),
("markdownlint", "markdownlint_bin" | "npm_stamp") => Some(FieldType::String),
("markdownlint", "local_repo") => Some(FieldType::Bool),
("aspell", "aspell" | "conf_dir" | "conf" | "words_file") => Some(FieldType::String),
("aspell", "auto_add_words") => Some(FieldType::Bool),
("marp", "marp_bin" | "output_dir") => Some(FieldType::String),
("marp", "formats") => Some(FieldType::StringArray),
("pandoc", "pandoc" | "from" | "output_dir") => Some(FieldType::String),
("pandoc", "formats") => Some(FieldType::StringArray),
("markdown", "markdown_bin" | "output_dir") => Some(FieldType::String),
("pdflatex", "pdflatex" | "output_dir") => Some(FieldType::String),
("pdflatex", "runs") => Some(FieldType::Integer),
("pdflatex", "qpdf") => Some(FieldType::Bool),
("a2x", "a2x" | "format" | "output_dir") => Some(FieldType::String),
("chromium", "chromium_bin" | "output_dir") => Some(FieldType::String),
("mermaid", "mmdc_bin" | "output_dir") => Some(FieldType::String),
("mermaid", "formats") => Some(FieldType::StringArray),
("drawio", "drawio_bin" | "output_dir") => Some(FieldType::String),
("drawio", "formats") => Some(FieldType::StringArray),
("libreoffice", "libreoffice_bin" | "output_dir") => Some(FieldType::String),
("libreoffice", "formats") => Some(FieldType::StringArray),
("pdfunite", "pdfunite_bin" | "source_dir" | "source_ext" | "source_output_dir" | "output_dir") => Some(FieldType::String),
("objdump", "output_dir") => Some(FieldType::String),
("cargo" | "sphinx" | "mdbook" | "npm" | "gem", "cache_output_dir") => Some(FieldType::Bool),
_ => None,
}
}
fn validate_processor_fields(raw: &toml::Value) -> Result<()> {
let processor_table = match raw.get("processor").and_then(|v| v.as_table()) {
Some(t) => t,
None => return Ok(()),
};
let mut errors = Vec::new();
for (name, value) in processor_table {
let table = match value.as_table() {
Some(t) => t,
None => continue, };
let own_fields: &[&str] = match ProcessorConfig::known_fields_for(name.as_str()) {
Some(fields) => fields,
None => continue, };
for (key, field_value) in table {
if !own_fields.contains(&key.as_str()) && !SCAN_CONFIG_FIELDS.contains(&key.as_str()) {
let all_fields: Vec<&str> = own_fields.iter()
.chain(SCAN_CONFIG_FIELDS.iter())
.copied()
.collect();
errors.push(format!(
"[processor.{}]: unknown field '{}' (valid fields: {})",
name, key, all_fields.join(", ")
));
continue;
}
if let Some(expected) = expected_field_type(name, key)
&& !expected.matches(field_value)
{
errors.push(format!(
"[processor.{}]: field '{}' must be {}, got {} ({})",
name, key, expected.label(),
FieldType::describe_value(field_value),
field_value,
));
}
}
}
if errors.is_empty() {
Ok(())
} else {
anyhow::bail!("Invalid config:\n{}", errors.join("\n"))
}
}
impl Config {
pub(crate) fn require_config() -> Result<()> {
let config_path = Path::new(CONFIG_FILE);
if !config_path.exists() {
return Err(crate::exit_code::RsconstructError::new(
crate::exit_code::RsconstructExitCode::ConfigError,
"No rsconstruct.toml found. Run 'rsconstruct init' to create one.",
).into());
}
Ok(())
}
pub(crate) fn load() -> Result<Self> {
let config_path = Path::new(CONFIG_FILE);
let mut config = if config_path.exists() {
let content = fs::read_to_string(config_path)
.with_context(|| format!("Failed to read config file: {}", config_path.display()))?;
let substituted = substitute_variables(&content)
.with_context(|| format!("Failed to substitute variables in: {}", config_path.display()))?;
let raw: toml::Value = toml::from_str(&substituted)
.with_context(|| format!("Failed to parse config file: {}", config_path.display()))?;
validate_processor_fields(&raw)?;
toml::from_str(&substituted)
.with_context(|| format!("Failed to parse config file: {}", config_path.display()))?
} else {
Config::default()
};
config.processor.resolve_scan_defaults();
Ok(config)
}
}
pub(crate) fn scan_config_from_toml(
value: &toml::Value,
default_scan_dir: &str,
default_extensions: &[&str],
default_exclude_dirs: &[&str],
) -> ScanConfig {
let table = value.as_table();
let scan_dir = table
.and_then(|t| t.get("scan_dir"))
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let extensions = table
.and_then(|t| t.get("extensions"))
.and_then(|v| v.as_array())
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect());
let exclude_dirs = table
.and_then(|t| t.get("exclude_dirs"))
.and_then(|v| v.as_array())
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect());
let exclude_files = table
.and_then(|t| t.get("exclude_files"))
.and_then(|v| v.as_array())
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect());
let exclude_paths = table
.and_then(|t| t.get("exclude_paths"))
.and_then(|v| v.as_array())
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect());
let mut scan = ScanConfig {
scan_dir,
extensions,
exclude_dirs,
exclude_files,
exclude_paths,
};
scan.resolve(default_scan_dir, default_extensions, default_exclude_dirs);
scan
}