Skip to main content

quilt_zin/
lib.rs

1//! quilt-zin — the Z_in primitive of a Quilt cell.
2//! 
3//! Z_in is the input space. What the cell receives.
4//! A cell is a system. The system has 8 parts. The 8 parts are the 8 primitives.
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10pub struct Zin {
11    pub data: HashMap<String, f64>,
12}
13
14impl Zin {
15    pub fn new() -> Self { Self::default() }
16    pub fn insert(mut self, k: &str, v: f64) -> Self {
17        self.data.insert(k.to_string(), v);
18        self
19    }
20    pub fn put(&mut self, k: &str, v: f64) {
21        self.data.insert(k.to_string(), v);
22    }
23    pub fn get(&self, k: &str) -> Option<f64> {
24        self.data.get(k).copied()
25    }
26    pub fn keys(&self) -> impl Iterator<Item = &String> {
27        self.data.keys()
28    }
29    pub fn len(&self) -> usize { self.data.len() }
30    pub fn is_empty(&self) -> bool { self.data.is_empty() }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    #[test]
37    fn test_zin_basic() {
38        let z = Zin::new().insert("x", 1.0).insert("y", 2.0);
39        assert_eq!(z.get("x"), Some(1.0));
40        assert_eq!(z.get("y"), Some(2.0));
41        assert_eq!(z.len(), 2);
42    }
43}