use super::*;
#[derive(Debug, Clone)]
pub struct CreateArgs {
pub title: String,
pub mem: String,
pub entity_type: String,
pub sections: IndexMap<String, String>,
pub metadata: IndexMap<String, String>,
pub relations: Vec<RelateArg>,
pub dry_run: bool,
}
#[derive(Debug, Clone)]
pub struct UpdateArgs {
pub id: EntityId,
pub expected_hash: String,
pub sections: IndexMap<String, String>,
pub append_sections: IndexMap<String, String>,
pub patch_sections: IndexMap<String, PatchArg>,
pub metadata: IndexMap<String, String>,
pub metadata_unset: Vec<String>,
pub dry_run: bool,
}
#[derive(Debug, Clone)]
pub struct PatchArg {
pub old: String,
pub new: String,
pub all: bool,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ModifiedSections {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub replaced: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub appended: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub patched: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub unset: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ModifiedMetadata {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub set: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub unset: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct UpdateResult {
pub id: EntityId,
pub title: String,
pub modified_sections: ModifiedSections,
pub modified_metadata: ModifiedMetadata,
pub modified_date: String,
#[serde(rename = "_hash")]
pub content_hash: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub prospective_hash: Option<String>,
#[serde(default)]
pub write_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<WarningHint>,
}
pub fn envelope(
code: &str,
message: impl Into<String>,
details: serde_json::Value,
) -> serde_json::Value {
serde_json::json!({
"code": code,
"message": message.into(),
"details": details,
})
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateResult {
pub id: EntityId,
pub title: String,
pub mem: String,
pub file_path: String,
pub created_date: String,
#[serde(rename = "_hash")]
pub content_hash: String,
#[serde(default)]
pub write_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<WarningHint>,
#[serde(default)]
pub type_guidance: std::collections::BTreeMap<String, Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub incoming_count: Option<usize>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub incoming: Vec<IncomingRef>,
}
#[derive(Debug, Clone, Serialize)]
pub struct IncomingRef {
pub from: EntityId,
pub rel_type: String,
pub source: String,
}
pub fn project_incoming(edges: &[crate::store::InEdge]) -> Vec<IncomingRef> {
let mut out: Vec<IncomingRef> = edges
.iter()
.map(|e| IncomingRef {
from: e.from.clone(),
rel_type: e.rel_type.clone(),
source: match e.source {
crate::store::EdgeSource::Explicit => "explicit",
crate::store::EdgeSource::Hierarchy => "hierarchy",
crate::store::EdgeSource::BodyLink => "body_link",
}
.to_string(),
})
.collect();
out.sort_by(|a, b| a.rel_type.cmp(&b.rel_type).then(a.from.0.cmp(&b.from.0)));
out
}
#[derive(Debug, Clone, Serialize)]
pub struct DeleteResult {
pub id: EntityId,
pub relations_removed: usize,
#[serde(default)]
pub write_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub orphan_stubs_removed: Vec<EntityId>,
}
#[derive(Debug, Clone, Serialize)]
pub struct RenameResult {
pub old_id: EntityId,
pub new_id: EntityId,
pub old_path: String,
pub new_path: String,
#[serde(default, rename = "_hash", skip_serializing_if = "String::is_empty")]
pub content_hash: String,
#[serde(default)]
pub write_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<WarningHint>,
}
#[derive(Debug, Clone)]
pub struct RelateArg {
pub target: EntityId,
pub rel_type: String,
pub description: Option<String>,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct RelationUnsetArg {
pub rel_type: String,
pub target: EntityId,
}
#[derive(Debug, Clone, Serialize)]
pub struct RelateResult {
pub from: EntityId,
pub to: EntityId,
pub rel_type: String,
pub source: String,
#[serde(default, rename = "_hash", skip_serializing_if = "String::is_empty")]
pub content_hash: String,
#[serde(default)]
pub write_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<WarningHint>,
#[serde(skip)]
pub disk_changed: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub orphan_stubs_removed: Vec<EntityId>,
}
fn is_zero(n: &usize) -> bool {
*n == 0
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchResult {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<WarningHint>,
pub applied: bool,
pub results: Vec<BatchEntry>,
pub succeeded: usize,
#[serde(default, skip_serializing_if = "is_zero")]
pub errors_suppressed: usize,
pub failed: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub orphan_stubs_removed: Vec<EntityId>,
#[serde(default)]
pub write_id: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchEntry {
pub id: EntityId,
pub action: String,
pub error: Option<BatchError>,
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchError {
pub code: String,
pub message: String,
pub details: serde_json::Value,
}
#[derive(Debug, Clone, Serialize)]
pub struct RelateEcho {
pub from: String,
pub to: String,
pub rel_type: String,
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("batch refused: {failed} of {total} operation(s) failed, nothing committed{first}")]
pub struct BatchRefused {
failed: usize,
total: usize,
first: String,
details: serde_json::Value,
}
impl BatchRefused {
pub fn code(&self) -> &'static str {
"BATCH_REFUSED"
}
pub fn details(&self) -> serde_json::Value {
self.details.clone()
}
}
impl BatchResult {
pub fn refusal(&self, relations: &[RelateEcho]) -> BatchRefused {
let entries: Vec<serde_json::Value> = self
.results
.iter()
.enumerate()
.map(|(i, entry)| {
let mut e = serde_json::json!({
"index": i,
"id": entry.id,
"action": entry.action,
});
if let Some(r) = relations.get(i) {
e["from"] = serde_json::json!(r.from);
e["to"] = serde_json::json!(r.to);
e["rel_type"] = serde_json::json!(r.rel_type);
}
if let Some(err) = &entry.error {
e["code"] = serde_json::json!(err.code);
e["message"] = serde_json::json!(err.message);
e["details"] = err.details.clone();
}
e
})
.collect();
let mut details = serde_json::json!({
"applied": self.applied,
"entries": entries,
"failed": self.failed,
"succeeded": self.succeeded,
"errors_suppressed": self.errors_suppressed,
});
if !self.warnings.is_empty() {
details["warnings"] = serde_json::json!(self.warnings);
}
if !self.orphan_stubs_removed.is_empty() {
details["orphan_stubs_removed"] = serde_json::json!(self.orphan_stubs_removed);
}
let first = self
.results
.iter()
.find_map(|e| e.error.as_ref().map(|err| (e, err)))
.map(|(e, err)| {
format!(
"; first failure [{}] on `{}`: {}",
err.code, e.id, err.message
)
})
.unwrap_or_default();
BatchRefused {
failed: self.failed,
total: self.results.len(),
first,
details,
}
}
}