File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change 35
35
from dataclasses import dataclass
36
36
from enum import Enum
37
37
from typing import TYPE_CHECKING , ClassVar
38
+ import json
38
39
39
40
import zstd
40
41
@@ -53,13 +54,19 @@ def make_envelope(package: Package, config: EnvelopeConfig) -> bytes:
53
54
payload : bytes
54
55
match config .format :
55
56
case EnvelopeFormat .JSON :
56
- json = package ._to_serial ().model_dump_json ()
57
+ json_str = package ._to_serial ().model_dump_json ()
57
58
# This introduces an extra encode/decode roundtrip when calling
58
59
# `make_envelope_str`, but we prioritize speed for binary formats.
59
- payload = json .encode ("utf-8" )
60
- case EnvelopeFormat .MODULE | EnvelopeFormat .MODULE_WITH_EXTS :
61
- msg = "MODULE encoding of HUGR envelopes is not supported yet."
62
- raise ValueError (msg )
60
+ payload = json_str .encode ("utf-8" )
61
+
62
+ case EnvelopeFormat .MODULE :
63
+ return bytes (package .to_model ())
64
+
65
+ case EnvelopeFormat .MODULE_WITH_EXTS :
66
+ package_bytes = bytes (package .to_model ())
67
+ extension_str = json .dumps ([ext .model_dump () for ext in package .extensions ])
68
+ extension_bytes = extension_str .encode ("utf8" )
69
+ return package_bytes + extension_bytes
63
70
64
71
if config .zstd is not None :
65
72
payload = zstd .compress (payload , config .zstd )
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ def _deprecated(func):
25
25
read_envelope_str ,
26
26
)
27
27
from hugr .ops import FuncDecl , FuncDefn , Op
28
+ import hugr .model as model
28
29
29
30
if TYPE_CHECKING :
30
31
from hugr .ext import Extension
@@ -127,6 +128,13 @@ def from_json(cls, json_str: str) -> Package:
127
128
"""
128
129
return ext_s .Package .model_validate_json (json_str ).deserialize ()
129
130
131
+ def to_model (self ) -> model .Package :
132
+ """Export the package as its hugr model representation.
133
+
134
+ At the moment this does not yet contain the extensions.
135
+ """
136
+ return model .Package ([module .to_model () for module in self .modules ])
137
+
130
138
131
139
@dataclass (frozen = True )
132
140
class PackagePointer :
You can’t perform that action at this time.
0 commit comments