Skip to main content

json_ld/algorithms/expansion/
options.rs

1use crate::{JsonLdProcessingMode, algorithms::context_processing::JsonLdContextProcessingOptions};
2
3/// Expansion options.
4#[derive(Clone, Copy, Default)]
5pub struct JsonLdExpansionOptions {
6	/// Sets the processing mode.
7	pub processing_mode: JsonLdProcessingMode,
8
9	/// Term expansion policy.
10	///
11	/// Default is `JsonLdExpansionPolicy::Standard`.
12	pub policy: JsonLdExpansionPolicy,
13
14	/// If set to true, input document entries are processed lexicographically.
15	/// If false, order is not considered in processing.
16	pub ordered: bool,
17}
18
19impl JsonLdExpansionOptions {
20	pub fn unordered(self) -> Self {
21		Self {
22			ordered: false,
23			..self
24		}
25	}
26}
27
28impl From<JsonLdExpansionOptions> for JsonLdContextProcessingOptions {
29	fn from(options: JsonLdExpansionOptions) -> JsonLdContextProcessingOptions {
30		JsonLdContextProcessingOptions {
31			processing_mode: options.processing_mode,
32			..Default::default()
33		}
34	}
35}
36
37/// Key expansion policy.
38///
39/// The default behavior of the expansion algorithm
40/// is to drop keys that are not defined in the context unless:
41///  - there is a vocabulary mapping (`@vocab`) defined in the context; or
42///  - the term contains a `:` character.
43///
44/// In other words, a key that cannot be expanded into an
45/// IRI or a blank node identifier is dropped unless it contains a `:` character.
46///
47/// Sometimes, it is preferable to keep undefined keys in the
48/// expanded document, or to forbid them completely by raising an error.
49/// You can define your preferred policy using one of this type variant
50/// with the [`JsonLdExpansionOptions::policy`] field.
51#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
52pub enum JsonLdExpansionPolicy {
53	/// Relaxed policy.
54	///
55	/// Undefined keys are always kept in the expanded document
56	/// using the [`JsonLdLenient::Invalid`](crate::JsonLdLenient::Invalid) variant.
57	Relaxed,
58
59	/// Standard policy.
60	///
61	/// Every key that cannot be expanded into an
62	/// IRI or a blank node identifier is dropped unless it contains a `:` character.
63	#[default]
64	Standard,
65
66	/// Strict policy.
67	///
68	/// Every key that cannot be expanded into an IRI or a blank node identifier
69	/// will raise an error unless the term contains a `:` character.
70	Strict,
71
72	/// Strictest policy.
73	///
74	/// Every key that cannot be expanded into an IRI or a blank node identifier
75	/// will raise an error.
76	Strictest,
77}
78
79impl JsonLdExpansionPolicy {
80	/// Returns `true` is the policy is `Strict` or `Strictest`.
81	pub fn is_strict(&self) -> bool {
82		matches!(self, Self::Strict | Self::Strictest)
83	}
84}