use crate::context::{
EnvironmentAdmissionError, InvocationContext, InvocationContextWire, RenderOrigin,
};
use crate::interview::{ApplicationSeed, Resolution};
use crate::{AnswerError, Interview, RawAnswer, RawAnswers, Seed, Template};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::{
fs,
io::Write,
path::{Component, Path, PathBuf},
sync::atomic::{AtomicU64, Ordering},
};
static NEXT_TEMP: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StagedRecord {
target: PathBuf,
pub template: String,
pub commit: String,
#[serde(default)]
pub named: bool,
pub now: String,
pub submissions: Vec<IndexMap<String, Value>>,
#[serde(default, skip_serializing_if = "IndexMap::is_empty")]
pub(crate) update_overrides: IndexMap<String, Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub base: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub reanswer: bool,
#[serde(default, skip_serializing_if = "crate::parts::Selection::is_default")]
pub part_selection: crate::parts::Selection,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seed: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seed_entry: Option<Value>,
#[serde(default, skip_serializing_if = "InvocationContextWire::is_legacy")]
context: InvocationContextWire,
}
#[derive(Debug, thiserror::Error)]
pub enum StagingError {
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Json(#[from] serde_json::Error),
#[error("{0}")]
Replay(String),
#[error("environment access at {reference} requires stage trust")]
EnvironmentTrustRequired { reference: RenderOrigin },
#[error("{source}")]
EnvironmentAdmission {
#[source]
source: EnvironmentAdmissionError,
},
}
pub fn stage_admission_error(error: EnvironmentAdmissionError) -> StagingError {
match error {
EnvironmentAdmissionError::TrustRequired { reference } => {
StagingError::EnvironmentTrustRequired { reference }
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanonicalTarget(PathBuf);
impl CanonicalTarget {
pub fn as_path(&self) -> &Path {
&self.0
}
}
impl std::fmt::Display for CanonicalTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.display().fmt(f)
}
}
pub fn canonical_target(path: &Path) -> Result<CanonicalTarget, StagingError> {
let absolute = if path.is_absolute() {
path.to_owned()
} else {
std::env::current_dir()?.join(path)
};
let mut normalized = PathBuf::new();
for component in absolute.components() {
match component {
Component::Prefix(_) | Component::RootDir | Component::Normal(_) => {
normalized.push(component)
}
Component::CurDir => {}
Component::ParentDir => {
normalized.pop();
}
}
}
let mut ancestor = normalized.as_path();
while !ancestor.exists() {
ancestor = ancestor
.parent()
.ok_or_else(|| StagingError::Replay("target has no existing ancestor".into()))?;
}
let suffix = normalized
.strip_prefix(ancestor)
.expect("ancestor is a prefix");
let mut canonical = ancestor.canonicalize()?;
if !suffix.as_os_str().is_empty() {
canonical.push(suffix);
}
Ok(CanonicalTarget(canonical))
}
pub struct Store {
dir: PathBuf,
}
impl Store {
pub fn new(dir: PathBuf) -> Self {
Self { dir }
}
pub fn path_for(&self, target: &CanonicalTarget) -> PathBuf {
let digest = Sha256::digest(target.as_path().to_string_lossy().as_bytes());
self.dir.join(format!("{digest:x}.json"))
}
fn legacy_path_for(&self, target: &CanonicalTarget) -> Option<PathBuf> {
let path = target.as_path();
path.parent()?;
let mut legacy = path.as_os_str().to_os_string();
legacy.push(std::path::MAIN_SEPARATOR_STR);
let digest = Sha256::digest(Path::new(&legacy).to_string_lossy().as_bytes());
Some(self.dir.join(format!("{digest:x}.json")))
}
pub fn load(&self, target: &CanonicalTarget) -> Result<Option<StagedRecord>, StagingError> {
let read = |path: PathBuf| -> Result<Option<StagedRecord>, StagingError> {
match fs::read(path) {
Ok(bytes) => Ok(Some(serde_json::from_slice(&bytes)?)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
};
if let Some(record) = read(self.path_for(target))? {
if canonical_target(&record.target)? != *target {
return Err(StagingError::Replay(
"staged record target does not match its storage key".into(),
));
}
return Ok(Some(record));
}
let Some(path) = self.legacy_path_for(target) else {
return Ok(None);
};
let Some(record) = read(path)? else {
return Ok(None);
};
if canonical_target(&record.target)? != *target {
return Err(StagingError::Replay(
"staged record target does not match its storage key".into(),
));
}
Ok(Some(record))
}
pub fn save(
&self,
target: &CanonicalTarget,
record: &StagedRecord,
) -> Result<(), StagingError> {
if canonical_target(&record.target)? != *target {
return Err(StagingError::Replay(
"staged record target does not match the active target".into(),
));
}
fs::create_dir_all(&self.dir)?;
let canonical_path = self.path_for(target);
let mut stored = record.clone();
stored.target = target.as_path().to_owned();
let bytes = serde_json::to_vec_pretty(&stored)?;
let (temp, mut file) = loop {
let n = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
let temp = self
.dir
.join(format!(".toha-{}-{n}.tmp", std::process::id()));
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&temp)
{
Ok(file) => break (temp, file),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e.into()),
}
};
let result = (|| {
file.write_all(&bytes)?;
file.sync_all()?;
fs::rename(&temp, &canonical_path)
})();
if result.is_err() {
let _ = fs::remove_file(&temp);
}
result.map_err(StagingError::Io)?;
if let Some(legacy) = self
.legacy_path_for(target)
.filter(|p| p != &canonical_path)
{
match fs::remove_file(legacy) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e.into()),
}
}
Ok(())
}
pub fn remove(&self, target: &CanonicalTarget) -> Result<bool, StagingError> {
let canonical = self.path_for(target);
let legacy = self
.legacy_path_for(target)
.filter(|path| path != &canonical);
let remove = |path: PathBuf| -> Result<bool, StagingError> {
match fs::remove_file(path) {
Ok(()) => Ok(true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e.into()),
}
};
let canonical_removed = remove(canonical)?;
let legacy_removed = match legacy {
Some(path) => remove(path)?,
None => false,
};
Ok(canonical_removed || legacy_removed)
}
}
impl StagedRecord {
pub fn new(
target: &CanonicalTarget,
template: String,
commit: String,
named: bool,
now: String,
submissions: Vec<IndexMap<String, Value>>,
) -> Self {
Self {
target: target.as_path().to_owned(),
template,
commit,
named,
now,
submissions,
update_overrides: IndexMap::new(),
base: None,
reanswer: false,
part_selection: Default::default(),
seed: None,
seed_entry: None,
context: InvocationContextWire::Legacy,
}
}
pub fn new_with_context(
context: InvocationContext,
commit: String,
named: bool,
now: String,
submissions: Vec<IndexMap<String, Value>>,
) -> Self {
let (target, formal_name, wire) = context.staged_parts();
Self {
target: target.as_path().to_owned(),
template: formal_name.unwrap_or_default(),
commit,
named,
now,
submissions,
update_overrides: IndexMap::new(),
base: None,
reanswer: false,
part_selection: Default::default(),
seed: None,
seed_entry: None,
context: wire,
}
}
pub fn invocation_context(
&self,
target: &CanonicalTarget,
) -> Result<InvocationContext, StagingError> {
if canonical_target(&self.target)? != *target {
return Err(StagingError::Replay(
"staged record target does not match its storage key".into(),
));
}
Ok(InvocationContext::from_wire(
target.clone(),
&self.template,
self.context.clone(),
))
}
pub fn replay<'a>(
&self,
template: &'a Template,
target: &CanonicalTarget,
) -> Result<Interview<'a>, StagingError> {
self.replay_with_defaults(template, IndexMap::new(), target)
}
pub fn replay_with_defaults<'a>(
&self,
template: &'a Template,
defaults: IndexMap<crate::Id, RawAnswer>,
target: &CanonicalTarget,
) -> Result<Interview<'a>, StagingError> {
let now = self
.now
.parse()
.map_err(|e: jiff::Error| StagingError::Replay(e.to_string()))?;
let context = self.invocation_context(target)?;
let interview = Interview::start_with_selection(
template,
Seed {
now,
defaults,
context,
},
self.part_selection.clone(),
)
.map_err(|e| StagingError::Replay(e.to_string()))?;
self.replay_from(interview, template)
}
pub fn replay_with_resolution<'a>(
&self,
template: &'a Template,
resolution: Resolution,
target: &CanonicalTarget,
) -> Result<Interview<'a>, StagingError> {
let now = self
.now
.parse()
.map_err(|e: jiff::Error| StagingError::Replay(e.to_string()))?;
let context = self.invocation_context(target)?;
let interview = resolution
.start_with_selection(template, now, context, None, self.part_selection.clone())
.map(|(interview, _)| interview)
.map_err(|e| StagingError::Replay(e.to_string()))?;
self.replay_from(interview, template)
}
pub fn replay_with_seed<'a>(
&self,
template: &'a Template,
resolution: Resolution,
seed: Option<ApplicationSeed>,
target: &CanonicalTarget,
) -> Result<Interview<'a>, StagingError> {
self.replay_with_seed_reporting(template, resolution, seed, target)
.map(|(interview, _)| interview)
}
pub(crate) fn replay_with_seed_reporting<'a>(
&self,
template: &'a Template,
resolution: Resolution,
seed: Option<ApplicationSeed>,
target: &CanonicalTarget,
) -> Result<(Interview<'a>, Vec<String>), StagingError> {
let now = self
.now
.parse()
.map_err(|e: jiff::Error| StagingError::Replay(e.to_string()))?;
let context = self.invocation_context(target)?;
let (interview, warnings) = resolution
.start_with_selection(template, now, context, seed, self.part_selection.clone())
.map_err(|e| StagingError::Replay(e.to_string()))?;
self.replay_from(interview, template)
.map(|interview| (interview, warnings))
}
fn replay_from<'a>(
&self,
mut interview: Interview<'a>,
template: &Template,
) -> Result<Interview<'a>, StagingError> {
for submission in &self.submissions {
let filtered: IndexMap<_, _> = submission.iter().filter(|(id, _)| {
let Ok(id) = crate::Id::parse(id) else { return true; };
!matches!(template.question(&id).map(|q| &q.kind), Some(crate::QuestionKind::Document { contract }) if contract.admitted().is_some())
}).map(|(id, value)| (id.clone(), value.clone())).collect();
if !submission.is_empty() && filtered.is_empty() {
continue;
}
let submission = &filtered;
let Interview::Asking(pending) = interview else {
return Err(StagingError::Replay(
"submission after completed interview".into(),
));
};
let raw: RawAnswers = submission
.iter()
.map(|(key, value)| crate::Id::parse(key).map(|id| (id, RawAnswer(value.clone()))))
.collect::<Result<_, _>>()
.map_err(StagingError::Replay)?;
interview = pending.answer(raw).map_err(|e| match e {
AnswerError::Rejected { rejections, .. } => StagingError::Replay(
rejections
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n"),
),
AnswerError::Eval(e) => StagingError::Replay(e.to_string()),
})?;
}
Ok(interview)
}
}