Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hugr-cli)!: Add Package::validate and return ExtensionRegistry in helpers. #1507

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading