Skip to main content

graph_grammar/
types.rs

1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3use serde_json::{Map, Value};
4
5// ============================================================================
6// Data model ,serde structs mirroring packages/graph-grammar/src/types.ts.
7// `props` use serde_json::Value for lossless JSON fidelity (PropValue =
8// string | number | boolean | null).
9// ============================================================================
10
11pub(crate) type Props = Map<String, Value>;
12
13fn default_true() -> bool {
14    true
15}
16
17#[derive(Clone, Serialize, Deserialize)]
18pub(crate) struct GNode {
19    pub(crate) id: String,
20    pub(crate) label: String,
21    #[serde(default)]
22    pub(crate) props: Props,
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub(crate) x: Option<f64>,
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub(crate) y: Option<f64>,
27}
28
29#[derive(Clone, Serialize, Deserialize)]
30pub(crate) struct GEdge {
31    pub(crate) id: String,
32    pub(crate) source: String,
33    pub(crate) target: String,
34    #[serde(default)]
35    pub(crate) label: String,
36    #[serde(default)]
37    pub(crate) props: Props,
38    #[serde(default = "default_true")]
39    pub(crate) directed: bool,
40}
41
42#[derive(Clone, Serialize, Deserialize, Default)]
43pub(crate) struct Graph {
44    #[serde(default)]
45    pub(crate) nodes: Vec<GNode>,
46    #[serde(default)]
47    pub(crate) edges: Vec<GEdge>,
48}
49
50#[derive(Clone, Deserialize)]
51pub(crate) struct PropPredicate {
52    pub(crate) key: String,
53    pub(crate) op: String,
54    #[serde(default)]
55    pub(crate) value: Option<Value>,
56}
57
58#[derive(Clone, Deserialize)]
59pub(crate) struct PatternNode {
60    pub(crate) id: String,
61    #[serde(default)]
62    pub(crate) label: String,
63    #[serde(default)]
64    pub(crate) wildcard: bool,
65    #[serde(default)]
66    pub(crate) predicates: Vec<PropPredicate>,
67    #[serde(default, rename = "exactDegree")]
68    pub(crate) exact_degree: Option<f64>,
69}
70
71#[derive(Clone, Deserialize)]
72pub(crate) struct PatternEdge {
73    pub(crate) id: String,
74    pub(crate) source: String,
75    pub(crate) target: String,
76    #[serde(default)]
77    pub(crate) label: String,
78    #[serde(default)]
79    pub(crate) predicates: Vec<PropPredicate>,
80    #[serde(default)]
81    pub(crate) directed: bool,
82    #[serde(default)]
83    pub(crate) wildcard: bool,
84    #[serde(default, rename = "anyDirection")]
85    pub(crate) any_direction: bool,
86}
87
88#[derive(Clone, Deserialize)]
89pub(crate) struct PatternGraph {
90    #[serde(default)]
91    pub(crate) nodes: Vec<PatternNode>,
92    #[serde(default)]
93    pub(crate) edges: Vec<PatternEdge>,
94}
95
96#[derive(Clone, Deserialize)]
97#[serde(tag = "kind", rename_all = "camelCase")]
98#[allow(dead_code)] // RandInt/RandFloat min/max are parsed but rejected in the slice
99pub(crate) enum PropExpr {
100    Literal { value: Value },
101    Copy { from: String, key: String },
102    Increment { from: String, key: String, by: f64 },
103    Counter,
104    RandInt { min: f64, max: f64 },
105    RandFloat { min: f64, max: f64 },
106}
107
108#[derive(Clone, Deserialize)]
109pub(crate) struct RhsNode {
110    pub(crate) id: String,
111    #[serde(default)]
112    pub(crate) label: String,
113    #[serde(default)]
114    pub(crate) props: Props,
115    #[serde(default)]
116    pub(crate) x: Option<f64>,
117    #[serde(default)]
118    pub(crate) y: Option<f64>,
119    #[serde(default, rename = "mapFrom")]
120    pub(crate) map_from: Option<String>,
121    #[serde(default, rename = "setProps")]
122    pub(crate) set_props: Option<IndexMap<String, PropExpr>>,
123}
124
125#[derive(Clone, Deserialize)]
126pub(crate) struct RhsEdge {
127    #[allow(dead_code)] // present for schema fidelity; new edge ids are generated
128    pub(crate) id: String,
129    pub(crate) source: String,
130    pub(crate) target: String,
131    #[serde(default)]
132    pub(crate) label: String,
133    #[serde(default)]
134    pub(crate) props: Props,
135    #[serde(default = "default_true")]
136    pub(crate) directed: bool,
137    #[serde(default, rename = "mapFrom")]
138    pub(crate) map_from: Option<String>,
139    #[serde(default, rename = "setProps")]
140    pub(crate) set_props: Option<IndexMap<String, PropExpr>>,
141}
142
143#[derive(Clone, Deserialize)]
144pub(crate) struct RhsGraph {
145    #[serde(default)]
146    pub(crate) nodes: Vec<RhsNode>,
147    #[serde(default)]
148    pub(crate) edges: Vec<RhsEdge>,
149}
150
151#[derive(Clone, Deserialize)]
152pub(crate) struct EmbeddingRule {
153    #[serde(rename = "lhsNodeId")]
154    pub(crate) lhs_node_id: String,
155    pub(crate) strategy: String,
156    #[serde(default, rename = "targetRhsNodeId")]
157    pub(crate) target_rhs_node_id: Option<String>,
158    #[serde(default, rename = "edgeLabelFilter")]
159    pub(crate) edge_label_filter: Option<String>,
160    #[serde(default, rename = "newEdgeLabel")]
161    pub(crate) new_edge_label: Option<String>,
162}
163
164fn one_f64() -> f64 {
165    1.0
166}
167
168/// A rule. Fields the standalone rewrite ignores (weight/probability/…) are used
169/// by the [`Engine`]. Unknown fields (morphism, color, …) are dropped by serde.
170#[derive(Clone, Deserialize)]
171pub(crate) struct Rule {
172    #[serde(default)]
173    pub(crate) id: String,
174    #[serde(default = "default_true")]
175    pub(crate) enabled: bool,
176    #[serde(default = "one_f64")]
177    pub(crate) weight: f64,
178    #[serde(default = "one_f64")]
179    pub(crate) probability: f64,
180    #[serde(default)]
181    pub(crate) priority: f64,
182    #[serde(default, rename = "maxApplications")]
183    pub(crate) max_applications: i64,
184    pub(crate) lhs: PatternGraph,
185    pub(crate) rhs: RhsGraph,
186    #[serde(default)]
187    pub(crate) embedding: Vec<EmbeddingRule>,
188    #[serde(default)]
189    pub(crate) nac: Vec<PatternGraph>,
190}
191
192#[derive(Clone, Deserialize)]
193pub(crate) struct GrammarConfig {
194    #[serde(default)]
195    pub(crate) strategy: String,
196    #[serde(default)]
197    pub(crate) seed: i64,
198    #[serde(default, rename = "maxSteps")]
199    pub(crate) max_steps: i64,
200    #[serde(default, rename = "maxNodes")]
201    pub(crate) max_nodes: i64,
202}
203
204#[derive(Clone, Deserialize)]
205pub(crate) struct Grammar {
206    #[serde(default)]
207    pub(crate) rules: Vec<Rule>,
208    pub(crate) config: GrammarConfig,
209    #[serde(default)]
210    pub(crate) start: Graph,
211}
212
213#[derive(Serialize)]
214pub(crate) struct ApplyEnvelope {
215    pub(crate) applied: bool,
216    pub(crate) graph: Graph,
217    #[serde(rename = "createdNodes")]
218    pub(crate) created_nodes: Vec<String>,
219    #[serde(rename = "createdEdges")]
220    pub(crate) created_edges: Vec<String>,
221    #[serde(rename = "deletedNodes")]
222    pub(crate) deleted_nodes: Vec<String>,
223    #[serde(rename = "deletedEdges")]
224    pub(crate) deleted_edges: Vec<String>,
225}
226
227pub(crate) struct MatchResult {
228    // Ordered (compiled binding order) to mirror the engine's JS-object nodeMap:
229    // dangling-edge collection and node deletion iterate it, so the order affects
230    // the resulting node/edge insertion order when elements are created/deleted.
231    pub(crate) node_map: IndexMap<String, String>, // LHS node id -> host node id
232    pub(crate) edge_map: IndexMap<String, String>, // LHS edge id -> host edge id
233}
234
235/// Error carried back across the FFI boundary as `{ "error": { code, detail } }`.
236pub struct ApiError {
237    pub code: i32,
238    pub detail: String,
239}