Debian Codegraph @

src/keywords/pattern.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#;
2
3 use craterust-analyzer cargo jsonschema 0.37.4 crate/::{
4 compilerrust-analyzer cargo jsonschema 0.37.4 compiler/,
5 errorrust-analyzer cargo jsonschema 0.37.4 error/::ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#,
6 keywordsrust-analyzer cargo jsonschema 0.37.4 keywords/::CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#,
7 optionsrust-analyzer cargo jsonschema 0.37.4 options/::PatternEngineOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternEngineOptions#,
8 pathsrust-analyzer cargo jsonschema 0.37.4 paths/::{LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#},
9 regexrust-analyzer cargo jsonschema 0.37.4 regex/::{RegexEnginerust-analyzer cargo jsonschema 0.37.4 regex/RegexEngine#, RegexErrorrust-analyzer cargo jsonschema 0.37.4 regex/RegexError#},
10 typesrust-analyzer cargo jsonschema 0.37.4 types/::JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#,
11 validatorrust-analyzer cargo jsonschema 0.37.4 validator/::{Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate#, ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#},
12 };
13 use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::{Maprust-analyzer cargo serde_json 1.0.145 map/Map#, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#};
14
15 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) struct PatternValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#<R> {
16 regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<R>,
17 locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#location.: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#,
18 }
19
20 impl<R: RegexEnginerust-analyzer cargo jsonschema 0.37.4 regex/RegexEngine#> Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate# for PatternValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#<R> {
21 fn validaterust-analyzer cargo jsonschema 0.37.4 keywords/pattern/impl#[`PatternValidator<R>`][Validate]validate().<'i>(
22 &self,
23 instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
24 location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#,
25 _ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#,
26 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'i>> {
27 if let Valuerust-analyzer cargo serde_json 1.0.145 value/Value#::Stringrust-analyzer cargo serde_json 1.0.145 value/Value#String#(item) = instance {
28 match self.regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex..is_matchrust-analyzer cargo jsonschema 0.37.4 regex/RegexEngine#is_match().(item) {
29 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(is_match) => {
30 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_match {
31 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::patternrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]pattern().(
32 self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(),
33 location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
34 instance,
35 self.regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex..patternrust-analyzer cargo jsonschema 0.37.4 regex/RegexEngine#pattern().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
36 ));
37 }
38 }
39 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => {
40 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::backtrack_limitrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]backtrack_limit().(
41 self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(),
42 location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
43 instance,
44 e.into_backtrack_errorrust-analyzer cargo jsonschema 0.37.4 regex/RegexError#into_backtrack_error().()
45 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Can only fail with the fancy-regex crate"),
46 ));
47 }
48 }
49 }
50 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
51 }
52
53 fn is_validrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/impl#[`PatternValidator<R>`][Validate]is_valid().(&self, instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, _ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#) -> bool {
54 if let Valuerust-analyzer cargo serde_json 1.0.145 value/Value#::Stringrust-analyzer cargo serde_json 1.0.145 value/Value#String#(item) = instance {
55 return self.regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex..is_matchrust-analyzer cargo jsonschema 0.37.4 regex/RegexEngine#is_match().(item).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(false);
56 }
57 true
58 }
59 }
60
61 #[inline]
62 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/pattern/compile().<'a>(
63 ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#,
64 _: &'a Maprust-analyzer cargo serde_json 1.0.145 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#>,
65 schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
66 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a>> {
67 match schema {
68 Valuerust-analyzer cargo serde_json 1.0.145 value/Value#::Stringrust-analyzer cargo serde_json 1.0.145 value/Value#String#(item) => match ctx.configrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]config().().pattern_optionsrust-analyzer cargo jsonschema 0.37.4 options/impl#[ValidationOptions]pattern_options().() {
69 PatternEngineOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternEngineOptions#::FancyRegexrust-analyzer cargo jsonschema 0.37.4 options/PatternEngineOptions#FancyRegex# { .. } => {
70 let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(regex) = ctx.get_or_compile_regexrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]get_or_compile_regex().(item) else {
71 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(invalid_regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/invalid_regex().(ctx, schema)));
72 };
73 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(PatternValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator# {
74 regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex.,
75 locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#location.: ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().joinrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]join().("pattern"),
76 })))
77 }
78 PatternEngineOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternEngineOptions#::Regexrust-analyzer cargo jsonschema 0.37.4 options/PatternEngineOptions#Regex# { .. } => {
79 let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(regex) = ctx.get_or_compile_standard_regexrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]get_or_compile_standard_regex().(item) else {
80 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(invalid_regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/invalid_regex().(ctx, schema)));
81 };
82 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(PatternValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator# {
83 regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#regex.,
84 locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/PatternValidator#location.: ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().joinrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]join().("pattern"),
85 })))
86 }
87 },
88 _ => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::single_type_errorrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]single_type_error().(
89 Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#::newrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]new().(),
90 ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(),
91 schema,
92 JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#::Stringrust-analyzer cargo jsonschema 0.37.4 types/JsonType#String#,
93 ))),
94 }
95 }
96
97 fn invalid_regexrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/invalid_regex().<'a>(ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#, schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#) -> ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'a> {
98 ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::formatrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]format().(Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#::newrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]new().(), ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), schema, "regex")
99 }
100
101 #[cfg(test)]
102 mod testsrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/tests/ {
103 use craterust-analyzer cargo jsonschema 0.37.4 crate/::{tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/, PatternOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternOptions#};
104 use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::jsonrust-analyzer cargo serde_json 1.0.145 macros/json!;
105 use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:;
106
107 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("^(?!eo:)", "eo:bands", false)]
108 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("^(?!eo:)", "proj:epsg", true)]
109 fn negative_lookbehind_matchrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/tests/negative_lookbehind_match().(pattern: &str, text: &str, is_matching: bool) {
110 let text = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(text);
111 let schema = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"pattern": pattern});
112 let validator = craterust-analyzer cargo jsonschema 0.37.4 crate/::validator_forrust-analyzer cargo jsonschema 0.37.4 validator_for().(&schema).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
113 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(validator.is_validrust-analyzer cargo jsonschema 0.37.4 validator/impl#[Validator]is_valid().(&text), is_matching);
114 }
115
116 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
117 fn locationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/tests/location().() {
118 tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/::assert_schema_locationrust-analyzer cargo jsonschema 0.37.4 tests_util/assert_schema_location().(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"pattern": "^f"}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!("b"), "/pattern");
119 }
120
121 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
122 #[ignore = "fancy-regex 0.16 no longer fails for this test case"]
123 fn test_fancy_regex_backtrack_limit_exceededrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/tests/test_fancy_regex_backtrack_limit_exceeded().() {
124 let schema = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"pattern": "(?i)(a|b|ab)*(?=c)"});
125 let validator = craterust-analyzer cargo jsonschema 0.37.4 crate/::optionsrust-analyzer cargo jsonschema 0.37.4 options().()
126 .with_pattern_optionsrust-analyzer cargo jsonschema 0.37.4 options/impl#[ValidationOptions]with_pattern_options().(PatternOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternOptions#::fancy_regexrust-analyzer cargo jsonschema 0.37.4 options/impl#[`PatternOptions<FancyRegex>`]fancy_regex().().backtrack_limitrust-analyzer cargo jsonschema 0.37.4 options/impl#[`PatternOptions<FancyRegex>`]backtrack_limit().(1))
127 .buildrust-analyzer cargo jsonschema 0.37.4 options/impl#[ValidationOptions]build().(&schema)
128 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Schema should be valid");
129
130 let instance = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!("abababababababababababababababababababababababababababab");
131
132 let error = validator.validaterust-analyzer cargo jsonschema 0.37.4 validator/impl#[Validator]validate().(&instance).expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("Should fail");
133 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
134 error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
135 "Error executing regex: Max limit for backtracking count exceeded"
136 );
137 }
138
139 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
140 fn test_regex_engine_validationrust-analyzer cargo jsonschema 0.37.4 keywords/pattern/tests/test_regex_engine_validation().() {
141 let schema = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"pattern": "^[a-z]+$"});
142 let validator = craterust-analyzer cargo jsonschema 0.37.4 crate/::optionsrust-analyzer cargo jsonschema 0.37.4 options().()
143 .with_pattern_optionsrust-analyzer cargo jsonschema 0.37.4 options/impl#[ValidationOptions]with_pattern_options().(PatternOptionsrust-analyzer cargo jsonschema 0.37.4 options/PatternOptions#::regexrust-analyzer cargo jsonschema 0.37.4 options/impl#[`PatternOptions<Regex>`]regex().())
144 .buildrust-analyzer cargo jsonschema 0.37.4 options/impl#[ValidationOptions]build().(&schema)
145 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Schema should be valid");
146
147 let valid = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!("hello");
148 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(validator.is_validrust-analyzer cargo jsonschema 0.37.4 validator/impl#[Validator]is_valid().(&valid));
149 let invalid = jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!("Hello123");
150 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().validator.is_validrust-analyzer cargo jsonschema 0.37.4 validator/impl#[Validator]is_valid().(&invalid));
151 }
152 }