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
use std::future::Future;
use serde::Deserialize;
use serde::Serialize;
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Serialize, Deserialize)]
pub struct Location<Name> {
pub descendant: Name,
pub distance: u64,
}
impl<Name> Location<Name> {
pub fn new(descendant: Name, distance: u64) -> Self {
Self {
descendant,
distance,
}
}
pub fn map_descendant<T, F>(self, f: F) -> Location<T>
where
F: FnOnce(Name) -> T,
{
let new_name = f(self.descendant);
Location::new(new_name, self.distance)
}
pub fn try_map_descendant<T, E, F>(self, f: F) -> Result<Location<T>, E>
where
F: FnOnce(Name) -> Result<T, E>,
{
let new_name = f(self.descendant)?;
Ok(Location::new(new_name, self.distance))
}
pub async fn then_descendant<T, Fut, F>(self, f: F) -> Location<T>
where
F: FnOnce(Name) -> Fut,
Fut: Future<Output = T>,
{
let new_name = f(self.descendant).await;
Location::new(new_name, self.distance)
}
pub async fn and_then_descendant<T, E, Fut, F>(self, f: F) -> Result<Location<T>, E>
where
F: FnOnce(Name) -> Fut,
Fut: Future<Output = Result<T, E>>,
{
let new_name = f(self.descendant).await?;
Ok(Location::new(new_name, self.distance))
}
pub fn with_descendant<T>(self, descendant: T) -> Location<T> {
Location::new(descendant, self.distance)
}
}
#[cfg(any(test, feature = "for-tests"))]
use quickcheck::Arbitrary;
#[cfg(any(test, feature = "for-tests"))]
use quickcheck::Gen;
#[cfg(any(test, feature = "for-tests"))]
impl<Name> Arbitrary for Location<Name>
where
Name: Arbitrary,
{
fn arbitrary(g: &mut Gen) -> Self {
Location {
descendant: Name::arbitrary(g),
distance: u64::arbitrary(g),
}
}
}