Skip to content

Commit a646f0b

Browse files
committed
Refactor pasfmt_config! macro to improve testability
1 parent e5e3e5f commit a646f0b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `try_create` and `validate` functions to the type generated by the `pasfmt_config!` macro.
13+
1014
### Fixed
1115

1216
- Lexing of conditional directive expressions containing compiler directives, comments, or strings.

orchestrator/src/command_line.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,29 @@ macro_rules! pasfmt_config {
3030
config: PasFmtConfiguration,
3131
}
3232
impl $type_name {
33-
pub fn create() -> PasFmtConfiguration {
34-
let parsed = Self::parse();
35-
let mut cmd = Self::command();
36-
if matches!(parsed.config.mode(), FormatMode::Files) && parsed.config.is_stdin() {
37-
cmd.error(
33+
#[allow(unused)]
34+
pub fn validate(self) -> Result<PasFmtConfiguration, clap::Error> {
35+
if matches!(self.config.mode(), FormatMode::Files) && self.config.is_stdin() {
36+
return Err(Self::command().error(
3837
ErrorKind::ArgumentConflict,
3938
"Files mode not supported when reading from stdin.",
40-
)
41-
.exit();
39+
));
4240
}
4341

44-
parsed.config
42+
Ok(self.config)
43+
}
44+
45+
#[allow(unused)]
46+
pub fn try_create() -> Result<PasFmtConfiguration, clap::Error> {
47+
Self::try_parse()?.validate()
48+
}
49+
50+
#[allow(unused)]
51+
pub fn create() -> PasFmtConfiguration {
52+
match Self::try_create() {
53+
Ok(config) => config,
54+
Err(err) => err.exit(),
55+
}
4556
}
4657
}
4758
};

0 commit comments

Comments
 (0)