-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod const_fold; | |
mod half_node; | ||
pub mod merge_bbs; | ||
pub mod nest_cfgs; | ||
pub mod validation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! Provides [ValidationLevel] with tools to run passes with configurable | ||
//! validation. | ||
use thiserror::Error; | ||
|
||
use hugr_core::{ | ||
extension::ExtensionRegistry, | ||
hugr::{hugrmut::HugrMut, ValidationError}, | ||
HugrView, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy, Ord, Eq, PartialOrd, PartialEq)] | ||
/// A type for running [HugrMut] algorithms with verification. | ||
/// | ||
/// Provides [ValidationLevel::run_validated_pass] to invoke a closure with pre and post | ||
/// validation. | ||
/// | ||
/// The default level is `None` because validation can be expensive. | ||
pub enum ValidationLevel { | ||
/// Do no verification. | ||
None, | ||
/// Validate using [HugrView::validate_no_extensions]. This is useful when you | ||
/// do not expect valid Extension annotations on Nodes. | ||
WithoutExtensions, | ||
/// Validate using [HugrView::validate]. | ||
WithExtensions, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
#[allow(missing_docs)] | ||
pub enum ValidatePassError { | ||
#[error("Failed to validate input HUGR: {err}\n{pretty_hugr}")] | ||
InputError { | ||
#[source] | ||
err: ValidationError, | ||
pretty_hugr: String, | ||
}, | ||
#[error("Failed to validate output HUGR: {err}\n{pretty_hugr}")] | ||
OutputError { | ||
#[source] | ||
err: ValidationError, | ||
pretty_hugr: String, | ||
}, | ||
} | ||
|
||
impl Default for ValidationLevel { | ||
fn default() -> Self { | ||
if cfg!(test) { | ||
// Many tests fail when run with Self::WithExtensions | ||
Self::WithoutExtensions | ||
} else { | ||
Self::None | ||
} | ||
} | ||
} | ||
|
||
impl ValidationLevel { | ||
/// Run an operation on a [HugrMut]. `hugr` will be verified according to | ||
/// [self](ValidationLevel), then `pass` will be invoked. If `pass` succeeds | ||
/// then `hugr` will be verified again. | ||
pub fn run_validated_pass<H: HugrMut, E, T>( | ||
&self, | ||
hugr: &mut H, | ||
reg: &ExtensionRegistry, | ||
pass: impl FnOnce(&mut H, &Self) -> Result<T, E>, | ||
) -> Result<T, E> | ||
where | ||
ValidatePassError: Into<E>, | ||
{ | ||
self.validation_impl(hugr, reg, |err, pretty_hugr| { | ||
ValidatePassError::InputError { err, pretty_hugr } | ||
})?; | ||
let result = pass(hugr, self)?; | ||
self.validation_impl(hugr, reg, |err, pretty_hugr| { | ||
ValidatePassError::OutputError { err, pretty_hugr } | ||
})?; | ||
Ok(result) | ||
} | ||
|
||
fn validation_impl<E>( | ||
&self, | ||
hugr: &impl HugrView, | ||
reg: &ExtensionRegistry, | ||
mk_err: impl FnOnce(ValidationError, String) -> ValidatePassError, | ||
) -> Result<(), E> | ||
where | ||
ValidatePassError: Into<E>, | ||
{ | ||
match self { | ||
ValidationLevel::None => Ok(()), | ||
ValidationLevel::WithoutExtensions => hugr.validate_no_extensions(reg), | ||
ValidationLevel::WithExtensions => hugr.validate(reg), | ||
} | ||
.map_err(|err| mk_err(err, hugr.mermaid_string()).into()) | ||
} | ||
} |