Skip to main content

jsonld_syntax/
lib.rs

1//! Syntax of JSON-LD contexts.
2//!
3//! This crate provides the Rust types mirroring the JSON-LD grammar of a
4//! `@context`: context definitions, term definitions, keywords, container
5//! mappings, language tags and text directions. Each of them can be built
6//! from a JSON value with [`TryFromJson`], converted back with [`IntoJson`],
7//! and pretty-printed with the [`Print`] trait re-exported from `jstrict`.
8//!
9//! Interpreting a context — resolving remote references, expanding terms into
10//! IRIs, compacting IRIs back into terms — is the job of the sibling
11//! `jsonld-context-processing`, `jsonld-expansion` and `jsonld-compaction`
12//! crates.
13// On docs.rs, label every feature-gated item with the feature that unlocks it.
14// `doc(auto_cfg)` is still nightly-gated, and `docsrs` is set by docs.rs itself
15// (see `rustdoc-args` in Cargo.toml), so stable builds are unaffected.
16#![cfg_attr(docsrs, feature(doc_cfg))]
17#![cfg_attr(docsrs, doc(auto_cfg))]
18mod compact_iri;
19mod compare;
20/// Container mappings: the values a `@container` entry may take.
21pub mod container;
22/// Contexts, their definitions and term definitions.
23pub mod context;
24mod direction;
25mod error;
26mod expandable;
27pub mod hash;
28pub mod intern;
29mod into_json;
30mod keyword;
31mod lang;
32pub mod native_stack;
33mod nullable;
34mod print_ld;
35mod try_from_json;
36mod utils;
37
38pub use compact_iri::*;
39pub use compare::*;
40pub use container::{Container, ContainerKind};
41pub use context::{Context, ContextDocument, ContextEntry};
42pub use direction::*;
43pub use error::*;
44pub use expandable::*;
45pub use into_json::*;
46pub use jstrict::{
47    BorrowUnordered,
48    Kind,
49    Number,
50    NumberBuf,
51    Object,
52    Parse,
53    Print,
54    String,
55    Unordered,
56    UnorderedEq,
57    UnorderedHash,
58    UnorderedPartialEq,
59    Value,
60    object,
61    parse,
62    print,
63};
64pub use keyword::*;
65pub use lang::*;
66pub use nullable::*;
67pub use try_from_json::*;
68
69#[cfg(feature = "serde")]
70pub use jstrict::{from_value, to_value};
71
72#[derive(Clone, Copy, Debug)]
73/// Error raised when a JSON value has the wrong kind.
74pub struct Unexpected(jstrict::Kind, &'static [jstrict::Kind]);
75
76impl Unexpected {
77    /// Returns the kind of the value that was found.
78    #[must_use]
79    pub fn found(&self) -> jstrict::Kind {
80        self.0
81    }
82
83    /// Returns the kinds that were expected instead.
84    #[must_use]
85    pub fn expected(&self) -> &'static [jstrict::Kind] {
86        self.1
87    }
88}
89
90impl std::fmt::Display for Unexpected {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        write!(f, "unexpected {}, expected ", self.0)?;
93        for (i, kind) in self.1.iter().enumerate() {
94            if i > 0 {
95                f.write_str(" or ")?;
96            }
97            write!(f, "{kind}")?;
98        }
99        Ok(())
100    }
101}
102
103impl std::error::Error for Unexpected {}