use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use super::epoch::GrammarEpoch;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LatentNode {
pub id: String,
pub kind: LatentNodeKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub attrs: BTreeMap<String, String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LatentNodeKind {
Root,
Atom,
Sequence,
Record,
Field,
Null,
Opaque,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LatentEdge {
pub from: String,
pub to: String,
pub rel: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LatentMetadata {
pub epoch: GrammarEpoch,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub composer: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LatentGraph {
pub meta: LatentMetadata,
pub nodes: Vec<LatentNode>,
pub edges: Vec<LatentEdge>,
}
impl LatentGraph {
pub fn node(&self, id: &str) -> Option<&LatentNode> {
self.nodes.iter().find(|n| n.id == id)
}
pub fn to_canonical_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec(self)
}
}