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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2022-2023 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Edge;
use crate::Hex;
use crate::Sodg;
use crate::Vertex;
use anyhow::{anyhow, Context, Result};
use log::trace;
use rstest::rstest;

impl Sodg {
    /// Add a new vertex `v1` to itself.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.add(42).unwrap();
    /// g.bind(0, 42, "hello").unwrap();
    /// ```
    ///
    /// If vertex `v1` already exists in the graph, `Ok` will be returned.
    pub fn add(&mut self, v1: u32) -> Result<()> {
        if self.vertices.contains_key(&v1) {
            return Ok(());
        }
        self.vertices.insert(v1, Vertex::empty());
        self.validate(vec![v1])?;
        trace!("#add(ν{}): new vertex added", v1);
        Ok(())
    }

    /// Make an edge `e1` from vertex `v1` to vertex `v2` and put `a` label on it.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.add(42).unwrap();
    /// g.bind(0, 42, "forward").unwrap();
    /// g.bind(42, 0, "backward").unwrap();
    /// ```
    ///
    /// If an edge with this label already exists, it will be replaced with a new edge.
    ///
    /// If either vertex `v1` or `v2` is absent, an `Err` will be returned.
    ///
    /// If `v1` equals to `v2`, an `Err` will be returned.
    ///
    /// The label `a` can't be empty. If it is empty, an `Err` will be returned.
    pub fn bind(&mut self, v1: u32, v2: u32, a: &str) -> Result<()> {
        let vtx1 = self
            .vertices
            .get_mut(&v1)
            .context(format!("Can't depart from ν{v1}, it's absent"))?;
        vtx1.edges
            .retain(|e| Self::split_a(&e.a).0 != Self::split_a(a).0);
        vtx1.edges.push(Edge::new(v2, a));
        let vtx2 = self
            .vertices
            .get_mut(&v2)
            .context(format!("Can't arrive at ν{v2}, it's absent"))?;
        vtx2.parents.insert(v1);
        self.validate(vec![v1, v2])?;
        trace!("#bind: edge added ν{}.{} → ν{}", v1, a, v2);
        Ok(())
    }

    /// Set vertex data.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Hex;
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(42).unwrap();
    /// g.put(42, Hex::from_str_bytes("hello, world!")).unwrap();
    /// ```
    ///
    /// If vertex `v1` is absent, an `Err` will be returned.
    pub fn put(&mut self, v: u32, d: Hex) -> Result<()> {
        let vtx = self
            .vertices
            .get_mut(&v)
            .context(format!("Can't find ν{v}"))?;
        vtx.data = d.clone();
        vtx.occupied = true;
        self.validate(vec![v])?;
        trace!("#data: data of ν{v} set to {d}");
        Ok(())
    }

    /// Read vertex data, and then submit the vertex to garbage collection.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Hex;
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(42).unwrap();
    /// let data = Hex::from_str_bytes("hello, world!");
    /// g.put(42, data.clone()).unwrap();
    /// assert_eq!(data, g.data(42).unwrap());
    /// #[cfg(feature = "gc")]
    /// assert!(g.is_empty());
    /// ```
    ///
    /// If vertex `v1` is absent, an `Err` will be returned.
    ///
    /// If there is no data, an `Err` will be returned, for example:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(42).unwrap();
    /// assert!(g.data(42).is_err());
    /// ```
    pub fn data(&mut self, v: u32) -> Result<Hex> {
        let vtx = self
            .vertices
            .get_mut(&v)
            .context(format!("Can't find ν{v}"))?;
        if !vtx.occupied {
            return Err(anyhow!("There is no data in ν{v}"));
        }
        let data = vtx.data.clone();
        vtx.taken = true;
        #[cfg(feature = "gc")]
        self.collect(v)?;
        Ok(data)
    }

    /// Find all kids of a vertex.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.add(42).unwrap();
    /// g.bind(0, 42, "k").unwrap();
    /// let (a, tail, to) = g.kids(0).unwrap().first().unwrap().clone();
    /// assert_eq!("k", a);
    /// assert_eq!("", tail);
    /// assert_eq!(42, to);
    /// ```
    ///
    /// If vertex `v1` is absent, `None` will be returned.
    ///
    /// Just in case, if you need to put all names into a single line:
    ///
    /// ```
    /// use itertools::Itertools;
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.add(42).unwrap();
    /// g.bind(0, 42, "a").unwrap();
    /// g.bind(0, 42, "b/d.f.e").unwrap();
    /// g.bind(0, 42, "c/hello-world").unwrap();
    /// assert_eq!("a,b,c", g.kids(0).unwrap().into_iter().map(|(a, _, _)| a).collect::<Vec<String>>().join(","));
    /// ```
    pub fn kids(&self, v: u32) -> Result<Vec<(String, String, u32)>> {
        let vtx = self.vertices.get(&v).context(format!("Can't find ν{v}"))?;
        let kids = vtx
            .edges
            .iter()
            .map(|x| {
                let p = Self::split_a(&x.a);
                (p.0, p.1, x.to)
            })
            .collect();
        Ok(kids)
    }

    /// Find a kid of a vertex, by its edge name, and return the ID of the vertex
    /// found and the locator of the edge.
    ///
    /// For example:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.add(42).unwrap();
    /// g.bind(0, 42, "k/foo").unwrap();
    /// assert_eq!((42, "foo".to_string()), g.kid(0, "k").unwrap());
    /// ```
    ///
    /// If vertex `v1` is absent, `None` will be returned.
    pub fn kid(&self, v: u32, a: &str) -> Option<(u32, String)> {
        if let Some(vtx) = self.vertices.get(&v) {
            vtx.edges
                .iter()
                .find(|e| Self::split_a(&e.a).0 == Self::split_a(a).0)
                .map(|e| (e.to, Self::split_a(&e.a).1))
        } else {
            None
        }
    }

    /// Check whether data is in the vertex.
    ///
    /// With this method you can check whether the data is in the vertex:
    ///
    /// ```
    /// use sodg::{Hex, Sodg};
    /// let mut g = Sodg::empty();
    /// g.add(0).unwrap();
    /// g.put(0, Hex::from(42)).unwrap();
    /// assert!(g.is_full(0).unwrap());
    /// ```
    ///
    /// If the vertex is absent, the method will return `Err`.
    pub fn is_full(&self, v: u32) -> Result<bool> {
        let vtx = self.vertices.get(&v).context(format!("Can't find ν{v}"))?;
        Ok(vtx.occupied)
    }

    /// Split label into two parts.
    pub(crate) fn split_a(a: &str) -> (String, String) {
        let s = a.splitn(2, '/').collect::<Vec<&str>>();
        (
            s.first().unwrap().to_string(),
            s.get(1).unwrap_or(&"").to_string(),
        )
    }
}

#[cfg(test)]
use crate::DeadRelay;

#[rstest]
#[case("hello", "hello", "")]
#[case("hello/", "hello", "")]
#[case("hello/a-1", "hello", "a-1")]
#[case("π/Φ.x.Δ", "π", "Φ.x.Δ")]
fn splits_label_correctly(#[case] a: &str, #[case] head: &str, #[case] tail: &str) {
    let s = Sodg::split_a(a);
    assert_eq!(head, s.0);
    assert_eq!(tail, s.1);
}

#[test]
fn adds_simple_vertex() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    assert_eq!(1, g.find(1, "", &DeadRelay::default())?);
    Ok(())
}

#[test]
fn binds_simple_vertices() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    let k = "hello";
    g.bind(1, 2, k)?;
    assert_eq!(2, g.find(1, k, &DeadRelay::default())?);
    Ok(())
}

#[test]
fn pre_defined_ids() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    let k = "a-привет";
    g.bind(1, 2, k)?;
    assert_eq!(2, g.find(1, k, &DeadRelay::default())?);
    Ok(())
}

#[test]
fn binds_two_names() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    g.bind(1, 2, "first")?;
    g.bind(1, 2, "second")?;
    assert_eq!(2, g.find(1, "first", &DeadRelay::default())?);
    Ok(())
}

#[rstest]
#[case("hello", "hello")]
#[case("hello/a.b.c", "hello")]
#[case("hello", "hello/f.f")]
#[case("hello", "hello/")]
#[case("hello/", "hello")]
fn overwrites_edges(#[case] before: &str, #[case] after: &str) {
    let mut g = Sodg::empty();
    g.alerts_off();
    g.add(1).unwrap();
    g.add(2).unwrap();
    g.bind(1, 2, before).unwrap();
    g.add(3).unwrap();
    g.bind(1, 3, after).unwrap();
    assert_eq!(3, g.kid(1, after).unwrap().0);
    assert_eq!(3, g.kid(1, before).unwrap().0);
}

#[test]
fn overwrites_edge() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    g.bind(1, 2, "foo")?;
    g.add(3)?;
    g.bind(1, 3, "foo/ee")?;
    assert_eq!(3, g.kid(1, "foo").unwrap().0);
    assert_eq!(3, g.kid(1, "foo/ee").unwrap().0);
    Ok(())
}

#[test]
fn binds_to_root() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "x")?;
    assert!(g.kid(0, "ρ").is_none());
    assert!(g.kid(0, "σ").is_none());
    Ok(())
}

#[test]
fn sets_simple_data() -> Result<()> {
    let mut g = Sodg::empty();
    let data = Hex::from_str_bytes("hello");
    g.add(0)?;
    g.put(0, data.clone())?;
    assert_eq!(data, g.data(0)?);
    Ok(())
}

#[test]
#[cfg(feature = "gc")]
fn simple_data_gc() -> Result<()> {
    let mut g = Sodg::empty();
    let data = Hex::from_str_bytes("hello");
    g.add(0)?;
    g.put(0, data.clone())?;
    assert_eq!(data, g.data(0)?);
    assert!(g.is_empty());
    Ok(())
}

#[test]
fn finds_all_kids() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "one/f.d")?;
    g.bind(0, 1, "two")?;
    assert_eq!(2, g.kids(0)?.len());
    let (a, tail, to) = g.kids(0)?.first().unwrap().clone();
    assert_eq!("one", a);
    assert_eq!("f.d", tail);
    assert_eq!(1, to);
    Ok(())
}

#[test]
fn builds_list_of_kids() -> Result<()> {
    let mut g = Sodg::empty();
    g.alerts_off();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "one")?;
    g.bind(0, 1, "two/d.f.hello-world")?;
    g.bind(0, 1, "three/")?;
    let names: Vec<String> = g.kids(0)?.into_iter().map(|(a, _, _)| a).collect();
    assert_eq!("one,two,three", names.join(","));
    Ok(())
}

#[test]
fn gets_data_from_empty_vertex() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    assert!(g.data(0).is_err());
    Ok(())
}

#[test]
fn gets_absent_kid() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    assert!(g.kid(0, "hello").is_none());
    Ok(())
}

#[test]
fn gets_kid_from_absent_vertex() -> Result<()> {
    let g = Sodg::empty();
    assert!(g.kid(0, "hello").is_none());
    Ok(())
}

#[test]
fn finds_kid_by_prefix() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "π/Φ.test")?;
    assert_eq!(1, g.kid(0, "π").unwrap().0);
    Ok(())
}

#[test]
fn finds_kid_and_loc_by_prefix() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "π/foo")?;
    assert_eq!(Some((1, "foo".to_string())), g.kid(0, "π"));
    Ok(())
}

#[test]
fn adds_twice() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    assert!(g.add(0).is_ok());
    Ok(())
}

#[test]
fn replaces_ignoring_locator() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "π/Φ.one")?;
    g.bind(0, 1, "π/Φ.two")?;
    assert_eq!(1, g.kids(0)?.len());
    Ok(())
}

#[test]
fn checks_for_data_presence() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.put(0, Hex::from(42)).unwrap();
    assert!(g.is_full(0).unwrap());
    Ok(())
}

#[test]
fn checks_for_data_absence() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    assert!(!g.is_full(0).unwrap());
    Ok(())
}