sot/
sot.rs

1// Copyright (c) 2022 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use serde::{Deserialize, Serialize};
22use std::collections::HashMap;
23use std::fmt;
24
25mod inconsistencies;
26mod inspect;
27mod merge;
28mod ops;
29mod parse;
30mod serialization;
31mod slice;
32mod xml;
33
34#[derive(Clone, Serialize, Deserialize, Eq, PartialOrd, PartialEq, Ord)]
35struct Edge {
36    to: u32,
37    a: String,
38}
39
40impl Edge {
41    fn new(to: u32, a: String) -> Edge {
42        Edge { to, a }
43    }
44}
45
46#[derive(Clone, Serialize, Deserialize)]
47struct Vertex {
48    edges: Vec<Edge>,
49    data: Vec<u8>,
50}
51
52impl Vertex {
53    /// Make an empty one.
54    pub fn empty() -> Self {
55        Vertex {
56            edges: vec![],
57            data: vec![],
58        }
59    }
60}
61
62#[derive(Serialize, Deserialize)]
63pub struct Sot {
64    vertices: HashMap<u32, Vertex>,
65}
66
67impl fmt::Debug for Sot {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        let mut lines = vec![];
70        for (i, v) in self.vertices.iter() {
71            let mut attrs = v
72                .edges
73                .iter()
74                .map(|e| format!("\n\t{} ➞ ν{}", e.a, e.to))
75                .collect::<Vec<String>>();
76            if !&v.data.is_empty() {
77                attrs.push(format!("{}b", v.data.len()));
78            }
79            lines.push(format!("ν{} -> ⟦{}⟧", i, attrs.join(", ")));
80        }
81        f.write_str(lines.join("\n").as_str())
82    }
83}
84
85impl Sot {
86    /// Makes an empty Sot, with no vertices and no edges.
87    pub fn empty() -> Self {
88        Sot {
89            vertices: HashMap::new(),
90        }
91    }
92
93    /// Get max ID of a vertex.
94    pub fn max(&self) -> u32 {
95        let mut id = 0;
96        for v in self.vertices.keys() {
97            if *v > id {
98                id = *v;
99            }
100        }
101        id
102    }
103}
104
105#[cfg(test)]
106use anyhow::Result;
107
108#[test]
109fn makes_an_empty_sot() -> Result<()> {
110    let mut sot = Sot::empty();
111    sot.add(0)?;
112    assert_eq!(1, sot.vertices.len());
113    Ok(())
114}
115
116#[test]
117fn calculates_max() -> Result<()> {
118    let mut sot = Sot::empty();
119    sot.add(0)?;
120    sot.add(1)?;
121    assert_eq!(1, sot.max());
122    Ok(())
123}