Skip to content

Commit

Permalink
feat(hugr-cli)!: return ExtensionRegistry in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Sep 3, 2024
1 parent be9e2be commit 768bfdb
Show file tree
Hide file tree
Showing 2 changed files with 27 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
45 changes: 26 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,42 @@ impl ValArgs {
}
}

impl Package {
/// TODO
pub fn validate(mut self, reg: &mut ExtensionRegistry) -> Result<Vec<Hugr>, ValError> {
// register packed extension
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()?;
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 768bfdb

Please sign in to comment.