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
use super::*;
impl<'ty> Ctxt<'_, 'ty, '_> {
pub(super) fn print_optional(&mut self, ty: &'ty Type) {
self.comment_optional();
self.sketch_optional(ty);
}
fn comment_optional(&mut self) {
if self.inline {
return;
}
if !self.fmt.auto_comments.optional {
return;
}
if let Some(Type {
kind: TypeKind::Optional { .. },
..
}) = self.parent
{
return;
}
self.out.append_comment(|comment| {
if comment.is_empty() {
swrite!(comment, "Optional");
} else {
swrite!(comment, "; optional");
}
});
}
fn sketch_optional(&mut self, ty: &'ty Type) {
let example = self.example();
self.nested().with_ty(ty).with_example(example).print();
}
}
#[cfg(test)]
mod tests {
use super::*;
fn target<T: Document>(optional: bool) -> String {
let fmt = Formatting {
auto_comments: AutoComments {
optional,
..Default::default()
},
..Default::default()
};
Printer::default().with_formatting(&fmt).print(&T::ty())
}
mod when_hint {
use super::*;
mod is_disabled {
use super::*;
#[test]
fn then_doesnt_print_hint() {
assert_doc!(
r#"
123
"#,
target::<Option<usize>>(false)
);
}
}
mod is_enabled {
use super::*;
#[test]
fn then_prints_hint() {
assert_doc!(
r#"
// Optional
123
"#,
target::<Option<usize>>(true)
);
assert_doc!(
r#"
// Optional
123
"#,
target::<Option<Option<Option<usize>>>>(true)
);
}
}
}
}