Skip to content

Commit 874e3e8

Browse files
committed
Rust working with updated schemas
1 parent 8a46cd8 commit 874e3e8

3 files changed

Lines changed: 69 additions & 4 deletions

File tree

config/self.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ lints:
1515
- no-msdos-reserved
1616
- no-whitespace
1717
- posix-portable
18+
- ext-ascii-lower

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[allow(dead_code)]
1+
#![allow(unused)]
22

33
mod safe_name;
44
mod load;

src/schema.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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+
25
use crate::load::must_load_data;
3-
use std::fmt;
46

57
const DEFAULT_RULE_SCHEMA: &'static str = include_str!("../docs/namelint-rule-schema.yaml");
68
const DEFAULT_CONFIG_SCHEMA: &'static str = include_str!("../docs/namelint-config-schema.yaml");
79

10+
#[derive(PartialEq)]
811
pub 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

2248
impl 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+
3184
pub 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

Comments
 (0)