1- use jsonschema:: { self , Validator } ;
1+ use jsonschema:: { self , Retrieve , Uri , ValidationError , Validator } ;
2+ use serde_json:: Value ;
3+ use std:: { collections:: HashMap , fmt} ;
4+
25use crate :: load:: must_load_data;
3- use std:: fmt;
46
57const DEFAULT_RULE_SCHEMA : & ' static str = include_str ! ( "../docs/namelint-rule-schema.yaml" ) ;
68const DEFAULT_CONFIG_SCHEMA : & ' static str = include_str ! ( "../docs/namelint-config-schema.yaml" ) ;
79
10+ #[ derive( PartialEq ) ]
811pub enum SchemaType {
912 Rule ,
1013 Config ,
@@ -17,6 +20,29 @@ impl SchemaType {
1720 SchemaType :: Config => "config" ,
1821 }
1922 }
23+ /*
24+ pub fn from_str(s: &str) -> Option<Self> {
25+ match s {
26+ "rule" => Some(SchemaType::Rule),
27+ "config" => Some(SchemaType::Config),
28+ _ => None,
29+ }
30+ }
31+
32+ pub fn get_validator(&self) -> Validator {
33+ match self {
34+ SchemaType::Rule => must_load_validator(SchemaType::Rule),
35+ SchemaType::Config => must_load_validator(SchemaType::Config),
36+ }
37+ }
38+ */
39+
40+ pub fn eq ( & self , other : & SchemaType ) -> bool {
41+ matches ! (
42+ ( self , other) ,
43+ ( SchemaType :: Rule , SchemaType :: Rule ) | ( SchemaType :: Config , SchemaType :: Config )
44+ )
45+ }
2046}
2147
2248impl fmt:: Display for SchemaType {
@@ -28,6 +54,33 @@ impl fmt::Display for SchemaType {
2854 }
2955}
3056
57+ struct RefRetriever {
58+ schemas : HashMap < String , Value > ,
59+ }
60+
61+
62+ impl Retrieve for RefRetriever {
63+ fn retrieve ( & self , uri : & Uri < String > ) -> Result < Value , Box < dyn std:: error:: Error + Send + Sync > > {
64+ self . schemas
65+ . get ( uri. as_str ( ) )
66+ . cloned ( )
67+ . ok_or_else ( || format ! ( "ERROF ref$ schema not found: {uri}" ) . into ( ) )
68+ }
69+ }
70+
71+ fn build_ref_retriever ( ) -> RefRetriever {
72+ let mut retriever = RefRetriever {
73+ schemas : HashMap :: new ( ) ,
74+ } ;
75+
76+ retriever. schemas . insert (
77+ "https://www.namelint.dev/namelint-rule-schema.json" . to_string ( ) ,
78+ must_load_data ( DEFAULT_RULE_SCHEMA , "yaml" , "ref" ) ,
79+ ) ;
80+
81+ retriever
82+ }
83+
3184pub fn must_load_validator ( schema_type : SchemaType ) -> Validator {
3285
3386 let schema_str = match schema_type {
@@ -37,7 +90,18 @@ pub fn must_load_validator(schema_type: SchemaType) -> Validator {
3790
3891 let schema = must_load_data ( & schema_str, "yaml" , schema_type. as_str ( ) ) ;
3992
40- let validator = jsonschema:: validator_for ( & schema) ;
93+ let validator: Result < Validator , ValidationError > ;
94+ if schema_type == SchemaType :: Config {
95+ // Add the rule schema to the config schema
96+ //let rule_schema = must_load_data(DEFAULT_RULE_SCHEMA, "yaml", "built-in");
97+ //let retriever = InMemoryRetriever{ schema: rule_schema };
98+ validator = jsonschema:: options ( )
99+ . with_retriever ( build_ref_retriever ( ) )
100+ . build ( & schema) ;
101+ } else {
102+ validator = jsonschema:: validator_for ( & schema) ;
103+ }
104+
41105 if validator. is_err ( ) {
42106 println ! ( "ERROR: unable to load schema validator '{}': {}" , schema_type, validator. err( ) . unwrap( ) ) ;
43107 std:: process:: exit ( 1 ) ;
0 commit comments