forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
39 lines (36 loc) · 1.32 KB
/
Copy pathmod.rs
File metadata and controls
39 lines (36 loc) · 1.32 KB
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
/// Formats any kind of structured text that uses curly braces blocks
pub fn pretty_curly_print(text: &str, indent: usize) -> String {
let mut formatted = String::new();
let mut level = 0;
let mut previous_char: Option<char> = None;
const CURLY_OPEN: char = '{';
const CURLY_CLOSE: char = '}';
const WHITESPACE: char = ' ';
const NEWLINE: &str = "\n";
for character in text.chars() {
match character {
CURLY_OPEN => {
level += 1;
formatted.push(character);
formatted.push_str(NEWLINE);
formatted.push_str(&WHITESPACE.to_string().repeat(indent * level));
}
CURLY_CLOSE => {
level -= 1;
formatted.push_str(NEWLINE);
formatted.push_str(&WHITESPACE.to_string().repeat(indent * level));
formatted.push(character);
formatted.push_str(NEWLINE);
formatted.push_str(&WHITESPACE.to_string().repeat(indent * level));
}
WHITESPACE => {
if !matches!(previous_char, Some(CURLY_CLOSE)) {
formatted.push(character)
}
}
_ => formatted.push(character),
}
previous_char = Some(character);
}
formatted
}