use std::collections::HashMap;
use loro::LoroValue;
use nodedb_types::document::Document;
use nodedb_types::error::{NodeDbError, NodeDbResult};
use nodedb_types::id::{EdgeId, NodeId};
use nodedb_types::value::Value;
use crate::nodedb::convert::value_to_loro;
pub(crate) fn edge_crdt_collection(collection: &str) -> String {
format!("__edges__{collection}")
}
pub(crate) fn edge_id_for(from: &NodeId, to: &NodeId, edge_type: &str) -> NodeDbResult<EdgeId> {
EdgeId::try_first(from.clone(), to.clone(), edge_type).map_err(|e| {
NodeDbError::storage(format!("edge_store: invalid edge label '{edge_type}': {e}"))
})
}
pub(crate) fn edge_crdt_fields<'a>(
from: &'a NodeId,
to: &'a NodeId,
edge_type: &'a str,
properties: &'a Option<Document>,
) -> Vec<(&'a str, LoroValue)> {
let mut fields: Vec<(&str, LoroValue)> = vec![
("src", LoroValue::String(from.as_str().into())),
("dst", LoroValue::String(to.as_str().into())),
("label", LoroValue::String(edge_type.into())),
];
if let Some(props) = properties {
for (k, v) in &props.fields {
fields.push((k.as_str(), value_to_loro(v)));
}
}
fields
}
pub(crate) fn edge_history_value(
from: &NodeId,
to: &NodeId,
edge_type: &str,
properties: &Option<Document>,
) -> Value {
let mut m: HashMap<String, Value> = HashMap::new();
m.insert("src".to_string(), Value::String(from.as_str().to_string()));
m.insert("dst".to_string(), Value::String(to.as_str().to_string()));
m.insert("label".to_string(), Value::String(edge_type.to_string()));
if let Some(props) = properties {
for (k, v) in &props.fields {
m.insert(k.clone(), v.clone());
}
}
Value::Object(m)
}