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
use std::hash::{Hash, Hasher};
use std::fmt::{self, Debug, Display};

use crate::graph::{Graph, Rope, Fork, NodeId, Node, Range};

impl<T> From<Fork> for Node<T> {
    fn from(fork: Fork) -> Self {
        Node::Fork(fork)
    }
}
impl<T> From<Rope> for Node<T> {
    fn from(rope: Rope) -> Self {
        Node::Rope(rope)
    }
}

fn is_ascii(byte: u8) -> bool {
    (byte >= 0x20) & (byte < 0x7F)
}

impl Hash for Fork {
    fn hash<H: Hasher>(&self, state: &mut H) {
        for branch in self.branches() {
            branch.hash(state);
        }
        self.miss.hash(state);
    }
}

impl<T> Hash for Node<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Node::Rope(rope) => {
                b"ROPE".hash(state);
                rope.hash(state);
            },
            Node::Fork(fork) => {
                b"FORK".hash(state);
                fork.hash(state);
            },
            Node::Leaf(_) => b"LEAF".hash(state),
        }
    }
}

impl Debug for NodeId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Debug::fmt(&self.0, f)
    }
}

/// We don't need debug impls in release builds
// #[cfg(test)]
mod debug {
    use super::*;
    use crate::graph::rope::Miss;
    use crate::graph::Disambiguate;
    use std::cmp::{Ord, Ordering};

    impl Disambiguate for &str {
        fn cmp(left: &&str, right: &&str) -> Ordering {
            Ord::cmp(left, right)
        }
    }

    impl Debug for Range {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let Range { start, end } = *self;

            if start != end || !is_ascii(start) {
                f.write_str("[")?;
            }
            match is_ascii(start) {
                true => write!(f, "{}", start as char),
                false => write!(f, "{:02X}", start),
            }?;
            if start != end {
                match is_ascii(end) {
                    true => write!(f, "-{}]", end as char),
                    false => write!(f, "-{:02X}]", end),
                }?;
            } else if !is_ascii(start) {
                f.write_str("]")?;
            }
            Ok(())
        }
    }

    impl Display for Range {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            <Range as Debug>::fmt(self, f)
        }
    }

    impl<T: Debug> Debug for Graph<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let entries = self
                .nodes()
                .iter()
                .enumerate()
                .filter_map(|(i, n)| n.as_ref().map(|n| (i, n)));

            f.debug_map().entries(entries).finish()
        }
    }

    struct Arm<T, U>(T, U);

    impl<T, U> Debug for Arm<T, U>
    where
        T: Display,
        U: Display,
    {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{} ⇒ {}", self.0, self.1)
        }
    }

    impl Debug for Fork {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let mut list = f.debug_set();

            for (range, then) in self.branches() {
                list.entry(&Arm(range, then));
            }
            if let Some(id) = self.miss {
                list.entry(&Arm('_', id));
            }

            list.finish()
        }
    }

    impl Display for Miss {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Miss::First(id) => Display::fmt(id, f),
                Miss::Any(id) => write!(f, "{}*", id),
                Miss::None => f.write_str("n/a"),
            }
        }
    }

    impl Debug for Rope {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            use std::fmt::Write;

            let mut rope = String::with_capacity(self.pattern.len());
            for range in self.pattern.iter() {
                write!(rope, "{}", range)?;
            }

            match self.miss.is_none() {
                false => {
                    let mut list = f.debug_list();

                    list.entry(&Arm(rope, self.then));
                    list.entry(&Arm('_', self.miss));

                    list.finish()
                },
                true => Arm(rope, self.then).fmt(f),
            }
        }
    }

    impl PartialEq for Fork {
        fn eq(&self, other: &Self) -> bool {
            self.miss == other.miss && self.branches().eq(other.branches())
        }
    }

    impl<T: Debug> Debug for Node<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Node::Fork(fork) => fork.fmt(f),
                Node::Rope(rope) => rope.fmt(f),
                Node::Leaf(leaf) => leaf.fmt(f),
            }
        }
    }

    use std::ops::RangeInclusive;

    impl From<RangeInclusive<u8>> for Range {
        fn from(range: RangeInclusive<u8>) -> Range {
            Range {
                start: *range.start(),
                end: *range.end(),
            }
        }
    }

    impl From<RangeInclusive<char>> for Range {
        fn from(range: RangeInclusive<char>) -> Range {
            Range {
                start: *range.start() as u8,
                end: *range.end() as u8,
            }
        }
    }

    impl<T> PartialEq<Rope> for Node<T> {
        fn eq(&self, other: &Rope) -> bool {
            match self {
                Node::Rope(rope) => rope == other,
                _ => false
            }
        }
    }

    impl<T> PartialEq<Fork> for Node<T> {
        fn eq(&self, other: &Fork) -> bool {
            match self {
                Node::Fork(fork) => fork == other,
                _ => false
            }
        }
    }
}