jsonld_expansion/options.rs
1use jsonld_core::ProcessingMode;
2
3pub use jsonld_context_processing::algorithm::Action;
4
5/// Expansion options.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub struct Options {
8 /// JSON-LD version the algorithm conforms to.
9 ///
10 /// Defaults to `json-ld-1.1`. Under `json-ld-1.0` some 1.1 features are
11 /// ignored, such as `@included` and `@direction` entries, and others are
12 /// rejected, such as a list object among the items of another list object.
13 /// The mode also applies to the processing of the contexts met on the way.
14 pub processing_mode: ProcessingMode,
15
16 /// What to do with the keys that cannot be expanded into a proper IRI.
17 ///
18 /// Defaults to [`Policy::default()`], the behaviour the specification
19 /// prescribes.
20 pub policy: Policy,
21
22 /// Whether the entries of every JSON object are processed in
23 /// lexicographic order of their keys.
24 ///
25 /// Ordering costs a sort per object and only matters when the expanded
26 /// output has to be reproducible entry by entry, as in the JSON-LD test
27 /// suite. `false` by default, which leaves entries in document order.
28 pub ordered: bool,
29}
30
31impl Options {
32 /// Returns a copy of these options with entry ordering switched off.
33 #[must_use]
34 pub fn unordered(self) -> Self {
35 Self { ordered: false, ..self }
36 }
37}
38
39impl From<Options> for jsonld_context_processing::Options {
40 fn from(options: Options) -> jsonld_context_processing::Options {
41 // Only the processing mode carries over. The other context-processing
42 // options keep their default value here; the expansion steps that need
43 // another value set it on the result, as with `with_override` for a
44 // property-scoped context.
45 jsonld_context_processing::Options {
46 processing_mode: options.processing_mode,
47 ..Default::default()
48 }
49 }
50}
51
52/// Key expansion policy.
53///
54/// By default the expansion algorithm drops the keys that are not defined in
55/// the active context, unless:
56/// - the active context defines a vocabulary mapping (`@vocab`); or
57/// - the key contains a `:` character, in which case it is kept as it is,
58/// even though it may not be a well-formed IRI.
59///
60/// Silently dropping data is not always what an application wants: a stricter
61/// setting turns each of those situations into an error instead. Set the
62/// wanted policy through the [`Options::policy`] field.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub struct Policy {
65 /// What to do with a key that expands to a malformed IRI.
66 ///
67 /// [`Action::Keep`] (the default) keeps it in the expanded document,
68 /// [`Action::Drop`] drops the entry, and [`Action::Reject`] aborts
69 /// expansion with [`Error::KeyExpansionFailed`] — or, for a value of
70 /// `@type`, [`Error::InvalidTypeValue`].
71 ///
72 /// [`Error::KeyExpansionFailed`]: crate::Error::KeyExpansionFailed
73 /// [`Error::InvalidTypeValue`]: crate::Error::InvalidTypeValue
74 pub invalid: Action,
75
76 /// What to do with a key that only expands because the active context
77 /// defines a vocabulary mapping (`@vocab`).
78 ///
79 /// [`Action::Keep`] (the default) expands it against the vocabulary
80 /// mapping, [`Action::Drop`] drops the entry, and [`Action::Reject`]
81 /// aborts expansion with [`Error::ForbiddenVocab`].
82 ///
83 /// [`Error::ForbiddenVocab`]: crate::Error::ForbiddenVocab
84 pub vocab: Action,
85
86 /// Whether a key that neither the active context nor a vocabulary mapping
87 /// can expand may be dropped.
88 ///
89 /// `true` by default. Set it to `false` to abort expansion with
90 /// [`Error::KeyExpansionFailed`] instead of losing the entry.
91 ///
92 /// [`Error::KeyExpansionFailed`]: crate::Error::KeyExpansionFailed
93 pub allow_undefined: bool,
94}
95
96impl Default for Policy {
97 fn default() -> Self {
98 Self {
99 invalid: Action::Keep,
100 vocab: Action::Keep,
101 allow_undefined: true,
102 }
103 }
104}