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,
}