torg_serde/lib.rs
1//! Serialization/deserialization for TØR-G graphs.
2//!
3//! This crate provides encoding and decoding of TØR-G graphs
4//! to various formats:
5//!
6//! - **Binary**: Compact format (~7 bytes per node) for storage and transmission
7//! - **JSON**: Human-readable format for debugging and inspection
8//!
9//! # Binary Format
10//!
11//! The binary format is designed for compactness and fast parsing:
12//!
13//! ```text
14//! Header: [0x54, 0x47] "TG" magic + version:u8 + flags:u8
15//! Inputs: count:u16 + [id:u16; count]
16//! Nodes: count:u16 + [Node; count]
17//! Outputs: count:u16 + [id:u16; count]
18//! ```
19//!
20//! # Examples
21//!
22//! ```
23//! use torg_core::{Graph, Node, BoolOp, Source};
24//! use torg_serde::{to_bytes, from_bytes, to_json, from_json};
25//!
26//! // Create a simple graph
27//! let graph = Graph {
28//! inputs: vec![0, 1],
29//! nodes: vec![Node::new(2, BoolOp::Or, Source::Id(0), Source::Id(1))],
30//! outputs: vec![2],
31//! };
32//!
33//! // Binary serialization
34//! let bytes = to_bytes(&graph);
35//! let restored = from_bytes(&bytes).unwrap();
36//! assert_eq!(graph, restored);
37//!
38//! // JSON serialization
39//! let json = to_json(&graph);
40//! let restored = from_json(&json).unwrap();
41//! assert_eq!(graph, restored);
42//! ```
43
44mod binary;
45mod error;
46mod json;
47
48pub use binary::{from_bytes, to_bytes};
49pub use error::SerdeError;
50pub use json::{from_json, to_json, to_json_compact};
51
52// Re-export torg_core for convenience
53pub use torg_core;