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
pub fn normalize_doc_string(s: &str) -> Option<String> {
let trimmed = s.trim();
if trimmed.is_empty() {
None
} else {
Some(
remove_asterisks_if_asterisk_docs(&normalize_indentation(s))
.trim()
.to_string(),
)
}
}
pub fn normalize_indentation(string: &str) -> String {
let lines = string.lines();
if lines.count() == 1 {
string.trim().to_string()
} else {
let num_leading = string
.lines()
.filter_map(|l| l.chars().position(|c| !c.is_whitespace()))
.min()
.unwrap_or(0);
string
.lines()
.map(|line| {
if line.len() > num_leading {
&line[num_leading..]
} else {
line
}
})
.map(|l| l.trim_end())
.collect::<Vec<_>>()
.join("\n")
}
}
pub fn remove_asterisks_if_asterisk_docs(s: &str) -> String {
let should_remove_asterisks = s
.lines()
.all(|l| l.is_empty() || l.starts_with("* ") || l == "*");
if should_remove_asterisks {
s.lines()
.map(|line| {
if let Some(fmt) = line.strip_prefix("* ") {
fmt
} else if line == "*" {
""
} else {
line
}
})
.collect::<Vec<_>>()
.join("\n")
} else {
s.to_string()
}
}