Skip to main content

syn_sem/semantic/tree/
format.rs

1use super::PathId;
2use crate::{etc::abs_fs::AbstractFiles, Map};
3use std::fmt::{self, Write};
4
5pub struct Brief<'a, T: DebugBriefly> {
6    data: &'a T,
7    filter: PrintFilter,
8}
9
10impl<'a, T: DebugBriefly> Brief<'a, T> {
11    /// Wraps the data within [`Brief`] type to make it to be printed concisely.
12    pub fn new(data: &'a T) -> Self {
13        Self {
14            data,
15            filter: PrintFilter {
16                is_item_of: Map::default(),
17                starts_with: Map::default(),
18                contains: Map::default(),
19            },
20        }
21    }
22
23    /// Hides known libraries.
24    pub fn hide_known(&mut self, files: &AbstractFiles) -> &mut Self {
25        let known_names = files.known_libraries().map(|(name, _)| &**name);
26        self.starts_with_then_hide(known_names)
27    }
28
29    pub fn starts_with_then_show_detail<'i, I>(&mut self, starts_with: I) -> &mut Self
30    where
31        I: IntoIterator<Item = &'i str>,
32    {
33        for s in starts_with {
34            self.filter
35                .starts_with
36                .insert(s.to_owned(), DebugBriefHow::ShowDetail);
37        }
38        self
39    }
40
41    pub fn starts_with_then_hide<'i, I>(&mut self, starts_with: I) -> &mut Self
42    where
43        I: IntoIterator<Item = &'i str>,
44    {
45        for s in starts_with {
46            self.filter
47                .starts_with
48                .insert(s.to_owned(), DebugBriefHow::Hide);
49        }
50        self
51    }
52
53    pub fn contains_then_show_detail<'i, I>(&mut self, contains: I) -> &mut Self
54    where
55        I: IntoIterator<Item = &'i str>,
56    {
57        for s in contains {
58            self.filter
59                .contains
60                .insert(s.to_owned(), DebugBriefHow::ShowDetail);
61        }
62        self
63    }
64
65    pub fn contains_then_hide<'i, I>(&mut self, contains: I) -> &mut Self
66    where
67        I: IntoIterator<Item = &'i str>,
68    {
69        for s in contains {
70            self.filter
71                .contains
72                .insert(s.to_owned(), DebugBriefHow::Hide);
73        }
74        self
75    }
76
77    pub fn is_item_of_then_show_detail<'i, I>(&mut self, item_names: I) -> &mut Self
78    where
79        I: IntoIterator<Item = &'i str>,
80    {
81        for s in item_names {
82            self.filter
83                .is_item_of
84                .insert(s.to_owned(), DebugBriefHow::ShowDetail);
85        }
86        self
87    }
88
89    pub fn is_item_of_then_hide<'i, I>(&mut self, item_names: I) -> &mut Self
90    where
91        I: IntoIterator<Item = &'i str>,
92    {
93        for s in item_names {
94            self.filter
95                .is_item_of
96                .insert(s.to_owned(), DebugBriefHow::Hide);
97        }
98        self
99    }
100}
101
102impl<T: DebugBriefly> fmt::Debug for Brief<'_, T> {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        self.data.fmt_briefly(f, &self.filter)
105    }
106}
107
108pub(crate) struct DebugItem<'a, T> {
109    pub(crate) id: &'a PathId,
110    pub(crate) path: &'a String,
111    pub(crate) item: &'a T,
112}
113
114impl<T: fmt::Debug> fmt::Debug for DebugItem<'_, T> {
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        fmt::Debug::fmt(&self.id, f)?;
117        f.write_char(' ')?;
118        fmt::Debug::fmt(&self.path, f)?;
119        f.write_str(" => ")?;
120        self.item.fmt(f)
121    }
122}
123
124pub(crate) struct BriefDebugItem<'a, T> {
125    pub(crate) id: &'a PathId,
126    pub(crate) path: &'a String,
127    pub(crate) item: &'a T,
128    pub(crate) filter: &'a PrintFilter,
129}
130
131impl<T: DebugBriefly> fmt::Debug for BriefDebugItem<'_, T> {
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        fmt::Debug::fmt(&self.id, f)?;
134        f.write_char(' ')?;
135        fmt::Debug::fmt(&self.path, f)?;
136        f.write_str(" => ")?;
137        self.item.fmt_briefly(f, self.filter)
138    }
139}
140
141pub trait DebugBriefly: fmt::Debug {
142    fn fmt_briefly(&self, f: &mut fmt::Formatter<'_>, filter: &PrintFilter) -> fmt::Result;
143
144    fn name(&self) -> &'static str;
145}
146
147// Priority of filtering is determined by each implementation, but it is recommended to follow
148// field declaration order.
149pub struct PrintFilter {
150    pub(crate) is_item_of: Map<String, DebugBriefHow>,
151    pub(crate) starts_with: Map<String, DebugBriefHow>,
152    pub(crate) contains: Map<String, DebugBriefHow>,
153}
154
155#[derive(Clone, Copy)]
156pub enum DebugBriefHow {
157    ShowDetail,
158    Hide,
159}