hugr_cli/
validate.rs

1//! The `validate` subcommand.
2
3use anyhow::Result;
4use clap::Parser;
5use std::io::Read;
6#[cfg(feature = "tracing")]
7use tracing::info;
8
9use crate::CliError;
10use crate::hugr_io::HugrInputArgs;
11
12/// Validate and visualise a HUGR file.
13#[derive(Parser, Debug)]
14#[clap(version = "1.0", long_about = None)]
15#[clap(about = "Validate a HUGR.")]
16#[group(id = "hugr")]
17#[non_exhaustive]
18pub struct ValArgs {
19    /// Hugr input.
20    #[command(flatten)]
21    pub input_args: HugrInputArgs,
22}
23
24/// String to print when validation is successful.
25pub const VALID_PRINT: &str = "HUGR valid!";
26
27impl ValArgs {
28    /// Run the HUGR cli and validate against an extension registry.
29    ///
30    /// # Arguments
31    ///
32    /// * `input_override` - Optional reader to use instead of the CLI input argument.
33    ///   If provided, this reader will be used for input instead of
34    ///   `self.input_args.input`.
35    pub fn run_with_input<R: Read>(&mut self, input_override: Option<R>) -> Result<()> {
36        let (desc, package) = self
37            .input_args
38            .get_described_package_with_reader(input_override)?;
39        let generator = desc.generator();
40        package
41            .validate()
42            .map_err(|val_err| CliError::validation(generator, val_err))?;
43        #[cfg(feature = "tracing")]
44        info!("{VALID_PRINT}");
45        #[cfg(not(feature = "tracing"))]
46        eprintln!("{VALID_PRINT}");
47
48        Ok(())
49    }
50
51    /// Run the HUGR cli and validate against an extension registry.
52    pub fn run(&mut self) -> Result<()> {
53        self.run_with_input(None::<&[u8]>)
54    }
55}