use std::io::{self, Write};
use serde_json::{Value, json};
use super::commands::{document_at, virtual_path};
use super::dispatch::{Call, Memory, Outcome, corrective, dispatch};
use super::index::count;
use super::mounts::{resolve, writable};
use crate::repr::repr;
use crate::store::error::{Error, MemoryError};
use crate::store::journal::Action;
pub fn settle(
result: Result<u8, Error>,
json: bool,
out: &mut impl Write,
err: &mut impl Write,
) -> io::Result<u8> {
match result {
Ok(code) => Ok(code),
Err(Error::Memory(error)) => fail(&corrective(&error), json, out, err),
Err(Error::Value(sentence)) => fail(&Outcome::fail(sentence), json, out, err),
Err(Error::Io(error)) => Err(error),
}
}
pub fn fail(
outcome: &Outcome,
json: bool,
out: &mut impl Write,
err: &mut impl Write,
) -> io::Result<u8> {
let error = outcome.result.as_ref().err();
if json {
writeln!(out, "{}", json!({"error": error, "hint": outcome.reminder}))?;
}
if let Some(error) = error {
writeln!(err, "error: {error}")?;
}
if let Some(hint) = outcome.reminder.as_deref().filter(|hint| !hint.is_empty()) {
writeln!(err, "hint: {hint}")?;
}
Ok(1)
}
pub fn versions(
memory: &Memory,
path: &str,
limit: usize,
json: bool,
out: &mut impl Write,
) -> Result<u8, Error> {
let (mount, document) = document_at(memory, path)?;
let rows = memory.store.versions(&mount.scope, document, limit)?;
let shown = virtual_path(mount, document);
if json {
let versions: Vec<Value> = rows
.iter()
.map(|row| {
json!({
"version": row.version,
"action": row.action.as_str(),
"actor": row.actor,
"created_at": row.created_at.iso(),
"redacted": row.redacted,
"content": row.content,
})
})
.collect();
writeln!(out, "{}", json!({"path": shown, "versions": versions}))?;
return Ok(0);
}
if rows.is_empty() {
writeln!(out, "no history for {shown}")?;
}
for row in &rows {
let actor = row.actor.as_deref().filter(|actor| !actor.is_empty());
let marker = if row.redacted { " (redacted)" } else { "" };
let (action, at) = (row.action.as_str(), row.created_at.iso());
writeln!(
out,
"v{} {action} {} {at}{marker}",
row.version,
actor.unwrap_or("-")
)?;
}
Ok(0)
}
pub fn redact(
memory: &Memory,
store: &crate::store::FileStore,
path: &str,
scope_wide: bool,
json: bool,
out: &mut impl Write,
) -> Result<u8, Error> {
let (mount, document) = resolve(&memory.mounts, path)?;
writable(mount)?;
let target = (!scope_wide).then_some(document);
let matched = store.redact(&mount.scope, target, &memory.actor)?;
if json {
let envelope = json!({"path": path, "scope_wide": scope_wide, "matched": matched});
writeln!(out, "{envelope}")?;
} else if scope_wide {
let (cleared, scope) = (count(matched), repr(&mount.scope));
writeln!(
out,
"redacted {cleared} in scope {scope} (mount /{})",
mount.path
)?;
} else {
let (cleared, shown) = (count(matched), virtual_path(mount, document));
writeln!(out, "redacted {cleared} at {shown}")?;
}
Ok(0)
}
fn undo(memory: &Memory, path: &str, version: u64) -> Result<Outcome, Error> {
let (mount, document) = document_at(memory, path)?;
writable(mount)?;
let rows = memory.store.versions(&mount.scope, document, 2)?;
let shown = virtual_path(mount, document);
let Some(newest) = rows.first() else {
return Ok(Outcome::fail(format!("No history for {shown}"))
.hint("List documents with the 'view' command and path '/' first."));
};
if newest.version != version {
return Err(MemoryError::conflict(&mount.scope, Some(document), "revert_stale").into());
}
let prior = rows.get(1).filter(|row| row.action != Action::Deleted);
if newest.redacted || prior.is_some_and(|row| row.redacted) {
let cleared = "history is redacted; the prior content was cleared and cannot be restored";
return Ok(Outcome::fail(format!("{shown}'s {cleared}"))
.hint("Create the document again if it is still needed."));
}
let (inverse, prose) = match prior {
Some(row) => (
Call::Create {
path,
content: &row.content,
},
format!("Reverted {shown} to v{}'s content", row.version),
),
None => (
Call::Delete { path },
format!("Reverted {shown}: v{version} undone — the document is removed"),
),
};
let undone = dispatch(memory, &inverse)?;
Ok(match undone.receipt {
Some(receipt) if undone.result.is_ok() => {
let prose = format!("{prose} (now v{})", receipt.version);
Outcome::ok(prose).wrote(receipt.path, receipt.version, receipt.previous_path)
}
_ => undone,
})
}
pub fn revert(
memory: &Memory,
path: &str,
version: u64,
json: bool,
out: &mut impl Write,
err: &mut impl Write,
) -> Result<u8, Error> {
let undone = undo(memory, path, version)?;
let Ok(detail) = &undone.result else {
return Ok(fail(&undone, json, out, err)?);
};
if json {
let receipt = undone.receipt.as_ref();
let envelope = json!({
"command": "revert",
"path": receipt.map(|receipt| &receipt.path),
"version": receipt.map(|receipt| receipt.version),
"previous_path": receipt.and_then(|receipt| receipt.previous_path.as_ref()),
"detail": detail,
});
writeln!(out, "{envelope}")?;
} else {
writeln!(out, "{detail}")?;
}
Ok(0)
}