wdl_format/config/
indent.rs1use thiserror::Error;
4
5use crate::SPACE;
6use crate::TAB;
7
8#[derive(Error, Debug)]
10pub enum IndentError {
11 #[error("indentation with tabs cannot have a number of spaces")]
13 InvalidConfiguration,
14 #[error("`{0}` is more than the maximum allowed number of spaces: `{max}`", max = MAX_SPACE_INDENT)]
16 TooManySpaces(usize),
17}
18
19const DEFAULT_SPACE_INDENT: usize = 4;
21pub const DEFAULT_INDENT: Indent = Indent::Spaces(DEFAULT_SPACE_INDENT);
23pub const MAX_SPACE_INDENT: usize = 16;
25
26#[derive(Clone, Copy, Debug)]
28pub enum Indent {
29 Tabs,
31 Spaces(usize),
33}
34
35impl Default for Indent {
36 fn default() -> Self {
37 DEFAULT_INDENT
38 }
39}
40
41impl Indent {
42 pub fn try_new(tab: bool, num_spaces: Option<usize>) -> Result<Self, IndentError> {
44 match (tab, num_spaces) {
45 (true, None) => Ok(Indent::Tabs),
46 (true, Some(_)) => Err(IndentError::InvalidConfiguration),
47 (false, Some(n)) => {
48 if n > MAX_SPACE_INDENT {
49 Err(IndentError::TooManySpaces(n))
50 } else {
51 Ok(Indent::Spaces(n))
52 }
53 }
54 (false, None) => Ok(Indent::default()),
55 }
56 }
57
58 pub fn num(&self) -> usize {
60 match self {
61 Indent::Tabs => 1,
62 Indent::Spaces(n) => *n,
63 }
64 }
65
66 pub fn character(&self) -> &str {
68 match self {
69 Indent::Tabs => TAB,
70 Indent::Spaces(_) => SPACE,
71 }
72 }
73
74 pub fn string(&self) -> String {
76 match self {
77 Indent::Tabs => self.character().to_string(),
78 Indent::Spaces(n) => self.character().repeat(*n),
79 }
80 }
81}