diff --git a/hugr-py/src/hugr/serialization/ops.py b/hugr-py/src/hugr/serialization/ops.py index 660a30f79..85750436e 100644 --- a/hugr-py/src/hugr/serialization/ops.py +++ b/hugr-py/src/hugr/serialization/ops.py @@ -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 ( @@ -73,6 +73,7 @@ class FuncDecl(BaseOp): class CustomConst(ConfiguredBaseModel): c: str v: Any + class Config: json_schema_extra = { "required": ["c", "v"], diff --git a/hugr/src/ops/constant.rs b/hugr/src/ops/constant.rs index a6361511c..4685039bb 100644 --- a/hugr/src/ops/constant.rs +++ b/hugr/src/ops/constant.rs @@ -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}; @@ -81,7 +81,6 @@ impl OpTrait for Const { } } - impl From for Value { fn from(konst: Const) -> Self { konst.value diff --git a/hugr/src/ops/constant/custom.rs b/hugr/src/ops/constant/custom.rs index fa4a10312..2146d3fd7 100644 --- a/hugr/src/ops/constant/custom.rs +++ b/hugr/src/ops/constant/custom.rs @@ -83,7 +83,8 @@ fn deserialize_custom_const( Err(dyn_cc) => { // TODO we should return dyn_cc in the error Err(::custom(format!( - "Failed to deserialize: {:?}", + "Failed to deserialize [{}]: {:?}", + std::any::type_name::(), dyn_cc ))) } @@ -130,11 +131,11 @@ pub enum CustomSerializedError { } impl CustomSerializedError { - fn new_serialize_err(err: serde_yaml::Error, payload: Box) -> Self { + fn new_ser(err: serde_yaml::Error, payload: Box) -> 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 } } } @@ -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)) } } @@ -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) @@ -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) @@ -262,7 +263,7 @@ impl From for Box { #[cfg(test)] mod test { - use serde::{Deserialize, Serialize}; + use crate::{ ops::constant::custom::{