1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Shared validation limits and error construction for the CLI module tree.
//!
//! These items are needed by both [`super::config`] (layered-configuration
//! validation) and [`super::parsing`] (Clap value validation), so they live in
//! their own leaf module rather than in [`super`]. Keeping them free of
//! dependencies lets the build script compile [`super::config`] without
//! dragging in the rest of the `cli` subtree.
use ortho_config::OrthoError;
use std::sync::Arc;
/// Maximum number of jobs accepted by the CLI.
pub(super) const MAX_JOBS: usize = 64;
/// Build a validation error for `key` with `message`.
///
/// Produces [`OrthoError::Validation`] so callers can preserve the rejected
/// field and its diagnostic while propagating a shared error value.
///
/// # Examples
///
/// ```ignore
/// let error = validation_error("jobs", "must be positive");
/// assert!(matches!(
/// error.as_ref(),
/// OrthoError::Validation { key, message }
/// if key == "jobs" && message == "must be positive"
/// ));
/// ```
pub(super) fn validation_error(key: &str, message: &str) -> Arc<OrthoError> {
Arc::new(OrthoError::Validation {
key: key.to_owned(),
message: message.to_owned(),
})
}
#[cfg(test)]
mod tests {
//! Unit tests for CLI validation limits and errors.
use super::*;
/// Verify that the maximum job limit remains 64.
#[test]
fn max_jobs_matches_the_cli_contract() {
assert_eq!(MAX_JOBS, 64);
}
/// Verify that validation errors preserve their supplied key and message.
#[test]
fn validation_error_preserves_its_key_and_message() {
let error = validation_error("jobs", "message");
let OrthoError::Validation { key, message } = error.as_ref() else {
panic!("validation_error should construct OrthoError::Validation");
};
assert_eq!(key, "jobs");
assert_eq!(message, "message");
}
}