1use petr_utils::{PrettyPrint, SymbolInterner};
2
3use crate::Comment;
4
5pub struct Commented<T>(T, Vec<Comment>);
6
7impl<T> Commented<T> {
8 pub fn new(
9 item: T,
10 comments: Vec<Comment>,
11 ) -> Self {
12 Commented(item, comments)
13 }
14
15 pub fn item(&self) -> &T {
16 &self.0
17 }
18
19 pub fn comments(&self) -> &[Comment] {
20 &self.1[..]
21 }
22}
23
24impl<T: Clone> Clone for Commented<T> {
25 fn clone(&self) -> Self {
26 Commented(self.0.clone(), self.1.clone())
27 }
28}
29
30impl PrettyPrint for Comment {
31 fn pretty_print(
32 &self,
33 _interner: &SymbolInterner,
34 indentation: usize,
35 ) -> String {
36 format!("{}{{- {} -}}", " ".repeat(indentation), self.content)
37 }
38}
39
40impl<T> PrettyPrint for Commented<T>
41where
42 T: PrettyPrint,
43{
44 fn pretty_print(
45 &self,
46 interner: &SymbolInterner,
47 indentation: usize,
48 ) -> String {
49 let comments = self.comments();
50 format!(
51 "{}{}",
52 if comments.is_empty() {
53 String::new()
54 } else {
55 format!(
56 "{}\n",
57 comments
58 .iter()
59 .map(|comment| comment.pretty_print(interner, indentation))
60 .collect::<Vec<_>>()
61 .join("\n")
62 )
63 },
64 self.item().pretty_print(interner, indentation)
65 )
66 }
67}