jsonschema_rs 0.61.2 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +90 -1
- data/Cargo.toml +2 -2
- data/MIGRATION.md +10 -12
- data/README.md +43 -31
- data/ext/jsonschema/Cargo.lock +20 -20
- data/ext/jsonschema/Cargo.toml +3 -3
- data/lib/jsonschema/version.rb +1 -1
- data/sig/jsonschema.rbs +90 -63
- data/src/canonical.rs +19 -19
- data/src/error_kind.rs +4 -2
- data/src/evaluation.rs +4 -8
- data/src/lib.rs +16 -76
- data/src/options.rs +59 -29
- data/src/registry.rs +24 -3
- data/src/ser.rs +2 -424
- data/src/static_id.rs +2 -5
- metadata +2 -2
data/src/lib.rs
CHANGED
|
@@ -128,48 +128,6 @@ fn build_validator(
|
|
|
128
128
|
})
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
/// RAII guard that registers Ruby callback values as GC roots for the duration
|
|
132
|
-
/// of a one-off validation call (module-level `valid?`, `validate!`, etc.).
|
|
133
|
-
///
|
|
134
|
-
/// Persistent `Validator` instances protect their callbacks via the GC mark phase
|
|
135
|
-
/// (see `Validator::mark_callback_roots`). One-off calls have no such wrapper, so
|
|
136
|
-
/// this guard calls `gc_register_address` on construction and `gc_unregister_address` on
|
|
137
|
-
/// drop to keep callbacks alive while validation runs.
|
|
138
|
-
struct CallbackRootGuard {
|
|
139
|
-
roots: Vec<Value>,
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
impl CallbackRootGuard {
|
|
143
|
-
fn new(ruby: &Ruby, callback_roots: &CallbackRoots) -> Self {
|
|
144
|
-
let roots = {
|
|
145
|
-
let roots_guard = match callback_roots.lock() {
|
|
146
|
-
Ok(roots) => roots,
|
|
147
|
-
Err(poisoned) => poisoned.into_inner(),
|
|
148
|
-
};
|
|
149
|
-
roots_guard
|
|
150
|
-
.iter()
|
|
151
|
-
.map(|root| ruby.get_inner(*root))
|
|
152
|
-
.collect::<Vec<_>>()
|
|
153
|
-
};
|
|
154
|
-
// We do not mutate `roots` after this point, so references used for GC address
|
|
155
|
-
// registration remain valid for the lifetime of the guard.
|
|
156
|
-
for root in &roots {
|
|
157
|
-
ruby.gc_register_address(root);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
Self { roots }
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
impl Drop for CallbackRootGuard {
|
|
165
|
-
fn drop(&mut self) {
|
|
166
|
-
let ruby = Ruby::get().expect("Ruby VM should be initialized");
|
|
167
|
-
for root in &self.roots {
|
|
168
|
-
ruby.gc_unregister_address(root);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
131
|
fn build_parsed_options(
|
|
174
132
|
ruby: &Ruby,
|
|
175
133
|
kw: ExtractedKwargs,
|
|
@@ -218,7 +176,7 @@ thread_local! {
|
|
|
218
176
|
static SUPPRESS_PANIC_OUTPUT: RefCell<bool> = const { RefCell::new(false) };
|
|
219
177
|
}
|
|
220
178
|
|
|
221
|
-
// RAII scope giving each
|
|
179
|
+
// RAII scope giving each validation call its own keyword-cause queue and restoring the caller's on
|
|
222
180
|
// exit. This discards causes a call leaves unconsumed (`evaluate`, an abandoned enumeration, a
|
|
223
181
|
// panic — the drop still runs) and lets a keyword re-enter validation without clobbering the outer
|
|
224
182
|
// call's pending causes.
|
|
@@ -388,8 +346,8 @@ fn error_message(error: &jsonschema::ValidationError<'_>, mask: Option<&str>) ->
|
|
|
388
346
|
}
|
|
389
347
|
}
|
|
390
348
|
|
|
391
|
-
// The
|
|
392
|
-
// convert
|
|
349
|
+
// The Ruby value at `location` under `root` when it holds only plain JSON types; `None` makes the
|
|
350
|
+
// caller convert the JSON copy.
|
|
393
351
|
fn plain_instance_at(ruby: &Ruby, root: RbNode<'_>, location: &str) -> Option<Value> {
|
|
394
352
|
#[allow(unsafe_code)]
|
|
395
353
|
let mut current: Value = unsafe { magnus::rb_sys::FromRawValue::from_raw(root.raw()) };
|
|
@@ -586,9 +544,7 @@ fn raise_validation_error(
|
|
|
586
544
|
}
|
|
587
545
|
}
|
|
588
546
|
|
|
589
|
-
/// RAII guard that sets `SUPPRESS_PANIC_OUTPUT`
|
|
590
|
-
/// resets it to `false` on drop, ensuring the flag is always restored even
|
|
591
|
-
/// if `catch_unwind` itself encounters a double-panic.
|
|
547
|
+
/// RAII guard that sets `SUPPRESS_PANIC_OUTPUT` for one `catch_unwind_silent` call.
|
|
592
548
|
struct SuppressPanicGuard;
|
|
593
549
|
|
|
594
550
|
impl SuppressPanicGuard {
|
|
@@ -631,9 +587,9 @@ fn handle_callback_panic(ruby: &Ruby, err: Box<dyn std::any::Any + Send>) -> Err
|
|
|
631
587
|
})
|
|
632
588
|
}
|
|
633
589
|
|
|
634
|
-
/// Run
|
|
590
|
+
/// Run `run` on `instance` read in place, then raise any read error the Magnus accessors recorded.
|
|
635
591
|
///
|
|
636
|
-
/// Accessors cannot fail, so
|
|
592
|
+
/// Accessors cannot fail, so they stash what they cannot read for this function to raise.
|
|
637
593
|
fn surface_pending_errors<T>(
|
|
638
594
|
instance: Value,
|
|
639
595
|
run: impl FnOnce(RbNode<'_>) -> Result<T, Error>,
|
|
@@ -805,10 +761,8 @@ impl Validator {
|
|
|
805
761
|
/// Detect the JSON Schema draft for a schema and return the corresponding validator class.
|
|
806
762
|
/// Draft is detected automatically from the `$schema` field. Defaults to Draft202012Validator.
|
|
807
763
|
///
|
|
808
|
-
///
|
|
809
|
-
///
|
|
810
|
-
/// >>> cls = JSONSchema.validator_cls_for({"$schema" => "http://json-schema.org/draft-07/schema#", "type" => "string"})
|
|
811
|
-
/// >>> validator = cls.new({"$schema" => "http://json-schema.org/draft-07/schema#", "type" => "string"})
|
|
764
|
+
/// JSONSchema.validator_cls_for({ "type" => "string" }) # => JSONSchema::Draft202012Validator
|
|
765
|
+
/// JSONSchema.validator_cls_for({ "$schema" => "http://json-schema.org/draft-07/schema#" }) # => JSONSchema::Draft7Validator
|
|
812
766
|
///
|
|
813
767
|
fn validator_cls_for(ruby: &Ruby, schema: Value) -> Result<RClass, Error> {
|
|
814
768
|
let draft = if let Some(hash) = RHash::from_value(schema) {
|
|
@@ -981,11 +935,10 @@ fn is_valid(ruby: &Ruby, args: &[Value]) -> Result<bool, Error> {
|
|
|
981
935
|
|
|
982
936
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
983
937
|
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
984
|
-
let has_ruby_callbacks = parsed.has_ruby_callbacks;
|
|
985
938
|
let BuiltValidator {
|
|
986
939
|
validator,
|
|
987
|
-
callback_roots,
|
|
988
940
|
compilation_roots: _compilation_roots,
|
|
941
|
+
..
|
|
989
942
|
} = build_validator(
|
|
990
943
|
ruby,
|
|
991
944
|
parsed.options,
|
|
@@ -997,8 +950,6 @@ fn is_valid(ruby: &Ruby, args: &[Value]) -> Result<bool, Error> {
|
|
|
997
950
|
)?;
|
|
998
951
|
|
|
999
952
|
surface_pending_errors(instance, |node| {
|
|
1000
|
-
let _callback_roots =
|
|
1001
|
-
has_ruby_callbacks.then(|| CallbackRootGuard::new(ruby, &callback_roots));
|
|
1002
953
|
catch_unwind_silent(AssertUnwindSafe(|| validator.is_valid(node)))
|
|
1003
954
|
.map_err(|err| handle_callback_panic(ruby, err))
|
|
1004
955
|
})
|
|
@@ -1012,11 +963,10 @@ fn validate(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
1012
963
|
|
|
1013
964
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1014
965
|
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1015
|
-
let has_ruby_callbacks = parsed.has_ruby_callbacks;
|
|
1016
966
|
let BuiltValidator {
|
|
1017
967
|
validator,
|
|
1018
|
-
callback_roots,
|
|
1019
968
|
compilation_roots: _compilation_roots,
|
|
969
|
+
..
|
|
1020
970
|
} = build_validator(
|
|
1021
971
|
ruby,
|
|
1022
972
|
parsed.options,
|
|
@@ -1028,8 +978,6 @@ fn validate(ruby: &Ruby, args: &[Value]) -> Result<(), Error> {
|
|
|
1028
978
|
)?;
|
|
1029
979
|
|
|
1030
980
|
surface_pending_errors(instance, |node| {
|
|
1031
|
-
let _callback_roots =
|
|
1032
|
-
has_ruby_callbacks.then(|| CallbackRootGuard::new(ruby, &callback_roots));
|
|
1033
981
|
match catch_unwind_silent(AssertUnwindSafe(|| validator.validate(node))) {
|
|
1034
982
|
Ok(Ok(())) => Ok(()),
|
|
1035
983
|
Ok(Err(error)) => Err(raise_validation_error(
|
|
@@ -1051,11 +999,10 @@ fn each_error(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
1051
999
|
|
|
1052
1000
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1053
1001
|
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1054
|
-
let has_ruby_callbacks = parsed.has_ruby_callbacks;
|
|
1055
1002
|
let BuiltValidator {
|
|
1056
1003
|
validator,
|
|
1057
|
-
callback_roots,
|
|
1058
1004
|
compilation_roots: _compilation_roots,
|
|
1005
|
+
..
|
|
1059
1006
|
} = build_validator(
|
|
1060
1007
|
ruby,
|
|
1061
1008
|
parsed.options,
|
|
@@ -1067,9 +1014,6 @@ fn each_error(ruby: &Ruby, args: &[Value]) -> Result<Value, Error> {
|
|
|
1067
1014
|
)?;
|
|
1068
1015
|
|
|
1069
1016
|
surface_pending_errors(instance, |node| {
|
|
1070
|
-
let _callback_roots =
|
|
1071
|
-
has_ruby_callbacks.then(|| CallbackRootGuard::new(ruby, &callback_roots));
|
|
1072
|
-
|
|
1073
1017
|
if ruby.block_given() {
|
|
1074
1018
|
// Lazy path: yield errors one at a time to the block
|
|
1075
1019
|
let mut iter = validator.iter_errors(node);
|
|
@@ -1115,11 +1059,10 @@ fn evaluate(ruby: &Ruby, args: &[Value]) -> Result<Evaluation, Error> {
|
|
|
1115
1059
|
|
|
1116
1060
|
let json_schema = to_schema_value(ruby, schema)?;
|
|
1117
1061
|
let parsed = build_parsed_options(ruby, kw, None)?;
|
|
1118
|
-
let has_ruby_callbacks = parsed.has_ruby_callbacks;
|
|
1119
1062
|
let BuiltValidator {
|
|
1120
1063
|
validator,
|
|
1121
|
-
callback_roots,
|
|
1122
1064
|
compilation_roots: _compilation_roots,
|
|
1065
|
+
..
|
|
1123
1066
|
} = build_validator(
|
|
1124
1067
|
ruby,
|
|
1125
1068
|
parsed.options,
|
|
@@ -1131,8 +1074,6 @@ fn evaluate(ruby: &Ruby, args: &[Value]) -> Result<Evaluation, Error> {
|
|
|
1131
1074
|
)?;
|
|
1132
1075
|
|
|
1133
1076
|
surface_pending_errors(instance, |node| {
|
|
1134
|
-
let _callback_roots =
|
|
1135
|
-
has_ruby_callbacks.then(|| CallbackRootGuard::new(ruby, &callback_roots));
|
|
1136
1077
|
match catch_unwind_silent(AssertUnwindSafe(|| validator.evaluate(node))) {
|
|
1137
1078
|
Ok(output) => Ok(Evaluation::new(output)),
|
|
1138
1079
|
Err(err) => Err(handle_callback_panic(ruby, err)),
|
|
@@ -1242,8 +1183,8 @@ fn json_text_schema(schema: Value) -> Option<serde_json::Value> {
|
|
|
1242
1183
|
serde_json::from_slice(bytes).ok()
|
|
1243
1184
|
}
|
|
1244
1185
|
|
|
1245
|
-
//
|
|
1246
|
-
//
|
|
1186
|
+
// Checks against a compiled-in meta-schema, reading the Ruby object in place; `None` when `$schema`
|
|
1187
|
+
// names none of them.
|
|
1247
1188
|
fn compiled_meta_is_valid(schema: Value) -> Option<Result<bool, Error>> {
|
|
1248
1189
|
#[allow(unsafe_code)]
|
|
1249
1190
|
let node = RbNode::new(schema.as_raw());
|
|
@@ -1398,9 +1339,8 @@ fn validation_error_hash(ruby: &Ruby, rb_self: Value) -> Result<Value, Error> {
|
|
|
1398
1339
|
|
|
1399
1340
|
#[magnus::init(name = "jsonschema_rb")]
|
|
1400
1341
|
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
1401
|
-
//
|
|
1402
|
-
//
|
|
1403
|
-
// Other panics pass through to the default handler to preserve debugging output.
|
|
1342
|
+
// Silence panic output inside `catch_unwind_silent`, which wraps every validation call; other
|
|
1343
|
+
// panics keep the default output.
|
|
1404
1344
|
let default_hook = std::panic::take_hook();
|
|
1405
1345
|
std::panic::set_hook(Box::new(move |info| {
|
|
1406
1346
|
let suppress = SUPPRESS_PANIC_OUTPUT.with(|flag| *flag.borrow());
|
data/src/options.rs
CHANGED
|
@@ -10,7 +10,7 @@ use magnus::{
|
|
|
10
10
|
prelude::*,
|
|
11
11
|
scan_args::{get_kwargs, scan_args, KwArgs},
|
|
12
12
|
value::Opaque,
|
|
13
|
-
Error, Exception, RArray, RHash, RModule, Ruby, TryConvert, Value,
|
|
13
|
+
Error, Exception, RArray, RHash, RModule, Ruby, Symbol, TryConvert, Value,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
use jsonschema::json::{magnus_invalidate_members_cache, Magnus, RbNode};
|
|
@@ -48,6 +48,7 @@ define_rb_intern!(static KW_MINIMUM_SUB_DOMAINS: "minimum_sub_domains");
|
|
|
48
48
|
define_rb_intern!(static KW_SIZE_LIMIT: "size_limit");
|
|
49
49
|
define_rb_intern!(static KW_DFA_SIZE_LIMIT: "dfa_size_limit");
|
|
50
50
|
define_rb_intern!(static KW_BACKTRACK_LIMIT: "backtrack_limit");
|
|
51
|
+
define_rb_intern!(static KW_SYNTAX: "syntax");
|
|
51
52
|
// HttpOptions kwargs
|
|
52
53
|
define_rb_intern!(static KW_TIMEOUT: "timeout");
|
|
53
54
|
define_rb_intern!(static KW_CONNECT_TIMEOUT: "connect_timeout");
|
|
@@ -63,9 +64,6 @@ pub struct ParsedOptions<'i> {
|
|
|
63
64
|
pub options: jsonschema::ValidationOptions<'i, Arc<dyn jsonschema::Retrieve>, Magnus>,
|
|
64
65
|
pub registry: Option<&'i jsonschema::Registry<'static>>,
|
|
65
66
|
pub retriever: Option<RubyRetriever>,
|
|
66
|
-
// Runtime callbacks invoked during `validator.*` calls (formats / custom keywords).
|
|
67
|
-
// Retriever callbacks are used at build time and do not affect GVL behavior at runtime.
|
|
68
|
-
pub has_ruby_callbacks: bool,
|
|
69
67
|
pub callback_roots: CallbackRoots,
|
|
70
68
|
pub compilation_roots: CompilationRootsRef,
|
|
71
69
|
}
|
|
@@ -79,16 +77,13 @@ pub struct ParsedOptions<'i> {
|
|
|
79
77
|
/// Two complementary collections keep every callback alive:
|
|
80
78
|
///
|
|
81
79
|
/// * **`CallbackRoots`** — held by the `Validator` wrapper and marked during Ruby's
|
|
82
|
-
/// GC mark phase (`DataTypeFunctions::mark`).
|
|
83
|
-
/// mechanism for preventing collection of objects referenced by native extensions.
|
|
84
|
-
/// For one-off validation functions (module-level `valid?`, `validate!`, etc.),
|
|
85
|
-
/// where no persistent `Validator` exists, a [`CallbackRootGuard`](crate::CallbackRootGuard)
|
|
86
|
-
/// temporarily registers the same roots via `gc_register_address` for the duration
|
|
87
|
-
/// of the call.
|
|
80
|
+
/// GC mark phase (`DataTypeFunctions::mark`).
|
|
88
81
|
///
|
|
89
82
|
/// * **`CompilationRoots`** — registered via `gc_register_address` immediately when
|
|
90
83
|
/// added, so callbacks are protected during schema compilation (before the
|
|
91
|
-
/// `Validator` wrapper and its mark function exist)
|
|
84
|
+
/// `Validator` wrapper and its mark function exist) and for the whole of a one-off
|
|
85
|
+
/// call (module-level `valid?`, `validate!`, etc.), which has no wrapper.
|
|
86
|
+
/// Unregistered on drop.
|
|
92
87
|
///
|
|
93
88
|
/// Both collections hold the **same** Ruby objects; they differ only in *when* and
|
|
94
89
|
/// *how* they protect them from the GC.
|
|
@@ -526,7 +521,6 @@ pub fn make_options_from_kwargs(
|
|
|
526
521
|
let mut registry = None;
|
|
527
522
|
let mut retriever = None;
|
|
528
523
|
let retriever_was_provided = retriever_val.is_some();
|
|
529
|
-
let mut has_ruby_callbacks = false;
|
|
530
524
|
let callback_roots = Arc::new(Mutex::new(Vec::new()));
|
|
531
525
|
let compilation_roots = Arc::new(CompilationRoots::default());
|
|
532
526
|
|
|
@@ -612,7 +606,6 @@ pub fn make_options_from_kwargs(
|
|
|
612
606
|
|
|
613
607
|
if let Some(formats_hash) = formats {
|
|
614
608
|
for item in formats_hash.enumeratorize("each", ()) {
|
|
615
|
-
has_ruby_callbacks = true;
|
|
616
609
|
let pair: magnus::RArray = magnus::TryConvert::try_convert(item?)?;
|
|
617
610
|
let name: String = pair.entry(0)?;
|
|
618
611
|
let callback: Value = pair.entry(1)?;
|
|
@@ -663,7 +656,6 @@ pub fn make_options_from_kwargs(
|
|
|
663
656
|
|
|
664
657
|
if let Some(keywords_hash) = keywords {
|
|
665
658
|
for item in keywords_hash.enumeratorize("each", ()) {
|
|
666
|
-
has_ruby_callbacks = true;
|
|
667
659
|
let pair: magnus::RArray = magnus::TryConvert::try_convert(item?)?;
|
|
668
660
|
let name: String = pair.entry(0)?;
|
|
669
661
|
let callback: Value = pair.entry(1)?;
|
|
@@ -729,14 +721,12 @@ pub fn make_options_from_kwargs(
|
|
|
729
721
|
))
|
|
730
722
|
})?;
|
|
731
723
|
|
|
732
|
-
// Convert keyword value to Ruby
|
|
733
724
|
let rb_value = value_to_ruby(&inner_ruby, value).map_err(|e| {
|
|
734
725
|
jsonschema::ValidationError::custom(format!(
|
|
735
726
|
"Failed to convert keyword value to Ruby: {e}"
|
|
736
727
|
))
|
|
737
728
|
})?;
|
|
738
729
|
|
|
739
|
-
// Convert path to Ruby array
|
|
740
730
|
let rb_path =
|
|
741
731
|
inner_ruby.ary_from_iter(path.iter().map(|segment| match segment {
|
|
742
732
|
jsonschema::paths::LocationSegment::Property(p) => {
|
|
@@ -854,7 +844,6 @@ pub fn make_options_from_kwargs(
|
|
|
854
844
|
options: opts,
|
|
855
845
|
registry,
|
|
856
846
|
retriever,
|
|
857
|
-
has_ruby_callbacks,
|
|
858
847
|
callback_roots,
|
|
859
848
|
compilation_roots,
|
|
860
849
|
})
|
|
@@ -936,21 +925,49 @@ impl EmailOptions {
|
|
|
936
925
|
pub struct RegexOptions {
|
|
937
926
|
pub size_limit: Option<usize>,
|
|
938
927
|
pub dfa_size_limit: Option<usize>,
|
|
928
|
+
pub syntax: jsonschema::PatternSyntax,
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
fn parse_syntax(ruby: &Ruby, syntax: Option<Symbol>) -> Result<jsonschema::PatternSyntax, Error> {
|
|
932
|
+
let Some(syntax) = syntax else {
|
|
933
|
+
return Ok(jsonschema::PatternSyntax::Ecma262);
|
|
934
|
+
};
|
|
935
|
+
match &*syntax.name()? {
|
|
936
|
+
"ecma262" => Ok(jsonschema::PatternSyntax::Ecma262),
|
|
937
|
+
"engine" => Ok(jsonschema::PatternSyntax::Engine),
|
|
938
|
+
name => Err(Error::new(
|
|
939
|
+
ruby.exception_arg_error(),
|
|
940
|
+
format!("Unknown pattern syntax: :{name}. Expected :ecma262 or :engine"),
|
|
941
|
+
)),
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
fn syntax_name(syntax: jsonschema::PatternSyntax) -> &'static str {
|
|
946
|
+
match syntax {
|
|
947
|
+
jsonschema::PatternSyntax::Engine => "engine",
|
|
948
|
+
_ => "ecma262",
|
|
949
|
+
}
|
|
939
950
|
}
|
|
940
951
|
|
|
941
952
|
impl RegexOptions {
|
|
942
|
-
|
|
953
|
+
#[allow(clippy::type_complexity)]
|
|
954
|
+
fn new_impl(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
|
|
943
955
|
let parsed = scan_args::<(), (), (), (), _, ()>(args)?;
|
|
944
|
-
let ids = [*KW_SIZE_LIMIT, *KW_DFA_SIZE_LIMIT];
|
|
945
|
-
let kw: KwArgs<(), (Option<usize>, Option<usize>), ()> =
|
|
956
|
+
let ids = [*KW_SIZE_LIMIT, *KW_DFA_SIZE_LIMIT, *KW_SYNTAX];
|
|
957
|
+
let kw: KwArgs<(), (Option<usize>, Option<usize>, Option<Symbol>), ()> =
|
|
946
958
|
get_kwargs(parsed.keywords, &[], &ids)?;
|
|
947
|
-
let (size_limit, dfa_size_limit) = kw.optional;
|
|
959
|
+
let (size_limit, dfa_size_limit, syntax) = kw.optional;
|
|
948
960
|
Ok(RegexOptions {
|
|
949
961
|
size_limit,
|
|
950
962
|
dfa_size_limit,
|
|
963
|
+
syntax: parse_syntax(ruby, syntax)?,
|
|
951
964
|
})
|
|
952
965
|
}
|
|
953
966
|
|
|
967
|
+
fn syntax(ruby: &Ruby, rb_self: &Self) -> Symbol {
|
|
968
|
+
ruby.to_symbol(syntax_name(rb_self.syntax))
|
|
969
|
+
}
|
|
970
|
+
|
|
954
971
|
fn size_limit(&self) -> Option<usize> {
|
|
955
972
|
self.size_limit
|
|
956
973
|
}
|
|
@@ -971,7 +988,7 @@ impl RegexOptions {
|
|
|
971
988
|
Some(n) => write!(s, "{n}").expect("Failed to write dfa_size_limit"),
|
|
972
989
|
None => s.push_str("nil"),
|
|
973
990
|
}
|
|
974
|
-
s.
|
|
991
|
+
write!(s, ", syntax=:{}>", syntax_name(self.syntax)).expect("Failed to write syntax");
|
|
975
992
|
s
|
|
976
993
|
}
|
|
977
994
|
}
|
|
@@ -984,7 +1001,7 @@ pub(crate) enum RbPatternOptions {
|
|
|
984
1001
|
|
|
985
1002
|
pub(crate) fn extract_pattern_options(ruby: &Ruby, val: Value) -> Result<RbPatternOptions, Error> {
|
|
986
1003
|
if let Ok(fancy_opts) = <&FancyRegexOptions>::try_convert(val) {
|
|
987
|
-
let mut po = jsonschema::PatternOptions::fancy_regex();
|
|
1004
|
+
let mut po = jsonschema::PatternOptions::fancy_regex().syntax(fancy_opts.syntax);
|
|
988
1005
|
if let Some(limit) = fancy_opts.backtrack_limit {
|
|
989
1006
|
po = po.backtrack_limit(limit);
|
|
990
1007
|
}
|
|
@@ -996,7 +1013,7 @@ pub(crate) fn extract_pattern_options(ruby: &Ruby, val: Value) -> Result<RbPatte
|
|
|
996
1013
|
}
|
|
997
1014
|
Ok(RbPatternOptions::Fancy(po))
|
|
998
1015
|
} else if let Ok(regex_opts) = <&RegexOptions>::try_convert(val) {
|
|
999
|
-
let mut po = jsonschema::PatternOptions::regex();
|
|
1016
|
+
let mut po = jsonschema::PatternOptions::regex().syntax(regex_opts.syntax);
|
|
1000
1017
|
if let Some(limit) = regex_opts.size_limit {
|
|
1001
1018
|
po = po.size_limit(limit);
|
|
1002
1019
|
}
|
|
@@ -1017,23 +1034,34 @@ pub struct FancyRegexOptions {
|
|
|
1017
1034
|
pub backtrack_limit: Option<usize>,
|
|
1018
1035
|
pub size_limit: Option<usize>,
|
|
1019
1036
|
pub dfa_size_limit: Option<usize>,
|
|
1037
|
+
pub syntax: jsonschema::PatternSyntax,
|
|
1020
1038
|
}
|
|
1021
1039
|
|
|
1022
1040
|
impl FancyRegexOptions {
|
|
1023
1041
|
#[allow(clippy::type_complexity)]
|
|
1024
|
-
fn new_impl(args: &[Value]) -> Result<Self, Error> {
|
|
1042
|
+
fn new_impl(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
|
|
1025
1043
|
let parsed = scan_args::<(), (), (), (), _, ()>(args)?;
|
|
1026
|
-
let ids = [
|
|
1027
|
-
|
|
1044
|
+
let ids = [
|
|
1045
|
+
*KW_BACKTRACK_LIMIT,
|
|
1046
|
+
*KW_SIZE_LIMIT,
|
|
1047
|
+
*KW_DFA_SIZE_LIMIT,
|
|
1048
|
+
*KW_SYNTAX,
|
|
1049
|
+
];
|
|
1050
|
+
let kw: KwArgs<(), (Option<usize>, Option<usize>, Option<usize>, Option<Symbol>), ()> =
|
|
1028
1051
|
get_kwargs(parsed.keywords, &[], &ids)?;
|
|
1029
|
-
let (backtrack_limit, size_limit, dfa_size_limit) = kw.optional;
|
|
1052
|
+
let (backtrack_limit, size_limit, dfa_size_limit, syntax) = kw.optional;
|
|
1030
1053
|
Ok(FancyRegexOptions {
|
|
1031
1054
|
backtrack_limit,
|
|
1032
1055
|
size_limit,
|
|
1033
1056
|
dfa_size_limit,
|
|
1057
|
+
syntax: parse_syntax(ruby, syntax)?,
|
|
1034
1058
|
})
|
|
1035
1059
|
}
|
|
1036
1060
|
|
|
1061
|
+
fn syntax(ruby: &Ruby, rb_self: &Self) -> Symbol {
|
|
1062
|
+
ruby.to_symbol(syntax_name(rb_self.syntax))
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1037
1065
|
fn backtrack_limit(&self) -> Option<usize> {
|
|
1038
1066
|
self.backtrack_limit
|
|
1039
1067
|
}
|
|
@@ -1063,7 +1091,7 @@ impl FancyRegexOptions {
|
|
|
1063
1091
|
Some(n) => write!(s, "{n}").expect("Failed to write dfa_size_limit"),
|
|
1064
1092
|
None => s.push_str("nil"),
|
|
1065
1093
|
}
|
|
1066
|
-
s.
|
|
1094
|
+
write!(s, ", syntax=:{}>", syntax_name(self.syntax)).expect("Failed to write syntax");
|
|
1067
1095
|
s
|
|
1068
1096
|
}
|
|
1069
1097
|
}
|
|
@@ -1159,6 +1187,7 @@ pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
1159
1187
|
regex_class.define_singleton_method("new", function!(RegexOptions::new_impl, -1))?;
|
|
1160
1188
|
regex_class.define_method("size_limit", method!(RegexOptions::size_limit, 0))?;
|
|
1161
1189
|
regex_class.define_method("dfa_size_limit", method!(RegexOptions::dfa_size_limit, 0))?;
|
|
1190
|
+
regex_class.define_method("syntax", method!(RegexOptions::syntax, 0))?;
|
|
1162
1191
|
regex_class.define_method("inspect", method!(RegexOptions::inspect, 0))?;
|
|
1163
1192
|
|
|
1164
1193
|
let fancy_regex_class = module.define_class("FancyRegexOptions", ruby.class_object())?;
|
|
@@ -1172,6 +1201,7 @@ pub fn define_classes(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
1172
1201
|
"dfa_size_limit",
|
|
1173
1202
|
method!(FancyRegexOptions::dfa_size_limit, 0),
|
|
1174
1203
|
)?;
|
|
1204
|
+
fancy_regex_class.define_method("syntax", method!(FancyRegexOptions::syntax, 0))?;
|
|
1175
1205
|
fancy_regex_class.define_method("inspect", method!(FancyRegexOptions::inspect, 0))?;
|
|
1176
1206
|
|
|
1177
1207
|
let http_class = module.define_class("HttpOptions", ruby.class_object())?;
|
data/src/registry.rs
CHANGED
|
@@ -207,6 +207,20 @@ impl Resolver {
|
|
|
207
207
|
|
|
208
208
|
#[allow(clippy::needless_pass_by_value)]
|
|
209
209
|
fn lookup(ruby: &Ruby, rb_self: &Resolver, reference: String) -> Result<Resolved, Error> {
|
|
210
|
+
rb_self.resolve(ruby, &reference, false)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#[allow(clippy::needless_pass_by_value)]
|
|
214
|
+
fn lookup_dynamic_ref(
|
|
215
|
+
ruby: &Ruby,
|
|
216
|
+
rb_self: &Resolver,
|
|
217
|
+
reference: String,
|
|
218
|
+
) -> Result<Resolved, Error> {
|
|
219
|
+
rb_self.resolve(ruby, &reference, true)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
fn resolve(&self, ruby: &Ruby, reference: &str, dynamic: bool) -> Result<Resolved, Error> {
|
|
223
|
+
let rb_self = self;
|
|
210
224
|
let resolver = if rb_self.0.dynamic_scope.is_empty() {
|
|
211
225
|
rb_self
|
|
212
226
|
.0
|
|
@@ -237,9 +251,12 @@ impl Resolver {
|
|
|
237
251
|
resolver
|
|
238
252
|
};
|
|
239
253
|
|
|
240
|
-
let resolved =
|
|
241
|
-
.
|
|
242
|
-
|
|
254
|
+
let resolved = if dynamic {
|
|
255
|
+
resolver.lookup_dynamic_ref(reference)
|
|
256
|
+
} else {
|
|
257
|
+
resolver.lookup(reference)
|
|
258
|
+
};
|
|
259
|
+
let resolved = resolved.map_err(|e| crate::referencing_error(ruby, e.to_string()))?;
|
|
243
260
|
let (contents, resolver, draft) = resolved.into_inner();
|
|
244
261
|
|
|
245
262
|
let contents_val = value_to_ruby(ruby, contents)?;
|
|
@@ -286,6 +303,10 @@ pub fn define_class(ruby: &Ruby, module: &RModule) -> Result<(), Error> {
|
|
|
286
303
|
resolver_class.define_method("base_uri", method!(Resolver::base_uri, 0))?;
|
|
287
304
|
resolver_class.define_method("dynamic_scope", method!(Resolver::dynamic_scope, 0))?;
|
|
288
305
|
resolver_class.define_method("lookup", method!(Resolver::lookup, 1))?;
|
|
306
|
+
resolver_class.define_method(
|
|
307
|
+
"lookup_dynamic_ref",
|
|
308
|
+
method!(Resolver::lookup_dynamic_ref, 1),
|
|
309
|
+
)?;
|
|
289
310
|
|
|
290
311
|
let resolved_class = module.define_class("Resolved", ruby.class_object())?;
|
|
291
312
|
resolved_class.define_method("contents", method!(Resolved::contents, 0))?;
|