-
Notifications
You must be signed in to change notification settings - Fork 7
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 binary cli tool #1096
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Standard command line tools, used by the hugr binary. | ||
|
||
use clap::Parser; | ||
use clap_stdin::FileOrStdin; | ||
|
||
use crate::{extension::ExtensionRegistry, Hugr, HugrView}; | ||
/// Validate and visualise a HUGR file. | ||
#[derive(Parser, Debug)] | ||
#[clap(version = "1.0", long_about = None)] | ||
#[clap(about = "Validate a HUGR.")] | ||
struct CmdLineArgs { | ||
input: FileOrStdin, | ||
/// Visualise with mermaid. | ||
#[arg(short, long, value_name = "MERMAID", help = "Visualise with mermaid.")] | ||
mermaid: bool, | ||
|
||
/// Skip validation. | ||
#[arg(short, long, help = "Skip validation.")] | ||
no_validate: bool, | ||
// TODO YAML extensions | ||
} | ||
|
||
/// String to print when validation is successful. | ||
pub const VALID_PRINT: &str = "HUGR valid!"; | ||
|
||
/// Run the HUGR cli and validate against an extension registry. | ||
pub fn run(registry: &ExtensionRegistry) -> Result<(), Box<dyn std::error::Error>> { | ||
let opts = CmdLineArgs::parse(); | ||
|
||
let mut hugr: Hugr = serde_json::from_reader(opts.input.into_reader()?)?; | ||
if opts.mermaid { | ||
println!("{}", hugr.mermaid_string()); | ||
} | ||
|
||
if !opts.no_validate { | ||
hugr.update_validate(registry)?; | ||
|
||
println!("{}", VALID_PRINT); | ||
} | ||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! Validate serialized HUGR on the command line | ||
|
||
use hugr::std_extensions::arithmetic::{ | ||
conversions::EXTENSION as CONVERSIONS_EXTENSION, float_ops::EXTENSION as FLOAT_OPS_EXTENSION, | ||
float_types::EXTENSION as FLOAT_TYPES_EXTENSION, int_ops::EXTENSION as INT_OPS_EXTENSION, | ||
int_types::EXTENSION as INT_TYPES_EXTENSION, | ||
}; | ||
use hugr::std_extensions::logic::EXTENSION as LOGICS_EXTENSION; | ||
|
||
use hugr::extension::{ExtensionRegistry, PRELUDE}; | ||
|
||
use hugr::cli::run; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// validate with all std extensions | ||
let reg = ExtensionRegistry::try_new([ | ||
PRELUDE.to_owned(), | ||
INT_OPS_EXTENSION.to_owned(), | ||
INT_TYPES_EXTENSION.to_owned(), | ||
CONVERSIONS_EXTENSION.to_owned(), | ||
FLOAT_OPS_EXTENSION.to_owned(), | ||
FLOAT_TYPES_EXTENSION.to_owned(), | ||
LOGICS_EXTENSION.to_owned(), | ||
]) | ||
.unwrap(); | ||
|
||
run(®) | ||
} |
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,95 @@ | ||
#![cfg(feature = "cli")] | ||
|
||
use assert_cmd::Command; | ||
use assert_fs::{fixture::FileWriteStr, NamedTempFile}; | ||
use hugr::{ | ||
builder::{Dataflow, DataflowHugr}, | ||
extension::prelude::BOOL_T, | ||
type_row, | ||
types::FunctionType, | ||
Hugr, | ||
}; | ||
use predicates::prelude::*; | ||
use rstest::{fixture, rstest}; | ||
|
||
use hugr::cli::VALID_PRINT; | ||
#[fixture] | ||
fn cmd() -> Command { | ||
Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap() | ||
} | ||
|
||
#[fixture] | ||
fn test_hugr() -> Hugr { | ||
use hugr::builder::DFGBuilder; | ||
|
||
let df = DFGBuilder::new(FunctionType::new_endo(type_row![BOOL_T])).unwrap(); | ||
let [i] = df.input_wires_arr(); | ||
df.finish_prelude_hugr_with_outputs([i]).unwrap() | ||
} | ||
|
||
#[fixture] | ||
fn test_hugr_string(test_hugr: Hugr) -> String { | ||
serde_json::to_string(&test_hugr).unwrap() | ||
} | ||
|
||
#[fixture] | ||
fn test_hugr_file(test_hugr_string: String) -> NamedTempFile { | ||
// TODO use proptests? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For generating the Hugr? That's a long way off, we need to generate valid sub-properties then wire them together into a Hugr. I don't think it would be useful here, we are already testing serialisation elsewhere. |
||
let file = assert_fs::NamedTempFile::new("sample.hugr").unwrap(); | ||
file.write_str(&test_hugr_string).unwrap(); | ||
file | ||
} | ||
|
||
#[rstest] | ||
fn test_doesnt_exist(mut cmd: Command) -> Result<(), Box<dyn std::error::Error>> { | ||
cmd.arg("foobar"); | ||
cmd.assert() | ||
.failure() | ||
.stderr(predicate::str::contains("No such file or directory")); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[rstest] | ||
fn test_validate( | ||
test_hugr_file: NamedTempFile, | ||
mut cmd: Command, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
cmd.arg(test_hugr_file.path()); | ||
cmd.assert() | ||
.success() | ||
.stdout(predicate::str::contains(VALID_PRINT)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[rstest] | ||
fn test_stdin( | ||
test_hugr_string: String, | ||
mut cmd: Command, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
cmd.write_stdin(test_hugr_string); | ||
cmd.arg("-"); | ||
|
||
cmd.assert() | ||
.success() | ||
.stdout(predicate::str::contains(VALID_PRINT)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[rstest] | ||
fn test_mermaid( | ||
test_hugr_file: NamedTempFile, | ||
mut cmd: Command, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
const MERMAID: &str = "graph LR\n subgraph 0 [\"(0) DFG\"]"; | ||
cmd.arg(test_hugr_file.path()); | ||
cmd.arg("--mermaid"); | ||
cmd.arg("--no-validate"); | ||
cmd.assert() | ||
.success() | ||
.stdout(predicate::str::contains(MERMAID)); | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to expose
pub CmdLineArgs
and either take it as a parameter here, or put this function onimpl CmdLineArgs
.This moves the
CmdLineArgs::parse()
into the caller, which allows callers to add (and handle) options, embed as aSubCommand
etc.