Skip to main content

quilt_save/
lib.rs

1//! quilt-save — .qzt file format, save/load cells
2
3use quilt_cell::Cell;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Qzt {
8    pub version: String,
9    pub kind: String,
10    pub name: String,
11    pub description: String,
12    pub cells: Vec<Cell>,
13    pub edges: Vec<(String, String, f64)>,
14}
15
16impl Qzt {
17    pub fn new(name: &str) -> Self {
18        Self {
19            version: "1.0".to_string(),
20            kind: "quilt-zip-target".to_string(),
21            name: name.to_string(),
22            description: "".to_string(),
23            cells: vec![],
24            edges: vec![],
25        }
26    }
27    pub fn add_cell(&mut self, cell: Cell) { self.cells.push(cell); }
28    pub fn add_edge(&mut self, from: &str, to: &str, weight: f64) {
29        self.edges.push((from.to_string(), to.to_string(), weight));
30    }
31    pub fn save(&self, path: &str) -> std::io::Result<()> {
32        std::fs::write(path, serde_json::to_string_pretty(self).unwrap())
33    }
34    pub fn load(path: &str) -> std::io::Result<Self> {
35        let data = std::fs::read_to_string(path)?;
36        Ok(serde_json::from_str(&data).unwrap_or_else(|_| Qzt::new("loaded")))
37    }
38    pub fn cell_count(&self) -> usize { self.cells.len() }
39    pub fn edge_count(&self) -> usize { self.edges.len() }
40}
41
42pub const MAGIC: &str = "QUILT-ZIP-TARGET";
43pub const VERSION: &str = "1.0";
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    #[test]
49    fn test_qzt() {
50        let mut q = Qzt::new("test");
51        q.add_cell(Cell::new("a"));
52        q.add_cell(Cell::new("b"));
53        q.add_edge("a", "b", 1.0);
54        assert_eq!(q.cell_count(), 2);
55        assert_eq!(q.edge_count(), 1);
56    }
57}