Skip to content

Commit

Permalink
Add an expansion policy option for @vocab. (#78)
Browse files Browse the repository at this point in the history
Apply the expansion policy to node types.
  • Loading branch information
timothee-haudebourg authored Jul 8, 2024
1 parent 4f9e266 commit 51b19b9
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 210 deletions.
18 changes: 17 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub enum Command {
/// Put the expanded document in canonical form.
#[clap(short, long)]
canonicalize: bool,

#[clap(long = "no-vocab")]
no_vocab: bool,

#[clap(long = "no-undef")]
no_undef: bool,
},

Flatten {
Expand Down Expand Up @@ -112,11 +118,21 @@ async fn main() {
base_url,
relabel,
canonicalize,
no_vocab,
no_undef,
} => {
let remote_document = get_remote_document(&mut vocabulary, url_or_path, base_url);

let options = json_ld::Options {
expansion_policy: json_ld::expansion::Policy::Strictest,
expansion_policy: json_ld::expansion::Policy {
invalid: json_ld::expansion::Action::Reject,
vocab: if no_vocab {
json_ld::expansion::Action::Reject
} else {
json_ld::expansion::Action::Keep
},
allow_undefined: !no_undef,
},
..Default::default()
};

Expand Down
64 changes: 33 additions & 31 deletions crates/context-processing/src/algorithm/define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,31 @@ where
active_context,
type_.cast(),
false,
true,
Some(options.vocab),
local_context,
defined,
remote_contexts.clone(),
options,
)
.await?;

// If the expanded type is @json or @none, and processing mode is
// json-ld-1.0, an invalid type mapping error has been detected and
// processing is aborted.
if options.processing_mode == ProcessingMode::JsonLd1_0
&& (typ == Term::Keyword(Keyword::Json)
|| typ == Term::Keyword(Keyword::None))
{
return Err(Error::InvalidTypeMapping);
}
if let Some(typ) = typ {
// If the expanded type is @json or @none, and processing mode is
// json-ld-1.0, an invalid type mapping error has been detected and
// processing is aborted.
if options.processing_mode == ProcessingMode::JsonLd1_0
&& (typ == Term::Keyword(Keyword::Json)
|| typ == Term::Keyword(Keyword::None))
{
return Err(Error::InvalidTypeMapping);
}

if let Ok(typ) = typ.try_into() {
// Set the type mapping for definition to type.
definition.typ = Some(typ);
} else {
return Err(Error::InvalidTypeMapping);
if let Ok(typ) = typ.try_into() {
// Set the type mapping for definition to type.
definition.typ = Some(typ);
} else {
return Err(Error::InvalidTypeMapping);
}
}
}

Expand Down Expand Up @@ -264,15 +266,15 @@ where
active_context,
Nullable::Some(reverse_value.as_str().into()),
false,
true,
Some(options.vocab),
local_context,
defined,
remote_contexts,
options,
)
.await?
{
Term::Id(mapping) if mapping.is_valid() => {
Some(Term::Id(mapping)) if mapping.is_valid() => {
definition.value = Some(Term::Id(mapping))
}
_ => return Err(Error::InvalidIriMapping),
Expand Down Expand Up @@ -348,27 +350,27 @@ where
active_context,
Nullable::Some(id_value.into()),
false,
true,
Some(options.vocab),
local_context,
defined,
remote_contexts.clone(),
options,
)
.await?
{
Term::Keyword(Keyword::Context) => {
Some(Term::Keyword(Keyword::Context)) => {
// if it equals `@context`, an invalid keyword alias error has
// been detected and processing is aborted.
return Err(Error::InvalidKeywordAlias);
}
Term::Id(prop) if !prop.is_valid() => {
Some(Term::Id(prop)) if !prop.is_valid() => {
// If the resulting IRI mapping is neither a keyword,
// nor an IRI, nor a blank node identifier, an
// invalid IRI mapping error has been detected and processing
// is aborted;
return Err(Error::InvalidIriMapping);
}
value => Some(value),
value => value,
};

// If `term` contains a colon (:) anywhere but as the first or
Expand All @@ -394,14 +396,14 @@ where
active_context,
Nullable::Some((&term).into()),
false,
true,
Some(options.vocab),
local_context,
defined,
remote_contexts.clone(),
options,
)
.await?;
if definition.value != Some(expanded_term) {
if definition.value != expanded_term {
return Err(Error::InvalidIriMapping);
}
}
Expand Down Expand Up @@ -507,11 +509,11 @@ where
iri_ref.as_str(),
)),
false,
true,
) {
Term::Id(Id::Valid(ValidId::Iri(id))) => {
definition.value = Some(id.into())
}
Some(options.vocab),
)? {
Some(Term::Id(Id::Valid(
ValidId::Iri(id),
))) => definition.value = Some(id.into()),
// If the resulting IRI mapping is not an IRI, an invalid IRI mapping
// error has been detected and processing is aborted.
_ => return Err(Error::InvalidIriMapping),
Expand Down Expand Up @@ -637,9 +639,9 @@ where
active_context,
Nullable::Some(index_value.as_str().into()),
false,
true,
) {
Term::Id(Id::Valid(ValidId::Iri(_))) => (),
Some(options.vocab),
)? {
Some(Term::Id(Id::Valid(ValidId::Iri(_)))) => (),
_ => return Err(Error::InvalidTermDefinition),
}

Expand Down
Loading

0 comments on commit 51b19b9

Please sign in to comment.