Skip to content

Commit

Permalink
refactor(hugr-cli): common up some args
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Aug 2, 2024
1 parent 18c3835 commit 7989adc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
24 changes: 21 additions & 3 deletions hugr-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Standard command line tools, used by the hugr binary.

use std::ffi::OsString;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use clio::Input;
use std::{ffi::OsString, path::PathBuf};
use thiserror::Error;
/// We reexport some clap types that are used in the public API.
pub use {clap::Parser, clap_verbosity_flag::Level};

pub mod extensions;
pub mod validate;
Expand Down Expand Up @@ -32,3 +33,20 @@ pub enum CliError {
/// Errors produced by the `validate` subcommand.
Validate(#[from] validate::CliError),
}

/// Validate and visualise a HUGR file.
#[derive(Parser, Debug)]
pub struct HugrArgs {
/// Input HUGR file, use '-' for stdin
#[clap(value_parser, default_value = "-")]
pub input: Input,
/// Verbosity.
#[command(flatten)]
pub verbose: Verbosity<InfoLevel>,
/// No standard extensions.
#[arg(long, help = "Don't use standard extensions when validating.")]
pub no_std: bool,
/// Skip validation.
#[arg(short, long, help = "Skip validation.")]
pub extensions: Vec<PathBuf>,
}
29 changes: 10 additions & 19 deletions hugr-cli/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
//! The `validate` subcommand.
use std::path::PathBuf;

use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Level, Verbosity};
use clio::Input;
use clap_verbosity_flag::Level;
use hugr_core::{extension::ExtensionRegistry, Extension, Hugr, HugrView as _};
use thiserror::Error;

use crate::HugrArgs;

/// Validate and visualise a HUGR file.
#[derive(Parser, Debug)]
#[clap(version = "1.0", long_about = None)]
#[clap(about = "Validate a HUGR.")]
#[group(id = "hugr")]
#[non_exhaustive]
pub struct ValArgs {
/// Input HUGR file, use '-' for stdin
#[clap(value_parser, default_value = "-")]
pub input: Input,
#[command(flatten)]
/// common arguments
pub hugr_args: HugrArgs,
/// Visualise with mermaid.
#[arg(short, long, value_name = "MERMAID", help = "Visualise with mermaid.")]
pub mermaid: bool,
/// Skip validation.
#[arg(short, long, help = "Skip validation.")]
pub no_validate: bool,
/// Verbosity.
#[command(flatten)]
pub verbose: Verbosity<InfoLevel>,
/// No standard extensions.
#[arg(long, help = "Don't use standard extensions when validating.")]
pub no_std: bool,
/// Skip validation.
#[arg(short, long, help = "Skip validation.")]
pub extensions: Vec<PathBuf>,
}

/// Error type for the CLI.
Expand Down Expand Up @@ -87,7 +78,7 @@ impl ValArgs {
/// Run the HUGR cli and validate against an extension registry.
pub fn run(&mut self) -> Result<Vec<Hugr>, CliError> {
// let rdr = self.input.
let val: serde_json::Value = serde_json::from_reader(&mut self.input)?;
let val: serde_json::Value = serde_json::from_reader(&mut self.hugr_args.input)?;
// read either a package or a single hugr
let (mut modules, packed_exts) = if let Ok(Package {
modules,
Expand All @@ -100,7 +91,7 @@ impl ValArgs {
(vec![hugr], vec![])
};

let mut reg: ExtensionRegistry = if self.no_std {
let mut reg: ExtensionRegistry = if self.hugr_args.no_std {
hugr_core::extension::PRELUDE_REGISTRY.to_owned()
} else {
hugr_core::std_extensions::std_reg()
Expand All @@ -112,7 +103,7 @@ impl ValArgs {
}

// register external extensions
for ext in &self.extensions {
for ext in &self.hugr_args.extensions {
let f = std::fs::File::open(ext)?;
let ext: Extension = serde_json::from_reader(f)?;
reg.register_updated(ext)?;
Expand All @@ -135,6 +126,6 @@ impl ValArgs {

/// Test whether a `level` message should be output.
pub fn verbosity(&self, level: Level) -> bool {
self.verbose.log_level_filter() >= level
self.hugr_args.verbose.log_level_filter() >= level
}
}

0 comments on commit 7989adc

Please sign in to comment.