render_tree/stylesheet/
format.rs

1use super::{Match, Node, Segment};
2use crate::Style;
3use std::fmt;
4
5impl fmt::Display for Segment {
6    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7        match self {
8            Segment::Name(s) => write!(f, "{}", s),
9            Segment::Glob => write!(f, "**"),
10            Segment::Star => write!(f, "*"),
11            Segment::Root => write!(f, "ε"),
12        }
13    }
14}
15
16impl fmt::Display for Node {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        write!(f, "{}", self.display())
19    }
20}
21
22pub(super) struct NodeDetails<'a> {
23    segment: Segment,
24    style: &'a Option<Style>,
25}
26
27impl<'a> NodeDetails<'a> {
28    pub(super) fn new(segment: Segment, style: &'a Option<Style>) -> NodeDetails<'a> {
29        NodeDetails { segment, style }
30    }
31}
32
33impl<'a> fmt::Display for NodeDetails<'a> {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        write!(f, "{} style=", self.segment)?;
36
37        match &self.style {
38            None => write!(f, "None")?,
39            Some(style) => write!(f, "{}", style)?,
40        }
41
42        Ok(())
43    }
44}
45
46pub(super) struct DisplayStyle<'a>(pub(super) &'a Option<Style>);
47
48impl<'a> fmt::Display for DisplayStyle<'a> {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        match self.0 {
51            None => write!(f, "None"),
52            Some(style) => write!(f, "{}", style),
53        }
54    }
55}
56
57impl<'a> fmt::Display for Match<'a> {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        if self.glob.is_none()
60            && self.star.is_none()
61            && self.skipped_glob.is_none()
62            && self.literal.is_none()
63        {
64            write!(f, "None")
65        } else {
66            write!(f, "[")?;
67
68            let mut wrote_first = false;
69
70            let mut comma = |f: &mut fmt::Formatter| -> fmt::Result {
71                if wrote_first {
72                    write!(f, ", ")
73                } else {
74                    wrote_first = true;
75                    Ok(())
76                }
77            };
78
79            if let Some(glob) = self.glob {
80                comma(f)?;
81                write!(f, "{}", glob.segment)?;
82            }
83
84            if let Some(star) = self.star {
85                comma(f)?;
86                write!(f, "{}", star.segment)?;
87            }
88
89            if let Some(skipped_glob) = self.skipped_glob {
90                comma(f)?;
91                write!(f, "skipped glob: {}", skipped_glob.segment)?;
92            }
93
94            if let Some(literal) = self.literal {
95                comma(f)?;
96                write!(f, "next: {}", literal.segment)?;
97            }
98
99            write!(f, "]")
100        }
101    }
102}