gml_parser/
gml.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*!
Object types that represent a parsed GML graph.
*/

use std::borrow::Cow;
use std::collections::HashMap;

/// An item that represents a key-value pair. For example, `node [ ... ]`, `directed 0`,
/// `label "abc"`, etc.
#[derive(Debug, Clone, PartialEq)]
pub enum GmlItem<'a> {
    Node(Node<'a>),
    Edge(Edge<'a>),
    Directed(bool),
    KeyValue((Cow<'a, str>, Value<'a>)),
}

impl<'a> GmlItem<'a> {
    /// Convert any borrowed references to owned values.
    pub fn upgrade_to_owned(&self) -> GmlItem<'static> {
        match self {
            Self::Node(node) => GmlItem::Node(node.upgrade_to_owned()),
            Self::Edge(edge) => GmlItem::Edge(edge.upgrade_to_owned()),
            Self::Directed(directed) => GmlItem::Directed(*directed),
            Self::KeyValue((name, value)) => GmlItem::KeyValue((
                Cow::Owned(name.clone().into_owned()),
                value.upgrade_to_owned(),
            )),
        }
    }
}

/// A graph node with an `id` and `other` key-value pairs.
#[derive(Debug, Clone, PartialEq)]
pub struct Node<'a> {
    pub id: Option<u32>,
    pub other: HashMap<Cow<'a, str>, Value<'a>>,
}

impl<'a> Node<'a> {
    pub fn new<K>(id: Option<u32>, other: HashMap<K, Value<'a>>) -> Self
    where
        K: Into<Cow<'a, str>>,
    {
        let other = other.into_iter().map(|(k, v)| (k.into(), v)).collect();
        Self { id, other }
    }

    /// Convert any borrowed references to owned values.
    pub fn upgrade_to_owned(&self) -> Node<'static> {
        Node {
            id: self.id,
            other: self
                .other
                .iter()
                .map(|(k, v)| (Cow::Owned(k.clone().into_owned()), v.upgrade_to_owned()))
                .collect(),
        }
    }
}

/// A graph edge from node `source` to node `target` with `other` key-value pairs.
#[derive(Debug, Clone, PartialEq)]
pub struct Edge<'a> {
    pub source: u32,
    pub target: u32,
    pub other: HashMap<Cow<'a, str>, Value<'a>>,
}

impl<'a> Edge<'a> {
    pub fn new<K>(source: u32, target: u32, other: HashMap<K, Value<'a>>) -> Self
    where
        K: Into<Cow<'a, str>>,
    {
        let other = other.into_iter().map(|(k, v)| (k.into(), v)).collect();
        Self {
            source,
            target,
            other,
        }
    }

    /// Convert any borrowed references to owned values.
    pub fn upgrade_to_owned(&self) -> Edge<'static> {
        Edge {
            source: self.source,
            target: self.target,
            other: self
                .other
                .iter()
                .map(|(k, v)| (Cow::Owned(k.clone().into_owned()), v.upgrade_to_owned()))
                .collect(),
        }
    }
}

/// The base value types supported by GML.
#[derive(Debug, Clone, PartialEq)]
pub enum Value<'a> {
    Int(i32),
    Float(f32),
    Str(Cow<'a, str>),
}

impl<'a> Value<'a> {
    /// Returns a string if the value is a string. Otherwise returns `None`.
    pub fn as_str(self) -> Option<Cow<'a, str>> {
        if let Self::Str(s) = self {
            return Some(s);
        }
        None
    }

    /// Returns a float if the value is a float. Otherwise returns `None`.
    pub fn as_float(self) -> Option<f32> {
        if let Self::Float(f) = self {
            return Some(f);
        }
        None
    }

    /// Convert any borrowed references to owned values.
    pub fn upgrade_to_owned(&self) -> Value<'static> {
        match self {
            Self::Int(x) => Value::Int(*x),
            Self::Float(x) => Value::Float(*x),
            Self::Str(s) => Value::Str(Cow::Owned(s.clone().into_owned())),
        }
    }
}

/// A GML graph.
#[derive(Debug, PartialEq)]
pub struct Gml<'a> {
    pub directed: bool,
    pub nodes: Vec<Node<'a>>,
    pub edges: Vec<Edge<'a>>,
    pub other: HashMap<Cow<'a, str>, Value<'a>>,
}

impl<'a> Gml<'a> {
    /// Convert any borrowed references to owned values.
    pub fn upgrade_to_owned(&self) -> Gml<'static> {
        Gml {
            directed: self.directed,
            nodes: self.nodes.iter().map(|n| n.upgrade_to_owned()).collect(),
            edges: self.edges.iter().map(|e| e.upgrade_to_owned()).collect(),
            other: self
                .other
                .iter()
                .map(|(k, v)| (Cow::Owned(k.clone().into_owned()), v.upgrade_to_owned()))
                .collect(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn upgrade_to_owned() {
        let node;
        {
            // a string with a short lifetime
            let local_str = "abc".to_string();

            let mut node_options = HashMap::new();
            node_options.insert(&local_str, Value::Int(5));
            let node_with_reference = Node::new(Some(0), node_options);

            node = node_with_reference.upgrade_to_owned();
        }

        println!("{:?}", node);
    }
}