Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hugr-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ordered-float = { workspace = true }
pest = { workspace = true }
pest_derive = { workspace = true }
pretty = { workspace = true }
semver = { workspace = true }
smol_str = { workspace = true, features = ["serde"] }
thiserror.workspace = true
pyo3 = { workspace = true, optional = true, features = ["extension-module"] }
Expand Down
1 change: 1 addition & 0 deletions hugr-model/FORMAT_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
21 changes: 19 additions & 2 deletions hugr-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
//! The data model of the HUGR intermediate representation.
//!
//! This crate defines data structures that capture the structure of a HUGR graph and
//! all its associated information in a form that can be stored on disk. The data structures
//! are not designed for efficient traversal or modification, but for simplicity and serialization.
//!
//! This crate supports version `
#![doc = include_str!("../FORMAT_VERSION")]
//! ` of the HUGR model format.
mod capnp;

pub mod v0;
mod version;
pub use version::*;

use std::sync::LazyLock;

// This is required here since the generated code assumes it's in the package root.
use capnp::hugr_v0_capnp;

/// The current version of the HUGR model format.
pub static CURRENT_VERSION: LazyLock<semver::Version> = LazyLock::new(|| {
// We allow non-zero patch versions, but ignore them for compatibility checks.
let v = semver::Version::parse(include_str!("../FORMAT_VERSION").trim())
.expect("`FORMAT_VERSION` in `hugr-model` contains version that fails to parse");
assert!(
v.pre.is_empty(),
"`FORMAT_VERSION` in `hugr-model` should not have a pre-release version"
);
v
});
15 changes: 7 additions & 8 deletions hugr-model/src/v0/binary/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::capnp::hugr_v0_capnp as hugr_capnp;
use crate::v0::table;
use crate::{Version, v0 as model};
use crate::{CURRENT_VERSION, v0 as model};
use bumpalo::Bump;
use bumpalo::collections::Vec as BumpVec;
use std::io::BufRead;
Expand All @@ -18,9 +18,9 @@ pub enum ReadError {
#[display("Can not read file with version {actual} (tooling version {current}).")]
VersionError {
/// The current version of the hugr-model format.
current: Version,
current: semver::Version,
/// The version of the hugr-model format in the file.
actual: Version,
actual: semver::Version,
},
}

Expand Down Expand Up @@ -68,11 +68,10 @@ fn read_package<'a>(
reader: hugr_capnp::package::Reader,
) -> ReadResult<table::Package<'a>> {
let version = read_version(reader.get_version()?)?;
let current_version = Version::current();

if version.major != current_version.major || version.minor > current_version.minor {
if version.major != CURRENT_VERSION.major || version.minor > CURRENT_VERSION.minor {
return Err(ReadError::VersionError {
current: current_version,
current: CURRENT_VERSION.clone(),
actual: version,
});
}
Expand All @@ -86,10 +85,10 @@ fn read_package<'a>(
Ok(table::Package { modules })
}

fn read_version(reader: hugr_capnp::version::Reader) -> ReadResult<Version> {
fn read_version(reader: hugr_capnp::version::Reader) -> ReadResult<semver::Version> {
let major = reader.get_major();
let minor = reader.get_minor();
Ok(Version { minor, major })
Ok(semver::Version::new(major as u64, minor as u64, 0))
}

fn read_module<'a>(
Expand Down
10 changes: 5 additions & 5 deletions hugr-model/src/v0/binary/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::Write;

use crate::Version;
use crate::CURRENT_VERSION;
use crate::capnp::hugr_v0_capnp as hugr_capnp;
use crate::v0::{self as model, table};

Expand Down Expand Up @@ -47,12 +47,12 @@ pub fn write_to_vec(package: &table::Package) -> Vec<u8> {

fn write_package(mut builder: hugr_capnp::package::Builder, package: &table::Package) {
write_list!(builder, init_modules, write_module, package.modules);
write_version(builder.init_version(), Version::current());
write_version(builder.init_version(), &CURRENT_VERSION);
}

fn write_version(mut builder: hugr_capnp::version::Builder, version: Version) {
builder.set_major(version.major);
builder.set_minor(version.minor);
fn write_version(mut builder: hugr_capnp::version::Builder, version: &semver::Version) {
builder.set_major(version.major as u32);
builder.set_minor(version.minor as u32);
}

fn write_module(mut builder: hugr_capnp::module::Builder, module: &table::Module) {
Expand Down
74 changes: 0 additions & 74 deletions hugr-model/src/version.rs

This file was deleted.

1 change: 0 additions & 1 deletion hugr-model/version.txt

This file was deleted.

Loading