@@ -15,7 +15,7 @@ use crate::{
15
15
cache:: { SharedUriCache , UriCache } ,
16
16
hasher:: BuildNoHashHasher ,
17
17
list:: List ,
18
- meta,
18
+ meta:: { self , metas_for_draft } ,
19
19
resource:: { unescape_segment, InnerResourcePtr , JsonSchemaResource } ,
20
20
uri,
21
21
vocabularies:: { self , VocabularySet } ,
@@ -44,34 +44,8 @@ type DocumentStore = AHashMap<Arc<Uri<String>>, Pin<Arc<ValueWrapper>>>;
44
44
type ResourceMap = AHashMap < Arc < Uri < String > > , InnerResourcePtr > ;
45
45
46
46
/// Pre-loaded registry containing all JSON Schema meta-schemas and their vocabularies
47
- pub static SPECIFICATIONS : Lazy < Registry > = Lazy :: new ( || {
48
- let pairs = meta:: META_SCHEMAS . into_iter ( ) . map ( |( uri, schema) | {
49
- (
50
- uri,
51
- ResourceRef :: from_contents ( schema) . expect ( "Invalid resource" ) ,
52
- )
53
- } ) ;
54
-
55
- // The capacity is known upfront
56
- let mut documents = DocumentStore :: with_capacity ( 18 ) ;
57
- let mut resources = ResourceMap :: with_capacity ( 18 ) ;
58
- let mut anchors = AHashMap :: with_capacity ( 8 ) ;
59
- let mut resolution_cache = UriCache :: with_capacity ( 35 ) ;
60
- process_meta_schemas (
61
- pairs,
62
- & mut documents,
63
- & mut resources,
64
- & mut anchors,
65
- & mut resolution_cache,
66
- )
67
- . expect ( "Failed to process meta schemas" ) ;
68
- Registry {
69
- documents,
70
- resources,
71
- anchors,
72
- resolution_cache : resolution_cache. into_shared ( ) ,
73
- }
74
- } ) ;
47
+ pub static SPECIFICATIONS : Lazy < Registry > =
48
+ Lazy :: new ( || Registry :: build_from_meta_schemas ( meta:: META_SCHEMAS_ALL . as_slice ( ) ) ) ;
75
49
76
50
/// A registry of JSON Schema resources, each identified by their canonical URIs.
77
51
///
@@ -563,6 +537,42 @@ impl Registry {
563
537
_ => unreachable ! ( ) ,
564
538
}
565
539
}
540
+
541
+ /// Build a registry with all the given meta-schemas from specs.
542
+ pub ( crate ) fn build_from_meta_schemas ( schemas : & [ ( & ' static str , & ' static Value ) ] ) -> Self {
543
+ let schemas_count = schemas. len ( ) ;
544
+ let pairs = schemas. iter ( ) . map ( |( uri, schema) | {
545
+ (
546
+ uri,
547
+ ResourceRef :: from_contents ( schema) . expect ( "Invalid resource" ) ,
548
+ )
549
+ } ) ;
550
+
551
+ let mut documents = DocumentStore :: with_capacity ( schemas_count) ;
552
+ let mut resources = ResourceMap :: with_capacity ( schemas_count) ;
553
+
554
+ // The actual number of anchors and cache-entries varies across
555
+ // drafts. We overshoot here to avoid reallocations, using the sum
556
+ // over all specifications.
557
+ let mut anchors = AHashMap :: with_capacity ( 8 ) ;
558
+ let mut resolution_cache = UriCache :: with_capacity ( 35 ) ;
559
+
560
+ process_meta_schemas (
561
+ pairs,
562
+ & mut documents,
563
+ & mut resources,
564
+ & mut anchors,
565
+ & mut resolution_cache,
566
+ )
567
+ . expect ( "Failed to process meta schemas" ) ;
568
+
569
+ Self {
570
+ documents,
571
+ resources,
572
+ anchors,
573
+ resolution_cache : resolution_cache. into_shared ( ) ,
574
+ }
575
+ }
566
576
}
567
577
568
578
fn process_meta_schemas (
@@ -706,15 +716,18 @@ fn handle_metaschemas(
706
716
refers_metaschemas : bool ,
707
717
resources : & mut ResourceMap ,
708
718
anchors : & mut AHashMap < AnchorKey , Anchor > ,
719
+ draft_version : Draft ,
709
720
) {
710
721
if refers_metaschemas {
711
- resources. reserve ( SPECIFICATIONS . resources . len ( ) ) ;
712
- for ( key, resource) in & SPECIFICATIONS . resources {
713
- resources. insert ( Arc :: clone ( key) , resource. clone ( ) ) ;
722
+ let schemas = metas_for_draft ( draft_version) ;
723
+ let draft_registry = Registry :: build_from_meta_schemas ( schemas) ;
724
+ resources. reserve ( draft_registry. resources . len ( ) ) ;
725
+ for ( key, resource) in draft_registry. resources {
726
+ resources. insert ( key, resource. clone ( ) ) ;
714
727
}
715
- anchors. reserve ( SPECIFICATIONS . anchors . len ( ) ) ;
716
- for ( key, anchor) in & SPECIFICATIONS . anchors {
717
- anchors. insert ( key. clone ( ) , anchor. clone ( ) ) ;
728
+ anchors. reserve ( draft_registry . anchors . len ( ) ) ;
729
+ for ( key, anchor) in draft_registry . anchors {
730
+ anchors. insert ( key, anchor) ;
718
731
}
719
732
}
720
733
}
@@ -783,7 +796,7 @@ fn process_resources(
783
796
}
784
797
}
785
798
786
- handle_metaschemas ( state. refers_metaschemas , resources, anchors) ;
799
+ handle_metaschemas ( state. refers_metaschemas , resources, anchors, default_draft ) ;
787
800
788
801
Ok ( ( ) )
789
802
}
@@ -860,7 +873,7 @@ async fn process_resources_async(
860
873
}
861
874
}
862
875
863
- handle_metaschemas ( state. refers_metaschemas , resources, anchors) ;
876
+ handle_metaschemas ( state. refers_metaschemas , resources, anchors, default_draft ) ;
864
877
865
878
Ok ( ( ) )
866
879
}
0 commit comments