use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use serde_json::{Map, Value};
use super::document::read_json;
use super::error::ConfigError;
const STUDY_MANIFEST: &str = "study.json";
const WORKFLOW_CONFIG_DIRECTORY: &str = "wf_configs";
#[derive(Clone)]
pub(crate) struct Config {
inner: Arc<ConfigInner>,
}
struct ConfigInner {
project_root: PathBuf,
config_root: PathBuf,
study_path: PathBuf,
study: Value,
runtime: super::RuntimeSettings,
documents: BTreeMap<PathBuf, ConfigDocument>,
}
#[derive(Clone)]
pub(crate) struct ConfigSnapshot {
bytes: Arc<[u8]>,
parameters: Arc<Value>,
}
impl ConfigSnapshot {
pub(crate) fn bytes(&self) -> &[u8] {
&self.bytes
}
pub(crate) fn parameters(&self) -> &Value {
&self.parameters
}
}
impl std::fmt::Debug for ConfigSnapshot {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("ConfigSnapshot")
.field("bytes", &self.bytes.len())
.finish()
}
}
struct ConfigDocument {
path: PathBuf,
value: Value,
}
impl Config {
pub(crate) fn load(project_root: &Path) -> Result<Self, ConfigError> {
let project_root = canonicalize(project_root)?;
ensure_utf8(&project_root, "project root")?;
let config_root = canonicalize(&project_root.join(WORKFLOW_CONFIG_DIRECTORY))?;
let study_path = canonicalize(&config_root.join(STUDY_MANIFEST))?;
ensure_contained(&config_root, &study_path)?;
let study = read_json(&study_path)?;
let mut paths = Vec::new();
discover_json(&config_root, &mut paths)?;
paths.sort();
let mut documents = BTreeMap::new();
for path in paths {
let canonical = canonicalize(&path)?;
ensure_contained(&config_root, &canonical)?;
ensure_utf8(&canonical, "configuration document")?;
if canonical == study_path {
continue;
}
let relative = path
.strip_prefix(&config_root)
.expect("contained config document has a relative path")
.to_path_buf();
if relative.to_str().is_none() {
return Err(ConfigError::invalid(
&path,
"/",
"a config document path relative to `wf_configs/` must be valid UTF-8",
));
}
let value = read_json(&canonical)?;
documents.insert(
relative,
ConfigDocument {
path: canonical,
value,
},
);
}
let (runtime_path, runtime_value) = documents
.get(Path::new("runtime.json"))
.map(|d| (d.path.as_path(), &d.value))
.ok_or_else(|| {
ConfigError::invalid(
&config_root.join("runtime.json"),
"/",
"required runtime.json is missing",
)
})?;
let runtime =
super::RuntimeSettings::parse(runtime_path, runtime_value.clone(), &project_root)?;
Ok(Self {
inner: Arc::new(ConfigInner {
project_root,
config_root,
study_path,
study,
runtime,
documents,
}),
})
}
pub(crate) fn verify_capture(&self, captured: &Path) -> Result<(), ConfigError> {
let study = captured.join("wf_configs/study.json");
if read_json(&study)? != self.inner.study {
return Err(ConfigError::invalid(
&study,
"/",
"study changed while inputs were captured",
));
}
for (relative, document) in &self.inner.documents {
let path = captured.join("wf_configs").join(relative);
if read_json(&path)? != document.value {
return Err(ConfigError::invalid(
&path,
"/",
"configuration changed while inputs were captured",
));
}
}
Ok(())
}
pub(crate) fn runtime(&self) -> &super::RuntimeSettings {
&self.inner.runtime
}
pub(crate) fn project_root(&self) -> &Path {
&self.inner.project_root
}
pub(crate) fn config_root(&self) -> &Path {
&self.inner.config_root
}
pub(crate) fn study_path(&self) -> &Path {
&self.inner.study_path
}
pub(crate) fn study_value(&self) -> &Value {
&self.inner.study
}
pub(crate) fn document(&self, relative: &Path) -> Option<(&Path, &Value)> {
self.inner
.documents
.get(relative)
.map(|document| (document.path.as_path(), &document.value))
}
pub(crate) fn project_document(
&self,
authored: &Path,
) -> Result<Option<(&Path, &Value)>, ConfigError> {
let path = if authored.is_absolute() {
authored.to_path_buf()
} else {
self.project_root().join(authored)
};
let canonical = canonicalize(&path)?;
ensure_contained(self.config_root(), &canonical)?;
Ok(self
.inner
.documents
.values()
.find(|document| document.path == canonical)
.map(|document| (document.path.as_path(), &document.value)))
}
pub(crate) fn snapshot_with_parameters(
&self,
parameters: &Value,
active_phases: &[usize],
) -> ConfigSnapshot {
ConfigSnapshot {
bytes: Arc::from(snapshot_json_with_parameters(
&self.inner.study,
&self.inner.documents,
parameters,
active_phases,
)),
parameters: Arc::new(parameters.clone()),
}
}
}
impl std::fmt::Debug for Config {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Config")
.field("project_root", &self.project_root())
.field("config_root", &self.config_root())
.field("documents", &self.inner.documents.keys())
.finish()
}
}
fn discover_json(directory: &Path, paths: &mut Vec<PathBuf>) -> Result<(), ConfigError> {
let entries = std::fs::read_dir(directory).map_err(|source| ConfigError::Read {
path: directory.to_path_buf(),
source,
})?;
let mut entries =
entries
.collect::<Result<Vec<_>, _>>()
.map_err(|source| ConfigError::Read {
path: directory.to_path_buf(),
source,
})?;
entries.sort_by_key(std::fs::DirEntry::file_name);
for entry in entries {
let file_type = entry.file_type().map_err(|source| ConfigError::Read {
path: entry.path(),
source,
})?;
if file_type.is_dir() {
discover_json(&entry.path(), paths)?;
} else if (file_type.is_file() || file_type.is_symlink())
&& entry.path().extension().and_then(std::ffi::OsStr::to_str) == Some("json")
{
paths.push(entry.path());
}
}
Ok(())
}
fn snapshot_json_with_parameters(
study: &Value,
documents: &BTreeMap<PathBuf, ConfigDocument>,
parameters: &Value,
active_phases: &[usize],
) -> Box<[u8]> {
let mut study = study.clone();
study
.as_object_mut()
.expect("validated study root is an object")
.insert(
"active_phases".to_owned(),
serde_json::to_value(active_phases)
.expect("effective phase indices are JSON-compatible"),
);
snapshot_json_inner(&study, documents, parameters)
}
fn snapshot_json_inner(
study: &Value,
documents: &BTreeMap<PathBuf, ConfigDocument>,
resolved_parameters: &Value,
) -> Box<[u8]> {
let mut config = Map::new();
for (relative, document) in documents {
let value = if relative == Path::new("parameters.json") {
resolved_parameters
} else {
&document.value
};
config.insert(
relative
.to_str()
.expect("config paths were validated as UTF-8")
.replace(std::path::MAIN_SEPARATOR, "/"),
value.clone(),
);
}
let snapshot = Value::Object(Map::from_iter([
("study".to_owned(), study.clone()),
("config".to_owned(), Value::Object(config)),
]));
serde_json::to_vec_pretty(&snapshot)
.expect("serializing centrally parsed JSON cannot fail")
.into_boxed_slice()
}
pub(crate) fn canonicalize(path: &Path) -> Result<PathBuf, ConfigError> {
std::fs::canonicalize(path).map_err(|source| ConfigError::Read {
path: path.to_path_buf(),
source,
})
}
pub(crate) fn ensure_contained(root: &Path, path: &Path) -> Result<(), ConfigError> {
if path.starts_with(root) {
Ok(())
} else {
Err(ConfigError::PathOutsideConfig {
path: path.to_path_buf(),
config_root: root.to_path_buf(),
})
}
}
pub(crate) fn ensure_utf8(path: &Path, context: &'static str) -> Result<(), ConfigError> {
if path.to_str().is_some() {
Ok(())
} else {
Err(ConfigError::NonUtf8Path {
path: path.to_path_buf(),
context,
})
}
}