Skip to content

Commit

Permalink
Merge branch 'main' of gh:CQCL-DEV/hugr into feat/proptest2
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed May 30, 2024
2 parents cdd82c1 + d0cd023 commit cab398c
Show file tree
Hide file tree
Showing 34 changed files with 432 additions and 302 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/ci-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ jobs:
RUSTDOCFLAGS: "-Dwarnings"

benches:
name: Build benchmarks 🏋️
needs: changes
if: ${{ needs.changes.outputs.rust == 'true' }} && github.event_name != 'merge_group'
if: ${{ needs.changes.outputs.rust == 'true' && github.event_name != 'merge_group' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -122,7 +123,9 @@ jobs:
strategy:
fail-fast: true
matrix:
rust: ['1.75', beta, nightly]
# Pinned nightly version until this gets resolved:
# https://github.com/rust-lang/rust/issues/125474
rust: ['1.75', beta, 'nightly-2024-05-22']
name: tests (Rust ${{ matrix.rust }})
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -176,8 +179,13 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: mozilla-actions/[email protected]
- uses: dtolnay/rust-toolchain@nightly
- uses: dtolnay/rust-toolchain@master
with:
# Nightly is required to count doctests coverage
#
# Pinned nightly version until this gets resolved
# https://github.com/rust-lang/rust/issues/125474
toolchain: 'nightly-2024-05-22'
components: llvm-tools-preview
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ repos:
- id: cargo-doc
name: cargo doc
description: Generate documentation with `cargo doc`.
entry: RUSTDOCFLAGS=-Dwarnings cargo doc --no-deps --all-features --workspace
entry: sh -c "RUSTDOCFLAGS=-Dwarnings cargo doc --no-deps --all-features --workspace"
language: system
files: \.rs$
pass_filenames: false
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ typetag = "0.2.7"
urlencoding = "2.1.2"
webbrowser = "1.0.0"
clap = { version = "4.5.4"}
clap-stdin = "0.4.0"
clap-verbosity-flag = "2.2.0"
assert_cmd = "2.0.14"
assert_fs = "1.1.1"
clap-stdin = "0.4.0"
predicates = "3.1.0"

[profile.dev.package]
Expand Down
11 changes: 10 additions & 1 deletion hugr-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.1.0 (2024-05-24)
## 0.1.0 (2024-05-29)

Initial release, ported from `hugr::cli` module.

### Bug Fixes

- Set initial version of hugr-core to 0.1.0 ([#1129](https://github.com/CQCL/hugr/pull/1129))

### Features

- [**breaking**] Move cli in to hugr-cli sub-crate ([#1107](https://github.com/CQCL/hugr/pull/1107))
- Add verbosity, return `Hugr` from `run`. ([#1116](https://github.com/CQCL/hugr/pull/1116))
9 changes: 7 additions & 2 deletions hugr-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ categories = ["compilers"]

[dependencies]
clap = {workspace = true, features = ["derive"]}
clap-stdin = { workspace = true }
hugr-core = { path = "../hugr-core", version = "0.0.0" }
clap-stdin.workspace = true
clap-verbosity-flag.workspace = true
hugr-core = { path = "../hugr-core", version = "0.1.0" }
serde_json.workspace = true
thiserror.workspace = true

Expand All @@ -28,3 +29,7 @@ assert_cmd = { workspace = true }
assert_fs = { workspace = true }
predicates = { workspace = true }
rstest.workspace = true

[[bin]]
name = "hugr"
path = "src/main.rs"
34 changes: 31 additions & 3 deletions hugr-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@ hugr-cli
[![msrv][]](https://github.com/CQCL/hugr)
[![codecov][]](https://codecov.io/gh/CQCL/hugr)

This crate provides tooling used by the `hugr` CLI tool.
Refer to the [main crate](http://crates.io/crates/hugr) for more information.
`hugr` CLI tool for common tasks on serialised HUGR (e.g. validation,
visualisation).

Refer to the [main HUGR crate](http://crates.io/crates/hugr) for more information.

## Usage

Add the dependency to your project:
Install using `cargo`:

```bash
cargo install hugr-cli
```

This will install the `hugr` binary. Running `hugr --help` shows:

```
Validate a HUGR.
Usage: hugr [OPTIONS] <INPUT>
Arguments:
<INPUT>
Options:
-m, --mermaid Visualise with mermaid.
-n, --no-validate Skip validation.
-v, --verbose... Increase logging verbosity
-q, --quiet... Decrease logging verbosity
-h, --help Print help
-V, --version Print version
```


To extend the CLI you can also add the project as a library dependency:

```bash
cargo add hugr-cli
Expand Down
26 changes: 20 additions & 6 deletions hugr-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//! Standard command line tools, used by the hugr binary.
pub use clap::Parser;
use clap::Parser;
use clap_stdin::FileOrStdin;
use hugr_core::{extension::ExtensionRegistry, Hugr, HugrView};
use clap_verbosity_flag::Level;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use thiserror::Error;

use hugr_core::{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.")]
#[group(id = "hugr")]
pub struct CmdLineArgs {
input: FileOrStdin,
/// Visualise with mermaid.
Expand All @@ -16,6 +21,9 @@ pub struct CmdLineArgs {
/// Skip validation.
#[arg(short, long, help = "Skip validation.")]
no_validate: bool,
/// Verbosity.
#[command(flatten)]
verbose: Verbosity<InfoLevel>,
// TODO YAML extensions
}

Expand All @@ -38,17 +46,23 @@ pub const VALID_PRINT: &str = "HUGR valid!";

impl CmdLineArgs {
/// Run the HUGR cli and validate against an extension registry.
pub fn run(&self, registry: &ExtensionRegistry) -> Result<(), CliError> {
pub fn run(&self, registry: &ExtensionRegistry) -> Result<Hugr, CliError> {
let mut hugr: Hugr = serde_json::from_reader(self.input.into_reader()?)?;
if self.mermaid {
println!("{}", hugr.mermaid_string());
}

if !self.no_validate {
hugr.update_validate(registry)?;

println!("{}", VALID_PRINT);
if self.verbosity(Level::Info) {
eprintln!("{}", VALID_PRINT);
}
}
Ok(())
Ok(hugr)
}

/// Test whether a `level` message should be output.
pub fn verbosity(&self, level: Level) -> bool {
self.verbose.log_level_filter() >= level
}
}
15 changes: 10 additions & 5 deletions hugr/src/main.rs → hugr-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! Validate serialized HUGR on the command line
use hugr::std_extensions::arithmetic::{
use hugr_core::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_core::std_extensions::logic::EXTENSION as LOGICS_EXTENSION;

use hugr::extension::{ExtensionRegistry, PRELUDE};
use hugr_core::extension::{ExtensionRegistry, PRELUDE};

use hugr_cli::{CmdLineArgs, Parser};
use hugr_cli::CmdLineArgs;

use clap::Parser;
use clap_verbosity_flag::Level;

fn main() {
let opts = CmdLineArgs::parse();
Expand All @@ -27,7 +30,9 @@ fn main() {
.unwrap();

if let Err(e) = opts.run(&reg) {
eprintln!("{}", e);
if opts.verbosity(Level::Error) {
eprintln!("{}", e);
}
std::process::exit(1);
}
}
25 changes: 21 additions & 4 deletions hugr/tests/cli.rs → hugr-cli/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg(feature = "_cli")]
use assert_cmd::Command;
use assert_fs::{fixture::FileWriteStr, NamedTempFile};
use hugr_cli::VALID_PRINT;
Expand All @@ -15,7 +14,7 @@ use rstest::{fixture, rstest};

#[fixture]
fn cmd() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap()
Command::cargo_bin("hugr").unwrap()
}

#[fixture]
Expand Down Expand Up @@ -48,15 +47,23 @@ fn test_doesnt_exist(mut cmd: Command) {
#[rstest]
fn test_validate(test_hugr_file: NamedTempFile, mut cmd: Command) {
cmd.arg(test_hugr_file.path());
cmd.assert().success().stdout(contains(VALID_PRINT));
cmd.assert().success().stderr(contains(VALID_PRINT));
}

#[rstest]
fn test_stdin(test_hugr_string: String, mut cmd: Command) {
cmd.write_stdin(test_hugr_string);
cmd.arg("-");

cmd.assert().success().stdout(contains(VALID_PRINT));
cmd.assert().success().stderr(contains(VALID_PRINT));
}

#[rstest]
fn test_stdin_silent(test_hugr_string: String, mut cmd: Command) {
cmd.args(["-", "-q"]);
cmd.write_stdin(test_hugr_string);

cmd.assert().success().stderr(contains(VALID_PRINT).not());
}

#[rstest]
Expand Down Expand Up @@ -91,3 +98,13 @@ fn test_bad_json(mut cmd: Command) {
.failure()
.stderr(contains("Error parsing input"));
}

#[rstest]
fn test_bad_json_silent(mut cmd: Command) {
cmd.write_stdin(r#"{"foo": "bar"}"#);
cmd.args(["-", "-qqq"]);

cmd.assert()
.failure()
.stderr(contains("Error parsing input").not());
}
16 changes: 16 additions & 0 deletions hugr-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Changelog

## 0.1.0 (2024-05-29)

### Bug Fixes

- Set initial version of hugr-core to 0.1.0 ([#1129](https://github.com/CQCL/hugr/pull/1129))

### Features

- [**breaking**] Move cli in to hugr-cli sub-crate ([#1107](https://github.com/CQCL/hugr/pull/1107))
- Make internals of int ops and the "int" CustomType more public. ([#1114](https://github.com/CQCL/hugr/pull/1114))
- Unseal and make public the traits `HugrInternals` and `HugrMutInternals` ([#1122](https://github.com/CQCL/hugr/pull/1122))

### Refactor

- Add a `hugr-core` crate ([#1108](https://github.com/CQCL/hugr/pull/1108))
2 changes: 1 addition & 1 deletion hugr-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hugr-core"
version = "0.0.0"
version = "0.1.0"
edition = { workspace = true }
rust-version = { workspace = true }

Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions hugr-core/src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use super::{
BuildError, Container,
};

use crate::{
extension::ExtensionRegistry,
hugr::{hugrmut::sealed::HugrMutInternals, views::HugrView, ValidationError},
ops,
types::{PolyFuncType, Type, TypeBound},
};
use crate::extension::ExtensionRegistry;
use crate::hugr::internal::HugrMutInternals;
use crate::hugr::views::HugrView;
use crate::hugr::ValidationError;
use crate::ops;
use crate::types::{PolyFuncType, Type, TypeBound};

use crate::ops::handle::{AliasID, FuncID, NodeHandle};

Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/extension/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
//! ### Example
//!
//! ```yaml
#![doc = include_str!("../../../hugr/examples/extension/declarative.yaml")]
#![doc = include_str!("../../examples/extension/declarative.yaml")]
//! ```
//!
//! The definition can be loaded into a registry using the [`load_extensions`] or [`load_extensions_file`] functions.
//! ```rust
//! # const DECLARATIVE_YAML: &str = include_str!("../../../hugr/examples/extension/declarative.yaml");
//! # const DECLARATIVE_YAML: &str = include_str!("../../examples/extension/declarative.yaml");
//! # use hugr::extension::declarative::load_extensions;
//! // Required extensions must already be present in the registry.
//! let mut reg = hugr::std_extensions::logic::LOGIC_REG.clone();
Expand Down Expand Up @@ -327,7 +327,7 @@ extensions:
"#;

/// The yaml used in the module documentation.
const EXAMPLE_YAML_FILE: &str = "../hugr/examples/extension/declarative.yaml";
const EXAMPLE_YAML_FILE: &str = "examples/extension/declarative.yaml";

#[rstest]
#[case(EMPTY_YAML, 1, 0, 0, &PRELUDE_REGISTRY)]
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/extension/infer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::ops::{CustomOp, Lift, OpType};
use crate::{
builder::test::closed_dfg_root_hugr,
extension::prelude::PRELUDE_ID,
hugr::{hugrmut::sealed::HugrMutInternals, validate::ValidationError},
hugr::{internal::HugrMutInternals, validate::ValidationError},
ops::{dataflow::DataflowParent, handle::NodeHandle},
};

Expand Down
1 change: 1 addition & 0 deletions hugr-core/src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod hugrmut;

pub(crate) mod ident;
pub mod internal;
pub mod rewrite;
pub mod serialize;
pub mod validate;
Expand Down
Loading

0 comments on commit cab398c

Please sign in to comment.