render_tree/stylesheet/
format.rs

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 super::{Match, Node, Segment};
use crate::Style;
use std::fmt;

impl fmt::Display for Segment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Segment::Name(s) => write!(f, "{}", s),
            Segment::Glob => write!(f, "**"),
            Segment::Star => write!(f, "*"),
            Segment::Root => write!(f, "ε"),
        }
    }
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.display())
    }
}

pub(super) struct NodeDetails<'a> {
    segment: Segment,
    style: &'a Option<Style>,
}

impl<'a> NodeDetails<'a> {
    pub(super) fn new(segment: Segment, style: &'a Option<Style>) -> NodeDetails<'a> {
        NodeDetails { segment, style }
    }
}

impl<'a> fmt::Display for NodeDetails<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} style=", self.segment)?;

        match &self.style {
            None => write!(f, "None")?,
            Some(style) => write!(f, "{}", style)?,
        }

        Ok(())
    }
}

pub(super) struct DisplayStyle<'a>(pub(super) &'a Option<Style>);

impl<'a> fmt::Display for DisplayStyle<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            None => write!(f, "None"),
            Some(style) => write!(f, "{}", style),
        }
    }
}

impl<'a> fmt::Display for Match<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.glob.is_none()
            && self.star.is_none()
            && self.skipped_glob.is_none()
            && self.literal.is_none()
        {
            write!(f, "None")
        } else {
            write!(f, "[")?;

            let mut wrote_first = false;

            let mut comma = |f: &mut fmt::Formatter| -> fmt::Result {
                if wrote_first {
                    write!(f, ", ")
                } else {
                    wrote_first = true;
                    Ok(())
                }
            };

            if let Some(glob) = self.glob {
                comma(f)?;
                write!(f, "{}", glob.segment)?;
            }

            if let Some(star) = self.star {
                comma(f)?;
                write!(f, "{}", star.segment)?;
            }

            if let Some(skipped_glob) = self.skipped_glob {
                comma(f)?;
                write!(f, "skipped glob: {}", skipped_glob.segment)?;
            }

            if let Some(literal) = self.literal {
                comma(f)?;
                write!(f, "next: {}", literal.segment)?;
            }

            write!(f, "]")
        }
    }
}