Skip to content

Commit

Permalink
fix: rework Config Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Jan 15, 2025
1 parent d16da2a commit b4d1020
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
32 changes: 6 additions & 26 deletions wdl-format/src/config/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

/// A builder for a [`Config`].
#[derive(Default)]
pub struct Builder {
/// The number of characters to indent.
indent: Option<Indent>,
Expand All @@ -44,15 +45,6 @@ pub struct Builder {
}

impl Builder {
/// Creates a new builder with default values.
pub fn new(indent: Option<Indent>, max_line_length: Option<MaxLineLength>) -> Result<Self> {
let indent = indent.unwrap_or_default();
let max_line_length = max_line_length.unwrap_or_default();
Ok(Self::default()
.indent(indent)?
.max_line_length(max_line_length))
}

/// Sets the indentation level.
///
/// This silently overwrites any previously provided value for the
Expand All @@ -76,25 +68,13 @@ impl Builder {
self
}

/// Consumes `self` and attempts to build a [`Config`].
pub fn try_build(self) -> Result<Config> {
let indent = self.indent.ok_or(Error::Missing("indent"))?;
let max_line_length = self
.max_line_length
.ok_or(Error::Missing("max_line_length"))?;

Ok(Config {
/// Consumes `self` to build a [`Config`].
pub fn build(self) -> Config {
let indent = self.indent.unwrap_or_default();
let max_line_length = self.max_line_length.unwrap_or_default();
Config {
indent,
max_line_length,
})
}
}

impl Default for Builder {
fn default() -> Self {
Self {
indent: Some(Default::default()),
max_line_length: Some(Default::default()),
}
}
}
23 changes: 22 additions & 1 deletion wdl-format/src/config/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
use crate::SPACE;
use crate::TAB;

/// The default number of spaces to represent one indentation level.
const DEFAULT_SPACE_INDENT: usize = 4;
/// The default indentation.
pub const DEFAULT_INDENT: Indent = Indent::Spaces(4);
pub const DEFAULT_INDENT: Indent = Indent::Spaces(DEFAULT_SPACE_INDENT);
/// The maximum number of spaces to represent one indentation level.
pub const MAX_SPACE_INDENT: usize = 16;

Expand All @@ -24,6 +26,25 @@ impl Default for Indent {
}

impl Indent {
/// Attempts to create a new indentation level configuration.
pub fn try_new(tab: bool, num_spaces: Option<usize>) -> Result<Self, String> {
match (tab, num_spaces) {
(true, None) => Ok(Indent::Tabs),
(true, Some(_)) => Err("Indentation with tabs cannot have a number of spaces".to_string()),
(false, Some(n)) => {
if n > MAX_SPACE_INDENT {
Err(format!(
"Indentation with spaces cannot have more than {} characters",
MAX_SPACE_INDENT
))
} else {
Ok(Indent::Spaces(n))
}
}
(false, None) => Ok(Indent::Spaces(DEFAULT_SPACE_INDENT)),
}
}

/// Gets the number of characters to indent.
pub fn num(&self) -> usize {
match self {
Expand Down
8 changes: 5 additions & 3 deletions wdl-format/src/config/max_line_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ pub const MAX_MAX_LINE_LENGTH: usize = 240;
pub struct MaxLineLength(Option<usize>);

impl MaxLineLength {
/// Creates a new `MaxLineLength` with the provided value.
pub fn with_value(value: usize) -> Result<Self, Error> {
/// Attempts to create a new `MaxLineLength` with the provided value.
///
/// A value of `0` indicates no maximum.
pub fn try_new(value: usize) -> Result<Self, Error> {
let val = match value {
0 => Self(None),
MIN_MAX_LINE_LENGTH..=MAX_MAX_LINE_LENGTH => Self(Some(value)),
Expand All @@ -39,7 +41,7 @@ impl MaxLineLength {
Ok(val)
}

/// Gets the maximum line length.
/// Gets the maximum line length. A value of `None` indicates no maximum.
pub fn get(&self) -> Option<usize> {
self.0
}
Expand Down

0 comments on commit b4d1020

Please sign in to comment.