use std::path::Path;
use crate::error::{Result, RototoError};
mod create;
mod engine;
mod entry;
mod layer;
mod lists;
mod operation;
mod paths;
#[cfg(test)]
mod tests;
mod tree;
mod value;
mod variable;
pub use operation::{
AllocationArmInput, ChangeRecord, EditOperation, EditOptions, EditOutcome, EditPlan,
PlannedWrite,
};
pub use tree::EditTree;
pub fn apply(
tree: &EditTree,
operations: &[EditOperation],
options: &EditOptions,
) -> Result<EditOutcome> {
engine::apply(tree, operations, options)
}
pub async fn apply_to_package(
root: &Path,
operations: &[EditOperation],
options: &EditOptions,
) -> Result<EditOutcome> {
let tree = EditTree::snapshot(root).await?;
apply(&tree, operations, options)
}
pub async fn write_plan(root: &Path, plan: &EditPlan) -> Result<()> {
for write in &plan.writes {
let path = root.join(&write.path);
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await.map_err(|err| {
RototoError::new(format!(
"failed to create directory {}: {err}",
parent.display()
))
})?;
}
tokio::fs::write(&path, &write.content)
.await
.map_err(|err| {
RototoError::new(format!("failed to write {}: {err}", path.display()))
})?;
}
for delete in &plan.deletes {
let path = root.join(delete);
tokio::fs::remove_file(&path).await.map_err(|err| {
RototoError::new(format!("failed to delete {}: {err}", path.display()))
})?;
}
Ok(())
}