rust_logic_graph/core/
graph.rs1
2use serde::{Serialize, Deserialize};
3use std::collections::HashMap;
4
5use crate::node::NodeType;
6
7#[derive(Debug, Serialize, Deserialize, Clone)]
8pub struct Edge {
9 pub from: String,
10 pub to: String,
11 pub rule: Option<String>,
12}
13
14#[derive(Debug, Serialize, Deserialize, Clone)]
15pub struct GraphDef {
16 pub nodes: HashMap<String, NodeType>,
17 pub edges: Vec<Edge>,
18}
19
20#[derive(Default)]
21pub struct Context {
22 pub data: HashMap<String, serde_json::Value>,
23}
24
25impl Context {
26 pub fn new() -> Self {
27 Self {
28 data: HashMap::new(),
29 }
30 }
31
32 pub fn set(&mut self, key: impl Into<String>, value: serde_json::Value) -> anyhow::Result<()> {
33 self.data.insert(key.into(), value);
34 Ok(())
35 }
36
37 pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
38 self.data.get(key)
39 }
40}
41
42pub struct Graph {
43 pub def: GraphDef,
44 pub context: Context,
45}
46
47impl Graph {
48 pub fn new(def: GraphDef) -> Self {
49 Self {
50 def,
51 context: Context::default(),
52 }
53 }
54}