use std::io;
use super::commands;
use super::mounts::{Mount, resolve, state_path};
use crate::state::State;
use crate::store::error::{Error, MemoryError};
use crate::store::grammar::Actor;
use std::sync::Arc;
const MAILBOX: &str =
"Use messages for mailbox lifecycle changes; operator redaction remains available.";
pub struct Memory {
pub store: Arc<dyn State>,
pub mounts: Vec<Mount>,
pub actor: Actor,
}
pub enum Call<'a> {
View {
path: &'a str,
range: Option<(i64, i64)>,
},
Create {
path: &'a str,
content: &'a str,
},
StrReplace {
path: &'a str,
old: &'a str,
new: &'a str,
},
Insert {
path: &'a str,
line: i64,
text: &'a str,
},
Delete {
path: &'a str,
},
Rename {
old: &'a str,
new: &'a str,
},
}
#[derive(Debug, PartialEq, Eq)]
pub struct Receipt {
pub path: String,
pub version: u64,
pub previous_path: Option<String>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Outcome {
pub result: Result<String, String>,
pub reminder: Option<String>,
pub receipt: Option<Receipt>,
}
impl Outcome {
pub fn ok(data: impl Into<String>) -> Self {
Self {
result: Ok(data.into()),
reminder: None,
receipt: None,
}
}
pub fn fail(error: impl Into<String>) -> Self {
Self {
result: Err(error.into()),
reminder: None,
receipt: None,
}
}
pub fn hint(mut self, reminder: impl Into<String>) -> Self {
self.reminder = Some(reminder.into());
self
}
pub fn wrote(mut self, path: String, version: u64, previous_path: Option<String>) -> Self {
self.receipt = Some(Receipt {
path,
version,
previous_path,
});
self
}
}
pub fn corrective(error: &MemoryError) -> Outcome {
let outcome = Outcome::fail(format!("[{}] {}", error.code, error.message));
let hint = match error.code {
"memory_document_not_found" => {
"Run the memory tool with command 'view' and path '/' to see what exists."
}
"memory_path_invalid" => {
"Paths look like /mount/topic-name; check the index with view '/'."
}
"memory_read_only_mount" => {
"This mount is reference-only; write to a writable mount instead."
}
"memory_edit_only_mount" => {
"This mount's document set is fixed; update its existing documents instead of \
creating, deleting, or renaming."
}
"memory_conflict" => "The document changed underneath you — view it again before editing.",
"memory_format_unsupported" => {
"This document was written by a newer neosian; leave it untouched."
}
_ => return outcome,
};
outcome.hint(hint)
}
pub fn dispatch(memory: &Memory, call: &Call) -> io::Result<Outcome> {
let answered = || {
let written = match *call {
Call::View { .. } => vec![],
Call::Rename { old, new } => vec![old, new],
Call::Create { path, .. }
| Call::StrReplace { path, .. }
| Call::Insert { path, .. }
| Call::Delete { path } => vec![path],
};
for path in written {
if state_path(resolve(&memory.mounts, path)?.1) {
return Ok(Outcome::fail(MAILBOX));
}
}
match *call {
Call::View { path, range } => commands::view(memory, path, range),
Call::Create { path, content } => commands::create(memory, path, content),
Call::StrReplace { path, old, new } => commands::str_replace(memory, path, old, new),
Call::Insert { path, line, text } => commands::insert(memory, path, line, text),
Call::Delete { path } => commands::delete(memory, path),
Call::Rename { old, new } => commands::rename(memory, old, new),
}
};
match answered() {
Ok(outcome) => Ok(outcome),
Err(Error::Memory(error)) => Ok(corrective(&error)),
Err(Error::Value(sentence)) => Ok(Outcome::fail(sentence)),
Err(Error::Io(error)) => Err(error),
}
}