1use std::{
2 fmt::{self, Display, Formatter},
3 num::NonZeroUsize,
4};
5#[derive(Debug, Clone, Copy)]
6pub enum Indentation {
7 Spaces(NonZeroUsize),
8 Tabs(NonZeroUsize),
9 None,
10}
11
12impl Indentation {
13 pub fn symbol(&self) -> &str {
14 match self {
15 Self::Spaces(_) => " ",
16 Self::Tabs(_) => "\t",
17 Self::None => "",
18 }
19 }
20
21 pub fn level_separator(&self) -> char {
22 match self {
23 Self::Spaces(_) | Self::Tabs(_) => '\n',
24 Self::None => ' ',
25 }
26 }
27
28 pub fn at_level(&self, level: usize) -> String {
29 self.to_string().repeat(level)
30 }
31
32 pub fn with_spaces(n: usize) -> Self {
33 match n {
34 0 => Self::None,
35 n => Self::Spaces(NonZeroUsize::new(n).unwrap()),
36 }
37 }
38
39 pub fn with_tabs(n: usize) -> Self {
40 match n {
41 0 => Self::None,
42 n => Self::Tabs(NonZeroUsize::new(n).unwrap()),
43 }
44 }
45}
46
47impl Display for Indentation {
48 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
49 match self {
50 Indentation::Spaces(n) | Indentation::Tabs(n) => self.symbol().repeat(n.get()).fmt(f),
51 Indentation::None => Ok(()),
52 }
53 }
54}
55
56impl Default for Indentation {
57 fn default() -> Self {
58 Self::Spaces(NonZeroUsize::new(2).unwrap())
59 }
60}