Skip to content

Commit

Permalink
Comments in export/import
Browse files Browse the repository at this point in the history
  • Loading branch information
zrho committed Jan 13, 2025
1 parent be3ee6f commit 90bd24b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 4 additions & 1 deletion hugr-core/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
types::{
type_param::{TypeArgVariable, TypeParam},
type_row::TypeRowBase,
CustomType, FuncTypeBase, MaybeRV, PolyFuncTypeBase, RowVariable, SumType, Type, TypeArg,
CustomType, FuncTypeBase, MaybeRV, PolyFuncTypeBase, RowVariable, SumType, TypeArg,
TypeBase, TypeBound, TypeEnum, TypeRow,
},
Direction, Hugr, HugrView, IncomingPort, Node, Port,
Expand Down Expand Up @@ -1028,6 +1028,9 @@ impl<'a> Context<'a> {
fn export_value(&mut self, value: &'a Value) -> model::TermId {
match value {
Value::Extension { e } => {
// NOTE: We have special cased arrays, integers, and floats for now.
// TODO: Allow arbitrary extension values to be exported as terms.

if let Some(array) = e.value().downcast_ref::<ArrayValue>() {
let len = self.make_term(model::Term::Nat(array.get_contents().len() as u64));
let element_type = self.export_type(array.get_element_type());
Expand Down
5 changes: 4 additions & 1 deletion hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,9 @@ impl<'a> Context<'a> {
}
}

// NOTE: We have special cased arrays, integers, and floats for now.
// TODO: Allow arbitrary extension values to be imported from terms.

if symbol_name == "collections.array.const" {
let element_type_term =
args.first().ok_or(model::ModelError::TypeError(term_id))?;
Expand Down Expand Up @@ -1409,7 +1412,7 @@ impl<'a> Context<'a> {
return Ok(ConstF64::new(value).into());
}

Err(error_unsupported!("constant value that is not JSON data"))
Err(error_unsupported!("unknown custom constant value"))
// TODO: This should ultimately include the following cases:
// - function definitions
// - custom constructors for values
Expand Down
15 changes: 9 additions & 6 deletions hugr/examples/qv.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
#![allow(missing_docs)]

use bumpalo::Bump;
use hugr::{package::Package, std_extensions::STD_REG, Hugr, HugrView};
use hugr::{package::Package, std_extensions::STD_REG, Hugr};
use hugr_core::{export::export_hugr, import::import_hugr};
use hugr_model::v0 as model;

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let bump = Bump::new();
let hugr = load_qv();

let module = export_hugr(&hugr, &bump);
let imported = import_hugr(&module, &STD_REG).unwrap();
let exported_again = export_hugr(&imported, &bump);

std::fs::write(
"qv_hugr.edn",
model::text::print_to_string(&module, 120).unwrap(),
model::text::print_to_string(&exported_again, 120).unwrap(),
)
.unwrap();

let binary = model::binary::write_to_vec(&module);
println!("size in bytes: {}", binary.len());
std::fs::write("qv_hugr.hugr", binary).unwrap();
// let binary = model::binary::write_to_vec(&module);
// println!("size in bytes: {}", binary.len());
// std::fs::write("qv_hugr.hugr", binary).unwrap();

// println!("{:#?}", module);
Ok(())
}

Expand Down

0 comments on commit 90bd24b

Please sign in to comment.