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
use super::change_id::*;
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct Vertex<H> {
pub change: H,
pub start: ChangePosition,
pub end: ChangePosition,
}
impl Vertex<ChangeId> {
pub const ROOT: Vertex<ChangeId> = Vertex {
change: ChangeId::ROOT,
start: ChangePosition::ROOT,
end: ChangePosition::ROOT,
};
pub(crate) const BOTTOM: Vertex<ChangeId> = Vertex {
change: ChangeId::ROOT,
start: ChangePosition::BOTTOM,
end: ChangePosition::BOTTOM,
};
pub fn is_root(&self) -> bool {
self == &Vertex::ROOT
}
pub(crate) fn to_option(&self) -> Vertex<Option<ChangeId>> {
Vertex {
change: Some(self.change),
start: self.start,
end: self.end,
}
}
}
impl<H: Clone> Vertex<H> {
pub fn start_pos(&self) -> Position<H> {
Position {
change: self.change.clone(),
pos: self.start,
}
}
pub fn end_pos(&self) -> Position<H> {
Position {
change: self.change.clone(),
pos: self.end,
}
}
pub fn len(&self) -> usize {
self.end - self.start
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct ChangePosition(pub u64);
impl ChangePosition {
pub(crate) const ROOT: ChangePosition = ChangePosition(0);
pub(crate) const BOTTOM: ChangePosition = ChangePosition(1);
}
impl std::ops::Add<usize> for ChangePosition {
type Output = ChangePosition;
fn add(self, x: usize) -> Self::Output {
ChangePosition(self.0 + x as u64)
}
}
impl std::ops::Sub<ChangePosition> for ChangePosition {
type Output = usize;
fn sub(self, x: ChangePosition) -> Self::Output {
(self.0 - x.0) as usize
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
#[doc(hidden)]
pub struct Position<P> {
pub change: P,
pub pos: ChangePosition,
}
use super::Base32;
use byteorder::{ByteOrder, LittleEndian};
impl<H: super::Base32> Base32 for Position<H> {
fn to_base32(&self) -> String {
let mut v = self.change.to_base32();
let mut bytes = [0; 8];
LittleEndian::write_u64(&mut bytes, self.pos.0);
let mut i = 7;
while i > 2 && bytes[i] == 0 {
i -= 1
}
i += 1;
let len = data_encoding::BASE32_NOPAD.encode_len(i);
let len0 = v.len() + 1;
v.push_str("..............");
v.truncate(len0 + len);
data_encoding::BASE32_NOPAD.encode_mut(&bytes[..i], unsafe {
v.split_at_mut(len0).1.as_bytes_mut()
});
v
}
fn from_base32(s: &[u8]) -> Option<Self> {
let n = if let Some(n) = s.iter().position(|c| *c == b'.') {
n
} else {
return None;
};
let (s, pos) = s.split_at(n);
let pos = &pos[1..];
let change = if let Some(change) = H::from_base32(s) {
change
} else {
return None;
};
let mut dec = [0; 8];
let len = if let Ok(len) = data_encoding::BASE32_NOPAD.decode_len(pos.len()) {
len
} else {
return None;
};
let pos = if let Ok(_) = data_encoding::BASE32_NOPAD.decode_mut(pos, &mut dec[..len]) {
LittleEndian::read_u64(&dec)
} else {
return None;
};
Some(Position {
change,
pos: ChangePosition(pos),
})
}
}
impl<H> std::ops::Add<usize> for Position<H> {
type Output = Position<H>;
fn add(self, x: usize) -> Self::Output {
Position {
change: self.change,
pos: self.pos + x,
}
}
}
impl Position<ChangeId> {
pub fn inode_vertex(&self) -> Vertex<ChangeId> {
Vertex {
change: self.change,
start: self.pos,
end: self.pos,
}
}
pub fn is_root(&self) -> bool {
self.change.is_root()
}
pub(crate) fn to_option(&self) -> Position<Option<ChangeId>> {
Position {
change: Some(self.change),
pos: self.pos,
}
}
pub const ROOT: Position<ChangeId> = Position {
change: ChangeId::ROOT,
pos: ChangePosition(0),
};
pub(crate) const OPTION_ROOT: Position<Option<ChangeId>> = Position {
change: Some(ChangeId::ROOT),
pos: ChangePosition(0),
};
pub(crate) const BOTTOM: Position<ChangeId> = Position {
change: ChangeId::ROOT,
pos: ChangePosition::BOTTOM,
};
}