11//! Bundles of hugr modules along with the extension required to load them.
22
33use derive_more:: { Display , Error , From } ;
4- use itertools:: Itertools ;
54use std:: io;
65
76use crate :: envelope:: { read_envelope, write_envelope, EnvelopeConfig , EnvelopeError } ;
8- use crate :: extension:: { ExtensionId , ExtensionRegistry , PRELUDE_REGISTRY } ;
7+ use crate :: extension:: { ExtensionRegistry , PRELUDE_REGISTRY } ;
98use crate :: hugr:: { HugrView , ValidationError } ;
109use crate :: { Hugr , Node } ;
1110
@@ -23,24 +22,25 @@ pub struct Package {
2322impl Package {
2423 /// Create a new package from a list of hugrs.
2524 ///
26- /// Collects the extensions used in the modules and stores them in top-level
27- /// `extensions` attribute.
25+ /// The hugr extensions are not automatically added to the package. Make
26+ /// sure to manually include any non-standard extensions to
27+ /// [`Package::extensions`].
2828 pub fn new ( modules : impl IntoIterator < Item = Hugr > ) -> Self {
2929 let modules: Vec < Hugr > = modules. into_iter ( ) . collect ( ) ;
30- let mut extensions = ExtensionRegistry :: default ( ) ;
31- for module in & modules {
32- extensions. extend ( module. extensions ( ) ) ;
33- }
3430 Self {
3531 modules,
36- extensions,
32+ extensions : ExtensionRegistry :: default ( ) ,
3733 }
3834 }
3935
4036 /// Create a new package containing a single HUGR.
37+ ///
38+ /// The hugr extensions are not automatically added to the package. Make
39+ /// sure to manually include any non-standard extensions to
40+ /// [`Package::extensions`].
4141 pub fn from_hugr ( hugr : Hugr ) -> Self {
4242 Package {
43- extensions : hugr . extensions ( ) . clone ( ) ,
43+ extensions : ExtensionRegistry :: default ( ) ,
4444 modules : vec ! [ hugr] ,
4545 }
4646 }
@@ -51,19 +51,6 @@ impl Package {
5151 pub fn validate ( & self ) -> Result < ( ) , PackageValidationError > {
5252 for hugr in self . modules . iter ( ) {
5353 hugr. validate ( ) ?;
54-
55- let missing_exts = hugr
56- . extensions ( )
57- . ids ( )
58- . filter ( |id| !self . extensions . contains ( id) )
59- . cloned ( )
60- . collect_vec ( ) ;
61- if !missing_exts. is_empty ( ) {
62- return Err ( PackageValidationError :: MissingExtension {
63- missing : missing_exts,
64- available : self . extensions . ids ( ) . cloned ( ) . collect ( ) ,
65- } ) ;
66- }
6754 }
6855 Ok ( ( ) )
6956 }
@@ -126,18 +113,6 @@ impl AsRef<[Hugr]> for Package {
126113#[ derive( Debug , Display , From , Error ) ]
127114#[ non_exhaustive]
128115pub enum PackageValidationError {
129- /// Error raised while processing the package extensions.
130- #[ display( "The package modules use the extension{} {} not present in the defined set. The declared extensions are {}" ,
131- if missing. len( ) > 1 { "s" } else { "" } ,
132- missing. iter( ) . map( |id| id. to_string( ) ) . collect:: <Vec <_>>( ) . join( ", " ) ,
133- available. iter( ) . map( |id| id. to_string( ) ) . collect:: <Vec <_>>( ) . join( ", " ) ,
134- ) ]
135- MissingExtension {
136- /// The missing extensions.
137- missing : Vec < ExtensionId > ,
138- /// The available extensions.
139- available : Vec < ExtensionId > ,
140- } ,
141116 /// Error raised while validating the package hugrs.
142117 Validation ( ValidationError < Node > ) ,
143118}
0 commit comments