Skip to main content

wdl_format/config/
max_line_length.rs

1//! Configuration for max line length formatting.
2
3use thiserror::Error;
4
5/// Error while creating a max line length configuration.
6#[derive(Error, Debug)]
7pub enum MaxLineLengthError {
8    /// Suppplied value outside allowed range.
9    #[error(
10        "`{0}` is outside the allowed range for the max line length: `{min}-{max}`",
11        min = MIN_MAX_LINE_LENGTH,
12        max = MAX_MAX_LINE_LENGTH
13    )]
14    OutsideAllowedRange(usize),
15}
16
17/// The default maximum line length.
18pub const DEFAULT_MAX_LINE_LENGTH: usize = 90;
19/// The minimum maximum line length.
20pub const MIN_MAX_LINE_LENGTH: usize = 60;
21/// The maximum maximum line length.
22pub const MAX_MAX_LINE_LENGTH: usize = 240;
23
24/// The maximum line length.
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub struct MaxLineLength(Option<usize>);
27
28impl MaxLineLength {
29    /// Attempts to create a new `MaxLineLength` with the provided value.
30    ///
31    /// A value of `0` indicates no maximum.
32    pub fn try_new(value: usize) -> Result<Self, MaxLineLengthError> {
33        let val = match value {
34            0 => Self(None),
35            MIN_MAX_LINE_LENGTH..=MAX_MAX_LINE_LENGTH => Self(Some(value)),
36            _ => {
37                return Err(MaxLineLengthError::OutsideAllowedRange(value));
38            }
39        };
40        Ok(val)
41    }
42
43    /// Gets the maximum line length. A value of `None` indicates no maximum.
44    pub fn get(&self) -> Option<usize> {
45        self.0
46    }
47}
48
49impl Default for MaxLineLength {
50    fn default() -> Self {
51        Self(Some(DEFAULT_MAX_LINE_LENGTH))
52    }
53}