corvus_json_schema 0.1.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.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/Cargo.lock +390 -0
  3. data/Cargo.toml +8 -0
  4. data/LICENSE +201 -0
  5. data/README.md +111 -0
  6. data/VERSIONHISTORY.md +7 -0
  7. data/ext/corvus_json_schema/Cargo.toml +18 -0
  8. data/ext/corvus_json_schema/extconf.rb +6 -0
  9. data/ext/corvus_json_schema/rustfmt.toml +2 -0
  10. data/ext/corvus_json_schema/src/lib.rs +643 -0
  11. data/ext/corvus_json_schema/vendor/corvus-json-schema/Cargo.toml +35 -0
  12. data/ext/corvus_json_schema/vendor/corvus-json-schema/LICENSE +201 -0
  13. data/ext/corvus_json_schema/vendor/corvus-json-schema/README.md +139 -0
  14. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/compiler.rs +1118 -0
  15. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/dialect.rs +108 -0
  16. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/document.rs +905 -0
  17. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval/plan/fused.rs +1187 -0
  18. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval/plan.rs +1728 -0
  19. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval.rs +1451 -0
  20. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/formats.rs +1021 -0
  21. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/instance.rs +228 -0
  22. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/lib.rs +189 -0
  23. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/loader.rs +403 -0
  24. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/metaschemas.rs +29 -0
  25. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/node.rs +417 -0
  26. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/numbers.rs +244 -0
  27. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/options.rs +108 -0
  28. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/pattern.rs +1591 -0
  29. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/results.rs +316 -0
  30. data/ext/corvus_json_schema/vendor/corvus-json-schema/src/uri.rs +274 -0
  31. data/lib/corvus_json_schema/version.rb +5 -0
  32. data/lib/corvus_json_schema.rb +65 -0
  33. metadata +94 -0
@@ -0,0 +1,108 @@
1
+ //! Compile options and errors (the counterparts of `JsonSchemaEvaluatorOptions` and the evaluator exceptions).
2
+
3
+ use std::collections::HashMap;
4
+ use std::fmt;
5
+ use std::sync::Arc;
6
+
7
+ use serde_json::Value;
8
+
9
+ use crate::dialect::Dialect;
10
+
11
+ /// Resolves a schema document by absolute URI; returns `None` if it is unknown.
12
+ pub type DocumentResolver = Arc<dyn Fn(&str) -> Option<Value> + Send + Sync>;
13
+
14
+ /// A custom format assertion over the string value (or the number's JSON text, for numbers).
15
+ pub type FormatValidator = Arc<dyn Fn(&str) -> bool + Send + Sync>;
16
+
17
+ /// Options for compiling a schema.
18
+ #[derive(Clone)]
19
+ pub struct CompileOptions {
20
+ /// The dialect for documents without `$schema`. Defaults to 2020-12.
21
+ pub default_dialect: Dialect,
22
+ /// Whether `format` is asserted. `None` (the default) follows the vocabularies: the 2020-12 `format-assertion`
23
+ /// vocabulary asserts, everything else annotates.
24
+ pub assert_format: Option<bool>,
25
+ /// When `assert_format` is `None`, assert `format` in draft 4 to 7 too.
26
+ pub assert_format_in_legacy_drafts: bool,
27
+ /// Assert `contentEncoding`/`contentMediaType` in draft 7 (the only draft that asserts them). Defaults to true.
28
+ pub assert_content: bool,
29
+ /// Custom format assertions, by format name; they take precedence over the built-in set.
30
+ pub formats: HashMap<String, FormatValidator>,
31
+ /// Resolves remote documents. The standard metaschemas are always available.
32
+ pub resolve_document: Option<DocumentResolver>,
33
+ /// The base URI of the root document.
34
+ pub base_uri: Option<String>,
35
+ /// A reference (relative to the root) to evaluate from, e.g. `#/$defs/item`. Defaults to the root.
36
+ pub entry_point: Option<String>,
37
+ /// Maximum depth of in-place recursion on a cycle before evaluation is abandoned. Defaults to 128.
38
+ pub max_depth: u32,
39
+ }
40
+
41
+ impl Default for CompileOptions {
42
+ fn default() -> Self {
43
+ CompileOptions {
44
+ default_dialect: Dialect::Draft202012,
45
+ assert_format: None,
46
+ assert_format_in_legacy_drafts: false,
47
+ assert_content: true,
48
+ formats: HashMap::new(),
49
+ resolve_document: None,
50
+ base_uri: None,
51
+ entry_point: None,
52
+ max_depth: 128,
53
+ }
54
+ }
55
+ }
56
+
57
+ impl fmt::Debug for CompileOptions {
58
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59
+ f.debug_struct("CompileOptions")
60
+ .field("default_dialect", &self.default_dialect)
61
+ .field("assert_format", &self.assert_format)
62
+ .field("assert_format_in_legacy_drafts", &self.assert_format_in_legacy_drafts)
63
+ .field("assert_content", &self.assert_content)
64
+ .field("formats", &self.formats.keys().collect::<Vec<_>>())
65
+ .field("resolve_document", &self.resolve_document.is_some())
66
+ .field("base_uri", &self.base_uri)
67
+ .field("entry_point", &self.entry_point)
68
+ .field("max_depth", &self.max_depth)
69
+ .finish()
70
+ }
71
+ }
72
+
73
+ /// A schema could not be compiled (an unresolvable reference, an invalid pattern).
74
+ #[derive(Clone, Debug, PartialEq, Eq)]
75
+ pub struct SchemaCompilationError {
76
+ message: String,
77
+ }
78
+
79
+ impl SchemaCompilationError {
80
+ pub(crate) fn new(message: impl Into<String>) -> Self {
81
+ SchemaCompilationError { message: message.into() }
82
+ }
83
+
84
+ /// The reason compilation failed.
85
+ pub fn message(&self) -> &str {
86
+ &self.message
87
+ }
88
+ }
89
+
90
+ impl fmt::Display for SchemaCompilationError {
91
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92
+ f.write_str(&self.message)
93
+ }
94
+ }
95
+
96
+ impl std::error::Error for SchemaCompilationError {}
97
+
98
+ /// Evaluation recursed in place beyond `max_depth` (a schema that loops without consuming the instance).
99
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
100
+ pub struct SchemaEvaluationDepthError;
101
+
102
+ impl fmt::Display for SchemaEvaluationDepthError {
103
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104
+ f.write_str("The schema recursed in place beyond the maximum depth.")
105
+ }
106
+ }
107
+
108
+ impl std::error::Error for SchemaEvaluationDepthError {}