Skip to content

Commit

Permalink
feat(hugr-cli)!: Add Package::validate and return `ExtensionRegistr…
Browse files Browse the repository at this point in the history
…y` in helpers. (#1507)

BREAKING CHANGE: Add an `ExtensionRegistry` result to several helpers.
  • Loading branch information
doug-q authored Sep 4, 2024
1 parent 6bc5593 commit f3dafd7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion hugr-cli/src/mermaid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl MermaidArgs {
/// Write the mermaid diagram to the output.
pub fn run_print(&mut self) -> Result<(), crate::CliError> {
let hugrs = if self.validate {
self.hugr_args.validate()?
self.hugr_args.validate()?.0
} else {
self.hugr_args.get_package()?.modules
};
Expand Down
52 changes: 33 additions & 19 deletions hugr-cli/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ pub const VALID_PRINT: &str = "HUGR valid!";

impl ValArgs {
/// Run the HUGR cli and validate against an extension registry.
pub fn run(&mut self) -> Result<Vec<Hugr>, CliError> {
self.hugr_args.validate()
pub fn run(&mut self) -> Result<(Vec<Hugr>, ExtensionRegistry), CliError> {
let result = self.hugr_args.validate()?;
if self.verbosity(Level::Info) {
eprintln!("{}", VALID_PRINT);
}
Ok(result)
}

/// Test whether a `level` message should be output.
Expand All @@ -46,39 +50,49 @@ impl ValArgs {
}
}

impl Package {
/// Validate the package against an extension registry.
///
/// `reg` is updated with any new extensions.
///
/// Returns the validated modules.
pub fn validate(mut self, reg: &mut ExtensionRegistry) -> Result<Vec<Hugr>, ValError> {
// register packed extensions
for ext in self.extensions {
reg.register_updated(ext)?;
}

for hugr in self.modules.iter_mut() {
hugr.update_validate(reg)?;
}

Ok(self.modules)
}
}

impl HugrArgs {
/// Load the package and validate against an extension registry.
pub fn validate(&mut self) -> Result<Vec<Hugr>, CliError> {
let Package {
mut modules,
extensions: packed_exts,
} = self.get_package()?;
///
/// Returns the validated modules and the extension registry the modules
/// were validated against.
pub fn validate(&mut self) -> Result<(Vec<Hugr>, ExtensionRegistry), CliError> {
let package = self.get_package()?;

let mut reg: ExtensionRegistry = if self.no_std {
hugr_core::extension::PRELUDE_REGISTRY.to_owned()
} else {
hugr_core::std_extensions::STD_REG.to_owned()
};

// register packed extensions
for ext in packed_exts {
reg.register_updated(ext).map_err(ValError::ExtReg)?;
}

// register external extensions
for ext in &self.extensions {
let f = std::fs::File::open(ext)?;
let ext: Extension = serde_json::from_reader(f)?;
reg.register_updated(ext).map_err(ValError::ExtReg)?;
}

for hugr in modules.iter_mut() {
hugr.update_validate(&reg).map_err(ValError::Validate)?;
if self.verbosity(Level::Info) {
eprintln!("{}", VALID_PRINT);
}
}
Ok(modules)
let modules = package.validate(&mut reg)?;
Ok((modules, reg))
}

/// Test whether a `level` message should be output.
Expand Down

0 comments on commit f3dafd7

Please sign in to comment.