Skip to main content

jsonld_context_processing/
processed.rs

1use iri_rs::IriBuf;
2use jsonld_core::Context;
3use rdfx::BlankIdBuf;
4use std::ops;
5
6/// An active context paired with a borrow of the `@context` it was built from.
7///
8/// Both halves are needed downstream: the active context drives expansion and
9/// compaction, while the original, syntactic context is what compaction embeds
10/// into its output so the result round-trips.
11///
12/// Derefs to the active context, so a caller that only needs that can use it
13/// directly.
14pub struct Processed<'l, T = IriBuf, B = BlankIdBuf> {
15    /// The context as it was written.
16    pub unprocessed: &'l jsonld_syntax::context::Context,
17    /// The active context it processed into.
18    pub processed: Context<T, B>,
19}
20
21impl<'l, T, B> Processed<'l, T, B> {
22    /// Pairs an active context with the `@context` it was built from.
23    pub fn new(unprocessed: &'l jsonld_syntax::context::Context, processed: Context<T, B>) -> Self {
24        Self { unprocessed, processed }
25    }
26
27    /// Returns the `@context` as it was written, before processing.
28    pub fn unprocessed(&self) -> &'l jsonld_syntax::context::Context {
29        self.unprocessed
30    }
31
32    /// Discards the original `@context` and returns just the active context.
33    pub fn into_processed(self) -> Context<T, B> {
34        self.processed
35    }
36
37    /// Borrows both halves as a [`ProcessedRef`].
38    pub fn as_ref(&self) -> ProcessedRef<'l, '_, T, B> {
39        ProcessedRef {
40            unprocessed: self.unprocessed,
41            processed: &self.processed,
42        }
43    }
44
45    /// Clones the original `@context` so the pair no longer borrows it.
46    pub fn into_owned(self) -> ProcessedOwned<T, B> {
47        ProcessedOwned {
48            unprocessed: self.unprocessed.clone(),
49            processed: self.processed,
50        }
51    }
52}
53
54impl<T, B> ops::Deref for Processed<'_, T, B> {
55    type Target = Context<T, B>;
56
57    fn deref(&self) -> &Self::Target {
58        &self.processed
59    }
60}
61
62impl<T, B> ops::DerefMut for Processed<'_, T, B> {
63    fn deref_mut(&mut self) -> &mut Self::Target {
64        &mut self.processed
65    }
66}
67
68/// A borrowed active context paired with a borrow of the `@context` it was built
69/// from.
70///
71/// The form document compaction takes its context in, since it only reads both
72/// halves. The two lifetimes are independent: the original `@context` normally
73/// outlives the active context borrowed from it.
74pub struct ProcessedRef<'l, 'a, T, B> {
75    /// The context as it was written.
76    pub unprocessed: &'l jsonld_syntax::context::Context,
77    /// The active context it processed into.
78    pub processed: &'a Context<T, B>,
79}
80
81impl<'l, 'a, T, B> ProcessedRef<'l, 'a, T, B> {
82    /// Pairs a borrowed active context with the `@context` it was built from.
83    pub fn new(unprocessed: &'l jsonld_syntax::context::Context, processed: &'a Context<T, B>) -> Self {
84        Self { unprocessed, processed }
85    }
86
87    /// Returns the `@context` as it was written, before processing.
88    #[must_use]
89    pub fn unprocessed(&self) -> &'l jsonld_syntax::context::Context {
90        self.unprocessed
91    }
92
93    /// Returns the active context.
94    #[must_use]
95    pub fn processed(&self) -> &'a Context<T, B> {
96        self.processed
97    }
98}
99
100/// An active context paired with an owned copy of the `@context` it was built
101/// from.
102///
103/// Borrows nothing, so it can be stored or returned past the lifetime of the
104/// document the `@context` was read from.
105pub struct ProcessedOwned<T, B> {
106    /// The context as it was written.
107    pub unprocessed: jsonld_syntax::context::Context,
108    /// The active context it processed into.
109    pub processed: Context<T, B>,
110}
111
112impl<T, B> ProcessedOwned<T, B> {
113    /// Pairs an active context with an owned copy of the `@context` it was built
114    /// from.
115    pub fn new(unprocessed: jsonld_syntax::context::Context, processed: Context<T, B>) -> Self {
116        Self { unprocessed, processed }
117    }
118
119    /// Returns the `@context` as it was written, before processing.
120    pub fn unprocessed(&self) -> &jsonld_syntax::context::Context {
121        &self.unprocessed
122    }
123
124    /// Returns the active context.
125    pub fn processed(&self) -> &Context<T, B> {
126        &self.processed
127    }
128
129    /// Borrows both halves as a [`ProcessedRef`].
130    pub fn as_ref(&self) -> ProcessedRef<'_, '_, T, B> {
131        ProcessedRef {
132            unprocessed: &self.unprocessed,
133            processed: &self.processed,
134        }
135    }
136}