@@ -676,99 +676,112 @@ fn package_protocol_contracts_match(
676676 } )
677677}
678678
679- pub ( super ) fn collect_package_type_contracts (
679+ /// Shared driver for the per-`Item` contract collectors.
680+ ///
681+ /// Iterates the sources matching `kind`, parses each, and for every item runs
682+ /// `extract`. When `extract` yields `Some((name, contract))`, the entry is
683+ /// inserted using the same visibility rule every collector shares: interface
684+ /// files always contribute, source files only when the declaration is public.
685+ /// The `is_public` flag is supplied by `extract` so each variant decides how to
686+ /// read it from its own declaration.
687+ fn collect_package_item_contracts < T > (
680688 sources : & [ PackageSource ] ,
681689 kind : PackageReviewFileKind ,
682- ) -> BTreeMap < String , PackageTypeContract > {
690+ extract : impl Fn ( Item ) -> Option < ( String , bool , T ) > ,
691+ ) -> BTreeMap < String , T > {
683692 let mut contracts = BTreeMap :: new ( ) ;
684693 for source in sources. iter ( ) . filter ( |source| source. kind == kind) {
685694 let program = parse_source ( & source. path , & source. contents ) ;
686695 for item in program. items {
687- let Item :: Type ( type_decl ) = item else {
696+ let Some ( ( name , is_public , contract ) ) = extract ( item) else {
688697 continue ;
689698 } ;
690- if kind == PackageReviewFileKind :: Interface || type_decl . is_public {
691- contracts. insert ( type_decl . name . clone ( ) , package_type_contract ( & type_decl ) ) ;
699+ if kind == PackageReviewFileKind :: Interface || is_public {
700+ contracts. insert ( name, contract ) ;
692701 }
693702 }
694703 }
695704 contracts
696705}
697706
707+ pub ( super ) fn collect_package_type_contracts (
708+ sources : & [ PackageSource ] ,
709+ kind : PackageReviewFileKind ,
710+ ) -> BTreeMap < String , PackageTypeContract > {
711+ collect_package_item_contracts ( sources, kind, |item| {
712+ let Item :: Type ( type_decl) = item else {
713+ return None ;
714+ } ;
715+ Some ( (
716+ type_decl. name . clone ( ) ,
717+ type_decl. is_public ,
718+ package_type_contract ( & type_decl) ,
719+ ) )
720+ } )
721+ }
722+
698723pub ( super ) fn collect_package_function_contracts (
699724 sources : & [ PackageSource ] ,
700725 kind : PackageReviewFileKind ,
701726) -> BTreeMap < String , PackageFunctionContract > {
702- let mut contracts = BTreeMap :: new ( ) ;
703- for source in sources. iter ( ) . filter ( |source| source. kind == kind) {
704- let program = parse_source ( & source. path , & source. contents ) ;
705- for item in program. items {
706- let Item :: Function ( function) = item else {
707- continue ;
708- } ;
709- if kind == PackageReviewFileKind :: Interface || function. is_public {
710- contracts. insert ( function. name . clone ( ) , package_function_contract ( & function) ) ;
711- }
712- }
713- }
714- contracts
727+ collect_package_item_contracts ( sources, kind, |item| {
728+ let Item :: Function ( function) = item else {
729+ return None ;
730+ } ;
731+ Some ( (
732+ function. name . clone ( ) ,
733+ function. is_public ,
734+ package_function_contract ( & function) ,
735+ ) )
736+ } )
715737}
716738
717739pub ( super ) fn collect_package_sum_type_contracts (
718740 sources : & [ PackageSource ] ,
719741 kind : PackageReviewFileKind ,
720742) -> BTreeMap < String , PackageSumTypeContract > {
721- let mut contracts = BTreeMap :: new ( ) ;
722- for source in sources. iter ( ) . filter ( |source| source. kind == kind) {
723- let program = parse_source ( & source. path , & source. contents ) ;
724- for item in program. items {
725- let Item :: SumType ( sum_type) = item else {
726- continue ;
727- } ;
728- if kind == PackageReviewFileKind :: Interface || sum_type. is_public {
729- contracts. insert ( sum_type. name . clone ( ) , package_sum_type_contract ( & sum_type) ) ;
730- }
731- }
732- }
733- contracts
743+ collect_package_item_contracts ( sources, kind, |item| {
744+ let Item :: SumType ( sum_type) = item else {
745+ return None ;
746+ } ;
747+ Some ( (
748+ sum_type. name . clone ( ) ,
749+ sum_type. is_public ,
750+ package_sum_type_contract ( & sum_type) ,
751+ ) )
752+ } )
734753}
735754
736755pub ( super ) fn collect_package_type_alias_contracts (
737756 sources : & [ PackageSource ] ,
738757 kind : PackageReviewFileKind ,
739758) -> BTreeMap < String , PackageTypeAliasContract > {
740- let mut contracts = BTreeMap :: new ( ) ;
741- for source in sources. iter ( ) . filter ( |source| source. kind == kind) {
742- let program = parse_source ( & source. path , & source. contents ) ;
743- for item in program. items {
744- let Item :: TypeAlias ( alias) = item else {
745- continue ;
746- } ;
747- if kind == PackageReviewFileKind :: Interface || alias. is_public {
748- contracts. insert ( alias. name . clone ( ) , package_type_alias_contract ( & alias) ) ;
749- }
750- }
751- }
752- contracts
759+ collect_package_item_contracts ( sources, kind, |item| {
760+ let Item :: TypeAlias ( alias) = item else {
761+ return None ;
762+ } ;
763+ Some ( (
764+ alias. name . clone ( ) ,
765+ alias. is_public ,
766+ package_type_alias_contract ( & alias) ,
767+ ) )
768+ } )
753769}
754770
755771pub ( super ) fn collect_package_const_contracts (
756772 sources : & [ PackageSource ] ,
757773 kind : PackageReviewFileKind ,
758774) -> BTreeMap < String , PackageConstContract > {
759- let mut contracts = BTreeMap :: new ( ) ;
760- for source in sources. iter ( ) . filter ( |source| source. kind == kind) {
761- let program = parse_source ( & source. path , & source. contents ) ;
762- for item in program. items {
763- let Item :: Const ( const_decl) = item else {
764- continue ;
765- } ;
766- if kind == PackageReviewFileKind :: Interface || const_decl. is_public {
767- contracts. insert ( const_decl. name . clone ( ) , package_const_contract ( & const_decl) ) ;
768- }
769- }
770- }
771- contracts
775+ collect_package_item_contracts ( sources, kind, |item| {
776+ let Item :: Const ( const_decl) = item else {
777+ return None ;
778+ } ;
779+ Some ( (
780+ const_decl. name . clone ( ) ,
781+ const_decl. is_public ,
782+ package_const_contract ( & const_decl) ,
783+ ) )
784+ } )
772785}
773786
774787pub ( super ) fn collect_package_protocol_impl_contracts (
0 commit comments