use crate::cli::args::ValidateArgs;
use crate::error::ThoughtJackError;
use crate::loader;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ValidateOutputMode {
Silent,
Normalized,
Status,
}
const fn output_mode(normalize: bool, quiet: bool) -> ValidateOutputMode {
if quiet {
ValidateOutputMode::Silent
} else if normalize {
ValidateOutputMode::Normalized
} else {
ValidateOutputMode::Status
}
}
#[allow(clippy::unused_async)]
pub async fn validate(args: &ValidateArgs, quiet: bool) -> Result<(), ThoughtJackError> {
let yaml = std::fs::read_to_string(&args.path)?;
let loaded = loader::load_document(&yaml)?;
match output_mode(args.normalize, quiet) {
ValidateOutputMode::Silent => {}
ValidateOutputMode::Normalized => {
let normalized = serde_yaml::to_string(&loaded.document)
.map_err(|e| ThoughtJackError::Usage(e.to_string()))?;
print!("{normalized}");
}
ValidateOutputMode::Status => {
eprintln!("Valid OATF document: {}", args.path.display());
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn output_mode_honors_quiet() {
assert_eq!(output_mode(false, true), ValidateOutputMode::Silent);
assert_eq!(output_mode(true, true), ValidateOutputMode::Silent);
}
#[test]
fn output_mode_normalized_when_not_quiet() {
assert_eq!(output_mode(true, false), ValidateOutputMode::Normalized);
}
#[test]
fn output_mode_status_when_not_quiet() {
assert_eq!(output_mode(false, false), ValidateOutputMode::Status);
}
}