Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed May 7, 2024
1 parent 4e1227b commit 00bd27d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion hugr-py/src/hugr/serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC
from typing import Any, Literal

from pydantic import Field, RootModel, ConfigDict
from pydantic import Field, RootModel

from . import tys
from .tys import (
Expand Down Expand Up @@ -73,6 +73,7 @@ class FuncDecl(BaseOp):
class CustomConst(ConfiguredBaseModel):
c: str
v: Any

class Config:
json_schema_extra = {
"required": ["c", "v"],
Expand Down
3 changes: 1 addition & 2 deletions hugr/src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::extension::ExtensionSet;
use crate::types::{CustomType, EdgeKind, FunctionType, SumType, SumTypeError, Type};
use crate::{Hugr, HugrView};

use delegate::delegate;
use itertools::Itertools;
use serde::{Deserializer, Serializer};
use smol_str::SmolStr;
use thiserror::Error;
use delegate::delegate;

pub use custom::{downcast_equal_consts, CustomConst, CustomSerialized};

Expand Down Expand Up @@ -81,7 +81,6 @@ impl OpTrait for Const {
}
}


impl From<Const> for Value {
fn from(konst: Const) -> Self {
konst.value
Expand Down
15 changes: 8 additions & 7 deletions hugr/src/ops/constant/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ fn deserialize_custom_const<CC: CustomConst>(
Err(dyn_cc) => {
// TODO we should return dyn_cc in the error
Err(<serde_yaml::Error as serde::de::Error>::custom(format!(
"Failed to deserialize: {:?}",
"Failed to deserialize [{}]: {:?}",
std::any::type_name::<CC>(),
dyn_cc
)))
}
Expand Down Expand Up @@ -130,11 +131,11 @@ pub enum CustomSerializedError {
}

impl CustomSerializedError {
fn new_serialize_err(err: serde_yaml::Error, payload: Box<dyn CustomConst>) -> Self {
fn new_ser(err: serde_yaml::Error, payload: Box<dyn CustomConst>) -> Self {
Self::SerializePayloadError { err, payload }
}

fn new_deserialize_err(err: serde_yaml::Error, payload: serde_yaml::Value) -> Self {
fn new_de(err: serde_yaml::Error, payload: serde_yaml::Value) -> Self {
Self::DeserializePayloadError { err, payload }
}
}
Expand Down Expand Up @@ -178,7 +179,7 @@ impl CustomSerialized {
let (typ, extension_reqs) = (cc.get_type(), cc.extension_reqs());
let value = cc
.serialize_dyn_custom_const()
.map_err(|err| CustomSerializedError::new_serialize_err(err, cc))?;
.map_err(|err| CustomSerializedError::new_ser(err, cc))?;
Ok(Self::new(typ, value, extension_reqs))
}
}
Expand All @@ -194,7 +195,7 @@ impl CustomSerialized {
// ideally we would not have to clone, but serde_json does not allow us
// to recover the value from the error
let cc_box = deserialize_dyn_custom_const(value.clone())
.map_err(|err| CustomSerializedError::new_deserialize_err(err, value))?;
.map_err(|err| CustomSerializedError::new_de(err, value))?;
assert_eq!(cc_box.get_type(), typ);
assert_eq!(cc_box.extension_reqs(), extensions);
Ok(cc_box)
Expand All @@ -210,7 +211,7 @@ impl CustomSerialized {
// ideally we would not have to clone, but serde_json does not allow us
// to recover the value from the error
let cc: CC = deserialize_custom_const(value.clone())
.map_err(|err| CustomSerializedError::new_deserialize_err(err, value))?;
.map_err(|err| CustomSerializedError::new_de(err, value))?;
assert_eq!(cc.get_type(), typ);
assert_eq!(cc.extension_reqs(), extensions);
Ok(cc)
Expand Down Expand Up @@ -262,7 +263,7 @@ impl From<CustomSerialized> for Box<dyn CustomConst> {

#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};


use crate::{
ops::constant::custom::{
Expand Down

0 comments on commit 00bd27d

Please sign in to comment.