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
use std::fmt::Display;

use super::*;

impl<A: Display> Display for WithLengthAndWidth<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.value())
    }
}

impl Display for RenderedAny {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Common(common) => write!(f, "{}", common),
            Self::Wide(vec) => write!(f, "( {} )", RenderVec(vec)),
            Self::Long(vec) => write!(f, "( {} )", RenderVec(vec)),
        }
    }
}

impl Display for RenderedCommon {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Empty => write!(f, "."),
            Self::Resolution => write!(f, "?"),
            Self::Event(event) => write!(f, "{}", event),
            Self::Action { name, rendered } => write!(f, "{} @ {}", name, rendered),
        }
    }
}

struct RenderVec<T>(T);

impl Display for RenderVec<&Vec<WithLengthAndWidth<RenderedWide>>> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut delimiter = "";
        let mut tail = "~";
        for rendered in self.0 {
            write!(f, "{}{}", delimiter, rendered)?;
            delimiter = " > ";
            tail = "";
        }
        write!(f, "{}", tail)
    }
}

impl Display for RenderVec<&Vec<WithLengthAndWidth<RenderedLong>>> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut delimiter = "";
        let mut tail = "~";
        for rendered in self.0 {
            write!(f, "{}{}", delimiter, rendered)?;
            delimiter = " | ";
            tail = "";
        }
        write!(f, "{}", tail)
    }
}

impl Display for RenderedLong {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Common(common) => write!(f, "{}", common),
            Self::Long(vec) => write!(f, "( {} )", RenderVec(vec)),
        }
    }
}

impl Display for RenderedWide {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Common(common) => write!(f, "{}", common),
            Self::Wide(vec) => write!(f, "( {} )", RenderVec(vec)),
        }
    }
}