use std::collections::{BTreeMap, BTreeSet};
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use datumesh_core::json::{self, Value};
use datumesh_core::{
sha256_hex, Arg, Args, Cell, ClassDescriptor, FunctionDescriptor, Receipt, ReceiptBuilder, Scalar, SigningKey, Type,
};
use datumesh_engine::{Clock, SystemClock};
use datumesh_formula::definition::{Callee, Callees, ClassCallee};
use datumesh_store::Store;
const END: &str = "9999-12-31T23:59:59.999999Z";
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Published {
pub functions: usize,
pub classes: usize,
pub cells: usize,
pub receipts: usize,
pub versions: usize,
pub blobs: usize,
pub blob_bytes: u64,
pub descriptors: u64,
}
#[derive(Clone)]
pub struct PublishOptions {
pub clock: Arc<dyn Clock + Send + Sync>,
pub backfill: Option<PathBuf>,
}
impl PublishOptions {
pub fn system() -> PublishOptions {
PublishOptions { clock: Arc::new(SystemClock), backfill: None }
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PublishErrorKind {
Line,
StartBeforeNow {
name: String,
start: String,
now: String,
},
Backfill(String),
OwnedVectorName(String),
}
impl std::fmt::Display for PublishErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PublishErrorKind::Line => f.write_str("refused"),
PublishErrorKind::StartBeforeNow { name, start, now } => {
write!(f, "binding start {start} of {name} is earlier than the mesh clock {now} (rule A)")
}
PublishErrorKind::Backfill(why) => write!(f, "--backfill refused: {why}"),
PublishErrorKind::OwnedVectorName(name) => {
write!(f, "{name} is under a namespace the mesh owns: import-vectors does not bind it")
}
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PublishError {
pub line: usize,
pub detail: String,
pub kind: PublishErrorKind,
}
impl PublishError {
fn at(line: usize, detail: impl Into<String>) -> PublishError {
PublishError { line, detail: detail.into(), kind: PublishErrorKind::Line }
}
pub(crate) fn typed(line: usize, kind: PublishErrorKind) -> PublishError {
PublishError { line, detail: kind.to_string(), kind }
}
}
impl std::fmt::Display for PublishError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "line {}: {}", self.line, self.detail)
}
}
impl std::error::Error for PublishError {}
fn s(v: &str) -> Value {
Value::String(v.to_string())
}
fn namespace_of(name: &str) -> &str {
name.rsplit_once('/').map(|(ns, _)| ns).unwrap_or("")
}
fn host_of(name: &str) -> &str {
name.split('/').next().unwrap_or("")
}
fn definer<'a>(name: &'a str, host: &'a str, owned: &[String]) -> &'a str {
let host_of_name = name.split('/').next().unwrap_or("");
if owned.iter().any(|o| o == host_of_name) {
namespace_of(name)
} else {
host
}
}
fn bound_of(v: &Value, line: usize, now: &str) -> Result<String, PublishError> {
match v.get("bound") {
None => Ok(now.to_string()),
Some(b) => b.as_str().map(str::to_string).ok_or_else(|| PublishError::at(line, "bound is not an instant")),
}
}
#[derive(Default)]
struct Simulation {
functions: BTreeMap<String, Vec<(String, String)>>,
classes: BTreeMap<String, Vec<(String, String)>>,
descriptors: BTreeMap<String, Vec<u8>>,
class_bytes: BTreeMap<String, Vec<u8>>,
versions: BTreeMap<String, Vec<(String, String)>>,
}
impl Simulation {
fn latest(binds: &BTreeMap<String, Vec<(String, String)>>, name: &str, at: &str) -> Option<(String, String)> {
binds.get(name)?.iter().filter(|(t, _)| t.as_str() <= at).max_by(|a, b| a.0.cmp(&b.0)).cloned()
}
}
struct Callers<'a, S: Store + ?Sized> {
store: &'a S,
sim: Option<&'a Simulation>,
}
impl<S: Store + ?Sized> Callers<'_, S> {
fn function_bytes(&self, hash: &str) -> Result<Option<Vec<u8>>, String> {
if let Some(b) = self.sim.and_then(|s| s.descriptors.get(hash)) {
return Ok(Some(b.clone()));
}
Ok(self.store.descriptor(hash).map(|b| b.into_owned()))
}
fn class_bytes(&self, hash: &str) -> Result<Option<Vec<u8>>, String> {
if let Some(b) = self.sim.and_then(|s| s.class_bytes.get(hash)) {
return Ok(Some(b.clone()));
}
self.store.class(hash).map_err(|e| e.to_string())
}
}
impl<S: Store + ?Sized> Callees for Callers<'_, S> {
fn callee(&self, name: &str, at: &str) -> Result<Option<Callee>, String> {
let stored = self.store.resolve_binding(name, at).map_err(|e| e.to_string())?;
let simmed = self.sim.and_then(|s| Simulation::latest(&s.functions, name, at));
let (fn_hash, start) = match (stored, simmed) {
(Some(b), Some((t, h))) if t >= b.tx => (h, t),
(Some(b), _) => (b.fn_hash, b.tx),
(None, Some((t, h))) => (h, t),
(None, None) => return Ok(None),
};
let bytes = self.function_bytes(&fn_hash)?.ok_or_else(|| format!("descriptor {fn_hash} is not stored"))?;
let descriptor = json::parse(&bytes).map_err(|e| e.to_string())?;
Ok(Some(Callee { fn_hash, start, descriptor }))
}
fn bound_class(&self, name: &str, at: &str) -> Result<Option<ClassCallee>, String> {
let stored = self.store.resolve_class_binding(name, at).map_err(|e| e.to_string())?;
let simmed = self.sim.and_then(|s| Simulation::latest(&s.classes, name, at));
let (class_hash, start) = match (stored, simmed) {
(Some(b), Some((t, h))) if t >= b.tx => (h, t),
(Some(b), _) => (b.class_hash, b.tx),
(None, Some((t, h))) => (h, t),
(None, None) => return Ok(None),
};
let bytes = self.class_bytes(&class_hash)?.ok_or_else(|| format!("class {class_hash} is not stored"))?;
let descriptor = ClassDescriptor::from_json(&bytes).map_err(|e| e.to_string())?;
Ok(Some(ClassCallee { class_hash, start, descriptor }))
}
fn class_descriptor(&self, class_hash: &str) -> Result<Option<ClassDescriptor>, String> {
let Some(bytes) = self.class_bytes(class_hash)? else { return Ok(None) };
ClassDescriptor::from_json(&bytes).map(Some).map_err(|e| e.to_string())
}
fn function_descriptor(&self, fn_hash: &str) -> Result<Option<Value>, String> {
let Some(bytes) = self.function_bytes(fn_hash)? else { return Ok(None) };
json::parse(&bytes).map(Some).map_err(|e| e.to_string())
}
}
fn current_function<S: Store + ?Sized>(store: &S, sim: &Simulation, name: &str) -> Result<Option<String>, String> {
if let Some((_, h)) = Simulation::latest(&sim.functions, name, END) {
return Ok(Some(h));
}
store.resolve_name(name, END).map_err(|e| e.to_string())
}
fn current_class<S: Store + ?Sized>(store: &S, sim: &Simulation, name: &str) -> Result<Option<String>, String> {
if let Some((_, h)) = Simulation::latest(&sim.classes, name, END) {
return Ok(Some(h));
}
store.resolve_class(name, END).map_err(|e| e.to_string())
}
fn function_at<S: Store + ?Sized>(store: &S, sim: &Simulation, name: &str, at: &str) -> Result<Option<String>, String> {
let stored = store.resolve_binding(name, at).map_err(|e| e.to_string())?;
let simmed = Simulation::latest(&sim.functions, name, at);
Ok(match (stored, simmed) {
(Some(b), Some((t, h))) if t >= b.tx => Some(h),
(Some(b), _) => Some(b.fn_hash),
(None, Some((_, h))) => Some(h),
(None, None) => None,
})
}
fn latest_bind<S: Store + ?Sized>(
store: &S,
sim: &Simulation,
name: &str,
class: bool,
) -> Result<Option<String>, String> {
let stored: Option<String> = if class {
store.resolve_class_binding(name, END).map_err(|e| e.to_string())?.map(|b| b.tx)
} else {
store.resolve_binding(name, END).map_err(|e| e.to_string())?.map(|b| b.tx)
};
let binds = if class { &sim.classes } else { &sim.functions };
let planned = binds.get(name).and_then(|v| v.iter().map(|(t, _)| t.clone()).max());
Ok(match (stored, planned) {
(Some(a), Some(b)) => Some(a.max(b)),
(a, b) => a.or(b),
})
}
fn bound_as_other<S: Store + ?Sized>(store: &S, sim: &Simulation, name: &str, class: bool) -> Result<bool, String> {
if class {
Ok(store.resolve_binding(name, END).map_err(|e| e.to_string())?.is_some() || sim.functions.contains_key(name))
} else {
Ok(store.resolve_class_binding(name, END).map_err(|e| e.to_string())?.is_some()
|| sim.classes.contains_key(name))
}
}
fn cell_latest<S: Store + ?Sized>(store: &S, sim: &Simulation, id: &str) -> Result<Option<(String, String)>, String> {
let stored = store.latest(id).map_err(|e| e.to_string())?.map(|v| (v.tx, v.receipt_hash));
let planned = sim.versions.get(id).and_then(|v| v.last().cloned());
Ok(match (stored, planned) {
(Some(a), Some(b)) => Some(if b.0 > a.0 { b } else { a }),
(a, b) => a.or(b),
})
}
fn cell_version_asof<S: Store + ?Sized>(
store: &S,
sim: &Simulation,
id: &str,
at: &str,
) -> Result<Option<String>, String> {
let stored = store.version_asof(id, at).map_err(|e| e.to_string())?.map(|v| (v.tx, v.receipt_hash));
let planned = sim.versions.get(id).and_then(|v| v.iter().rev().find(|(t, _)| t.as_str() <= at).cloned());
let latest = match (stored, planned) {
(Some(a), Some(b)) => Some(if b.0 > a.0 { b } else { a }),
(a, b) => a.or(b),
};
Ok(latest.map(|x| x.1))
}
fn suspended<S: Store + ?Sized>(store: &S, host: &str, line: usize) -> Result<(), PublishError> {
match store.namespace(host) {
Ok(Some(n)) if n.suspended_at.is_some() => {
Err(PublishError::at(line, format!("{host} is suspended: no new bindings or classes under it")))
}
Ok(_) => Ok(()),
Err(e) => Err(PublishError::at(line, e.to_string())),
}
}
struct Bind {
line: usize,
name: String,
}
#[derive(Default)]
struct Written {
namespaces: BTreeSet<String>,
starts: BTreeSet<String>,
}
impl Written {
fn bound(&mut self, name: &str, start: &str) {
self.namespaces.insert(namespace_of(name).to_string());
self.starts.insert(start.to_string());
}
}
struct Ctx<'a> {
host: &'a str,
owned: &'a [String],
key_from: &'a str,
now: &'a str,
base: &'a Path,
}
enum Op {
Descriptor { hash: String, bytes: Vec<u8>, counted: bool },
Function { name: String, start: String, hash: String, bytes: Vec<u8>, bind: bool },
Class { name: String, start: String, hash: String, bytes: Vec<u8>, bind: bool, methods: Vec<String> },
Cell { tx: String, id: String, hash: String, receipt: Vec<u8>, blob: Option<Vec<u8>>, version: bool },
}
fn read_lines(path: &Path) -> Result<Vec<(usize, String)>, PublishError> {
let file = std::fs::File::open(path).map_err(|e| PublishError::at(0, format!("{}: {e}", path.display())))?;
let mut out = Vec::new();
for (i, line) in std::io::BufReader::new(file).lines().enumerate() {
let n = i + 1;
let line = line.map_err(|e| PublishError::at(n, e.to_string()))?;
if !line.trim().is_empty() {
out.push((n, line));
}
}
Ok(out)
}
struct Planner<'a, S: Store + ?Sized> {
store: &'a S,
key: &'a SigningKey,
ctx: Ctx<'a>,
backfill: bool,
sim: Simulation,
ops: Vec<Op>,
binds: Vec<Bind>,
}
impl<S: Store + ?Sized> Planner<'_, S> {
fn callers(&self) -> Callers<'_, S> {
Callers { store: self.store, sim: Some(&self.sim) }
}
fn run(&mut self, lines: &[(usize, String)]) -> Result<(), PublishError> {
for (n, text) in lines {
let n = *n;
let err = |d: String| PublishError::at(n, d);
let v = json::parse(text.as_bytes()).map_err(|e| err(e.to_string()))?;
if let Some(name) = v.get("class").and_then(Value::as_str) {
self.class_line(n, name, &v)?;
} else if let Some(d) = v.get("descriptor") {
self.descriptor_line(n, d)?;
} else if let Some(name) = v.get("function").and_then(Value::as_str) {
let sig = v.get("signature").ok_or_else(|| err("a function without a signature".into()))?;
let start = bound_of(&v, n, self.ctx.now)?;
let fd = FunctionDescriptor::from_value(&stored_value(sig)).map_err(|e| err(e.to_string()))?;
self.function(n, name, start, fd.hash(), fd.canonical())?;
} else if let Some(name) = v.get("formula").and_then(Value::as_str) {
let source =
v.get("source").and_then(Value::as_str).ok_or_else(|| err("a formula without source".into()))?;
let sig = v.get("signature").ok_or_else(|| err("a formula without a signature".into()))?;
let start = bound_of(&v, n, self.ctx.now)?;
let def = datumesh_formula::definition::define_function(
name,
source,
sig,
&Default::default(),
&self.callers(),
&start,
)
.map_err(|e| err(format!("define {name}: {e}")))?;
self.function(n, name, start, def.fn_hash, def.bytes)?;
} else if let Some(cell_v) = v.get("cell") {
self.cell_line(n, &v, cell_v)?;
} else {
return Err(err("neither a function nor a cell".into()));
}
}
Ok(())
}
fn descriptor_line(&mut self, n: usize, d: &Value) -> Result<(), PublishError> {
let fd = FunctionDescriptor::from_value(d).map_err(|e| PublishError::at(n, e.to_string()))?;
if fd.lang != "stored" || !fd.body.is_empty() || !fd.inputs.is_empty() {
return Err(PublishError::at(
n,
"a descriptor line takes a stored function only: code.lang stored, code.body empty, no inputs",
));
}
let (hash, bytes) = (fd.hash(), fd.canonical());
self.sim.descriptors.insert(hash.clone(), bytes.clone());
self.ops.push(Op::Descriptor { hash, bytes, counted: true });
Ok(())
}
fn function(
&mut self,
n: usize,
name: &str,
start: String,
hash: String,
bytes: Vec<u8>,
) -> Result<(), PublishError> {
self.sim.descriptors.insert(hash.clone(), bytes.clone());
let current = current_function(self.store, &self.sim, name).map_err(|e| PublishError::at(n, e))?;
let bind = current.as_deref() != Some(hash.as_str());
if bind {
self.check_bind(n, name, &start, false)?;
self.sim.functions.entry(name.to_string()).or_default().push((start.clone(), hash.clone()));
}
self.ops.push(Op::Function { name: name.to_string(), start, hash, bytes, bind });
Ok(())
}
fn class_line(&mut self, n: usize, name: &str, v: &Value) -> Result<(), PublishError> {
let err = |d: String| PublishError::at(n, d);
let descriptor = v.get("descriptor").ok_or_else(|| err("a class without descriptor".into()))?;
let start = bound_of(v, n, self.ctx.now)?;
let def = datumesh_formula::definition::define_class(
name,
&descriptor.to_jcs(),
definer(name, self.ctx.host, self.ctx.owned),
&self.callers(),
&start,
)
.map_err(|e| err(format!("define {name}: {e}")))?;
suspended(self.store, host_of(&def.descriptor.steward.namespace), n)?;
let hash = def.class_hash.clone();
let methods: Vec<(String, String)> =
def.descriptor.methods.iter().map(|(m, fh)| (format!("{name}/{m}"), fh.clone())).collect();
let bind = current_class(self.store, &self.sim, name).map_err(err)?.as_deref() != Some(hash.as_str());
if bind {
self.check_bind(n, name, &start, true)?;
for (mname, _) in &methods {
self.check_bind(n, mname, &start, false)?;
}
self.sim.classes.entry(name.to_string()).or_default().push((start.clone(), hash.clone()));
for (mname, fh) in &methods {
self.sim.functions.entry(mname.clone()).or_default().push((start.clone(), fh.clone()));
}
}
self.sim.class_bytes.insert(hash.clone(), def.bytes.clone());
self.ops.push(Op::Class {
name: name.to_string(),
start,
hash,
bytes: def.bytes,
bind,
methods: methods.into_iter().map(|(m, _)| m).collect(),
});
Ok(())
}
fn check_bind(&mut self, line: usize, name: &str, start: &str, class: bool) -> Result<(), PublishError> {
if !self.backfill && start < self.ctx.now {
return Err(PublishError::typed(
line,
PublishErrorKind::StartBeforeNow { name: name.into(), start: start.into(), now: self.ctx.now.into() },
));
}
let err = |d: String| PublishError::at(line, d);
if bound_as_other(self.store, &self.sim, name, class).map_err(err)? {
let kind = if class { "function" } else { "class" };
return Err(err(format!("{name} is bound as a {kind}: one namespace of names")));
}
suspended(self.store, host_of(name), line)?;
if let Some(latest) = latest_bind(self.store, &self.sim, name, class).map_err(err)? {
if start <= latest.as_str() {
return Err(err(format!("{name} is bound from {latest}: a bind at {start} is not later")));
}
}
self.binds.push(Bind { line, name: name.into() });
Ok(())
}
fn cell_line(&mut self, n: usize, v: &Value, cell_v: &Value) -> Result<(), PublishError> {
let err = |d: String| PublishError::at(n, d);
let name = cell_v.get("fn").and_then(Value::as_str).ok_or_else(|| err("a cell without fn".into()))?;
let (ns, f) = name.rsplit_once('/').ok_or_else(|| err(format!("{name}: not a function name")))?;
let field = |k: &str| v.get(k).and_then(Value::as_str).map(str::to_string);
let required = |k: &str| field(k).ok_or_else(|| err(format!("no {k}")));
let (licence, summary) = (required("licence")?, required("summary")?);
let asof = field("asof").unwrap_or_else(|| self.ctx.now.to_string());
let tx = field("tx").unwrap_or_else(|| self.ctx.now.to_string());
if tx > asof {
return Err(err(format!("tx {tx} is after asof {asof} (SPEC 8)")));
}
let issued = field("issued").unwrap_or_else(|| std::cmp::max(asof.clone(), self.ctx.key_from.to_string()));
if asof > issued {
return Err(err(format!("asof {asof} is after issued {issued} (SPEC 8)")));
}
let fn_hash = function_at(self.store, &self.sim, name, &tx)
.map_err(err)?
.ok_or_else(|| err(format!("{name} is not bound as of {tx}")))?;
let d = self
.callers()
.function_bytes(&fn_hash)
.map_err(err)?
.ok_or_else(|| err(format!("no descriptor {fn_hash}")))?;
let d = FunctionDescriptor::from_json(&d).map_err(|e| err(e.to_string()))?;
let mut args = Args::new();
for (k, a) in cell_v.get("args").and_then(Value::as_object).ok_or_else(|| err("a cell without args".into()))? {
args.insert(k.clone(), Arg::Value(Scalar::from_json(&a.to_jcs()).map_err(|e| err(format!("{k}: {e}")))?));
}
let valid: Args = args
.iter()
.filter(|(k, _)| d.signature.args.get(*k).is_some_and(|x| x.valid))
.map(|(k, a)| (k.clone(), a.clone()))
.collect();
let value_v = v.get("value").ok_or_else(|| err("a cell without value".into()))?;
let (value, blob) = match value_v.get("blob").and_then(Value::as_str) {
Some(file) => {
let media = value_v.get("media_type").and_then(Value::as_str).unwrap_or("application/octet-stream");
let bytes = std::fs::read(self.ctx.base.join(file)).map_err(|e| err(format!("{file}: {e}")))?;
let sha = sha256_hex(&bytes);
let scalar = Scalar::from_lexical(Type::Blob, &format!("{sha}:{}:{media}", bytes.len()))
.map_err(|e| err(e.to_string()))?;
(scalar, Some(bytes))
}
None => (Scalar::from_json(&value_v.to_jcs()).map_err(|e| err(e.to_string()))?, None),
};
let cell = Cell { ns: ns.to_string(), fn_name: f.to_string(), fn_hash, args };
let r = ReceiptBuilder::new()
.cell(cell.clone())
.value(value)
.valid(valid)
.asof(asof.as_str())
.tx(tx.as_str())
.issued(issued.as_str())
.publisher(ns)
.signer_host(self.ctx.host)
.licence(licence.as_str())
.summary(summary.as_str())
.sign(self.key)
.map_err(|e| err(e.to_string()))?;
let receipt = receipt_json(&r);
let hash = Receipt::from_json(&receipt).map_err(|e| err(e.to_string()))?.hash().to_string();
let id = cell.id();
if field("tx").is_none() {
if let Some((_, latest)) = cell_latest(self.store, &self.sim, &id).map_err(err)? {
if let Some(old) = self.store.receipt(&latest) {
let old = json::parse(&old).map_err(|e| err(e.to_string()))?;
let new = json::parse(&receipt).map_err(|e| err(e.to_string()))?;
if ["type", "value", "valid", "licence", "summary"].iter().all(|k| old.get(k) == new.get(k)) {
return Ok(());
}
}
}
}
let version = match cell_latest(self.store, &self.sim, &id).map_err(err)? {
Some((latest_tx, _)) if latest_tx.as_str() >= tx.as_str() => {
if cell_version_asof(self.store, &self.sim, &id, &tx).map_err(err)?.as_deref() != Some(hash.as_str()) {
return Err(err(format!("{id} has a version at or after {tx} that is not this receipt")));
}
false
}
_ => {
self.sim.versions.entry(id.clone()).or_default().push((tx.clone(), hash.clone()));
true
}
};
self.ops.push(Op::Cell { tx, id, hash, receipt, blob, version });
Ok(())
}
}
fn stored_value(signature: &Value) -> Value {
Value::Object(vec![
("code".into(), Value::Object(vec![("body".into(), s("")), ("lang".into(), s("stored"))])),
("inputs".into(), Value::Array(vec![])),
("signature".into(), signature.clone()),
])
}
fn backfill_check<S: Store + ?Sized>(store: &S, binds: &[Bind]) -> Result<(), PublishError> {
for b in binds {
let refuse = |why: String| PublishError::typed(b.line, PublishErrorKind::Backfill(why));
if store.resolve_binding(&b.name, END).map_err(|e| PublishError::at(b.line, e.to_string()))?.is_some() {
return Err(refuse(format!("{} has a binding", b.name)));
}
let ns = namespace_of(&b.name);
if store.has_bindings_under(ns).map_err(|e| PublishError::at(b.line, e.to_string()))? {
return Err(refuse(format!("{ns} has bindings")));
}
}
Ok(())
}
fn append_log(log: &Path, written: &Written) -> Result<(), String> {
let line = Value::Object(vec![
("at".into(), s(&SystemClock.now())),
("namespaces".into(), Value::Array(written.namespaces.iter().map(|n| s(n)).collect())),
("starts".into(), Value::Array(written.starts.iter().map(|t| s(t)).collect())),
])
.to_jcs();
let mut opts = std::fs::OpenOptions::new();
opts.create(true).append(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.mode(0o600);
}
let mut f = opts.open(log).map_err(|e| format!("{}: {e}", log.display()))?;
let mut bytes = line;
bytes.push(b'\n');
f.write_all(&bytes).map_err(|e| format!("{}: {e}", log.display()))
}
fn write<S: Store + ?Sized>(store: &S, ops: &[Op]) -> Result<(Published, Written), PublishError> {
let mut out = Published::default();
let mut written = Written::default();
let err = |d: String| PublishError::at(0, d);
for op in ops {
match op {
Op::Descriptor { hash, bytes, counted } => {
let existed = store.descriptor(hash).is_some();
store.put_descriptor(bytes).map_err(|e| err(e.to_string()))?;
if *counted && !existed {
out.descriptors += 1;
}
}
Op::Function { name, start, hash, bytes, bind } => {
store.put_descriptor(bytes).map_err(|e| err(e.to_string()))?;
if *bind {
store.bind_name(name, hash, start).map_err(|e| err(format!("bind {name}: {e}")))?;
out.functions += 1;
written.bound(name, start);
}
}
Op::Class { name, start, hash, bytes, bind, methods } => {
store.put_class(bytes).map_err(|e| err(e.to_string()))?;
if *bind {
store.bind_class(name, hash, start).map_err(|e| err(format!("bind {name}: {e}")))?;
out.classes += 1;
written.bound(name, start);
for m in methods {
written.bound(m, start);
}
}
}
Op::Cell { tx, id, hash, receipt, blob, version } => {
if let Some(bytes) = blob {
store.put_blob(bytes).map_err(|e| err(e.to_string()))?;
out.blobs += 1;
out.blob_bytes += bytes.len() as u64;
}
store.put_receipt(receipt).map_err(|e| err(e.to_string()))?;
out.cells += 1;
out.receipts += 1;
if *version {
store.put_version(id, tx, hash).map_err(|e| err(e.to_string()))?;
out.versions += 1;
}
}
}
}
Ok((out, written))
}
pub fn receipt_json(r: &datumesh_core::Receipt) -> Vec<u8> {
let mut members = vec![("sig".to_string(), Value::String(r.sig().to_string()))];
if let Ok(Value::Object(m)) = json::parse(r.payload()) {
members.extend(m);
}
Value::Object(members).to_jcs()
}
pub fn publish_stored<S: Store + ?Sized>(
store: &S,
key: &SigningKey,
host: &str,
path: &Path,
) -> Result<Published, PublishError> {
publish_stored_in(store, key, host, &[host.to_string()], path)
}
pub fn publish_stored_in<S: Store + ?Sized>(
store: &S,
key: &SigningKey,
host: &str,
owned: &[String],
path: &Path,
) -> Result<Published, PublishError> {
publish_stored_with(store, key, host, owned, path, &PublishOptions::system())
}
pub fn publish_stored_with<S: Store + ?Sized>(
store: &S,
key: &SigningKey,
host: &str,
owned: &[String],
path: &Path,
opts: &PublishOptions,
) -> Result<Published, PublishError> {
let lines = read_lines(path)?;
let key_from = store
.namespace(host)
.ok()
.flatten()
.and_then(|n| json::parse(&n.keydoc).ok())
.and_then(|d| match d.get("keys") {
Some(Value::Array(ks)) => ks
.iter()
.find(|k| k.get("id").and_then(Value::as_str) == Some(key.key_id()))
.and_then(|k| k.get("valid_from").and_then(Value::as_str).map(str::to_string)),
_ => None,
})
.ok_or_else(|| {
PublishError::at(0, format!("the key {} is not in {host}'s key document in the store", key.key_id()))
})?;
let now = opts.clock.now();
let ctx = Ctx { host, owned, key_from: &key_from, now: &now, base: path.parent().unwrap_or(Path::new(".")) };
let mut planner = Planner {
store,
key,
ctx,
backfill: opts.backfill.is_some(),
sim: Simulation::default(),
ops: Vec::new(),
binds: Vec::new(),
};
planner.run(&lines)?;
let Planner { ops, binds, .. } = planner;
if opts.backfill.is_some() {
backfill_check(store, &binds)?;
}
let (out, written) = write(store, &ops)?;
if let Some(log) = &opts.backfill {
if out.functions + out.classes + out.versions > 0 {
append_log(log, &written).map_err(|e| PublishError::at(0, format!("backfill log: {e}")))?;
}
}
Ok(out)
}