1use pretty::{Pretty, DocAllocator, BoxAllocator, DocBuilder};
2use std::fmt;
3
4#[derive(Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
5pub struct Id(char);
6
7#[cfg(test)] use arbitrary::{Unstructured, Arbitrary};
8
9impl<'a, D, A> Pretty<'a, D, A> for Id
10where
11 D: DocAllocator<'a, A>,
12 D::Doc: Clone,
13 A: 'a + Clone,
14{
15 fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
16 allocator.text(self.0.to_string())
17 }
18}
19
20#[cfg(test)]
21impl<'a> Arbitrary<'a> for Id {
22 fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Id, arbitrary::Error> {
23 let c = u.int_in_range(0..=25)?;
24 Ok(Id((b'a' + c) as char))
25 }
26}
27
28impl<'a> fmt::Display for Id {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 <Id as Pretty<'_, BoxAllocator, ()>>::pretty(self.clone(), &BoxAllocator)
32 .1
33 .render_fmt(4, f)
34 }
35}
36
37impl From<char> for Id {
38 fn from(s: char) -> Self {
39 Id(s)
40 }
41}
42
43impl<'a> fmt::Debug for Id {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(f, "{}", self)
46 }
47}
48