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;
32mod nullable;
33mod print_ld;
34mod try_from_json;
35mod utils;
36
37pub use compact_iri::*;
38pub use compare::*;
39pub use container::{Container, ContainerKind};
40pub use context::{Context, ContextDocument, ContextEntry};
41pub use direction::*;
42pub use error::*;
43pub use expandable::*;
44pub use into_json::*;
45pub use jstrict::{
46    BorrowUnordered,
47    Kind,
48    Number,
49    NumberBuf,
50    Object,
51    Parse,
52    Print,
53    String,
54    Unordered,
55    UnorderedEq,
56    UnorderedHash,
57    UnorderedPartialEq,
58    Value,
59    object,
60    parse,
61    print,
62};
63pub use keyword::*;
64pub use lang::*;
65pub use nullable::*;
66pub use try_from_json::*;
67
68#[cfg(feature = "serde")]
69pub use jstrict::{from_value, to_value};
70
71#[derive(Clone, Copy, Debug)]
72/// Error raised when a JSON value has the wrong kind.
73pub struct Unexpected(jstrict::Kind, &'static [jstrict::Kind]);
74
75impl Unexpected {
76    /// Returns the kind of the value that was found.
77    #[must_use]
78    pub fn found(&self) -> jstrict::Kind {
79        self.0
80    }
81
82    /// Returns the kinds that were expected instead.
83    #[must_use]
84    pub fn expected(&self) -> &'static [jstrict::Kind] {
85        self.1
86    }
87}
88
89impl std::fmt::Display for Unexpected {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        write!(f, "unexpected {}, expected ", self.0)?;
92        for (i, kind) in self.1.iter().enumerate() {
93            if i > 0 {
94                f.write_str(" or ")?;
95            }
96            write!(f, "{kind}")?;
97        }
98        Ok(())
99    }
100}
101
102impl std::error::Error for Unexpected {}