Skip to content

Commit f3dafd7

Browse files
authored
feat(hugr-cli)!: Add Package::validate and return ExtensionRegistry in helpers. (#1507)
BREAKING CHANGE: Add an `ExtensionRegistry` result to several helpers.
1 parent 6bc5593 commit f3dafd7

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

hugr-cli/src/mermaid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl MermaidArgs {
3030
/// Write the mermaid diagram to the output.
3131
pub fn run_print(&mut self) -> Result<(), crate::CliError> {
3232
let hugrs = if self.validate {
33-
self.hugr_args.validate()?
33+
self.hugr_args.validate()?.0
3434
} else {
3535
self.hugr_args.get_package()?.modules
3636
};

hugr-cli/src/validate.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ pub const VALID_PRINT: &str = "HUGR valid!";
3636

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

4347
/// Test whether a `level` message should be output.
@@ -46,39 +50,49 @@ impl ValArgs {
4650
}
4751
}
4852

53+
impl Package {
54+
/// Validate the package against an extension registry.
55+
///
56+
/// `reg` is updated with any new extensions.
57+
///
58+
/// Returns the validated modules.
59+
pub fn validate(mut self, reg: &mut ExtensionRegistry) -> Result<Vec<Hugr>, ValError> {
60+
// register packed extensions
61+
for ext in self.extensions {
62+
reg.register_updated(ext)?;
63+
}
64+
65+
for hugr in self.modules.iter_mut() {
66+
hugr.update_validate(reg)?;
67+
}
68+
69+
Ok(self.modules)
70+
}
71+
}
72+
4973
impl HugrArgs {
5074
/// Load the package and validate against an extension registry.
51-
pub fn validate(&mut self) -> Result<Vec<Hugr>, CliError> {
52-
let Package {
53-
mut modules,
54-
extensions: packed_exts,
55-
} = self.get_package()?;
75+
///
76+
/// Returns the validated modules and the extension registry the modules
77+
/// were validated against.
78+
pub fn validate(&mut self) -> Result<(Vec<Hugr>, ExtensionRegistry), CliError> {
79+
let package = self.get_package()?;
5680

5781
let mut reg: ExtensionRegistry = if self.no_std {
5882
hugr_core::extension::PRELUDE_REGISTRY.to_owned()
5983
} else {
6084
hugr_core::std_extensions::STD_REG.to_owned()
6185
};
6286

63-
// register packed extensions
64-
for ext in packed_exts {
65-
reg.register_updated(ext).map_err(ValError::ExtReg)?;
66-
}
67-
6887
// register external extensions
6988
for ext in &self.extensions {
7089
let f = std::fs::File::open(ext)?;
7190
let ext: Extension = serde_json::from_reader(f)?;
7291
reg.register_updated(ext).map_err(ValError::ExtReg)?;
7392
}
7493

75-
for hugr in modules.iter_mut() {
76-
hugr.update_validate(&reg).map_err(ValError::Validate)?;
77-
if self.verbosity(Level::Info) {
78-
eprintln!("{}", VALID_PRINT);
79-
}
80-
}
81-
Ok(modules)
94+
let modules = package.validate(&mut reg)?;
95+
Ok((modules, reg))
8296
}
8397

8498
/// Test whether a `level` message should be output.

0 commit comments

Comments
 (0)