Skip to main content

jsonld_core/document/
mod.rs

1use std::{borrow::Borrow, hash::Hash, ops::Deref};
2
3use iri_rs::IriBuf;
4use rdfx::BlankIdBuf;
5
6/// Expanded documents.
7pub mod expanded;
8/// Flattened documents.
9pub mod flattened;
10
11pub use expanded::ExpandedDocument;
12pub use flattened::FlattenedDocument;
13
14use crate::RemoteDocument;
15
16/// Remote document paired with its expanded form.
17pub type DocumentParts<I, B> = (RemoteDocument<I>, ExpandedDocument<I, B>);
18
19/// JSON-LD document in both compact and expanded form.
20#[derive(Debug, Clone)]
21pub struct Document<I = IriBuf, B = BlankIdBuf> {
22    remote: RemoteDocument<I>,
23    expanded: ExpandedDocument<I, B>,
24}
25
26impl<I, B> Document<I, B> {
27    /// Creates a new `Document`.
28    pub fn new(remote: RemoteDocument<I>, expanded: ExpandedDocument<I, B>) -> Self {
29        Self { remote, expanded }
30    }
31
32    /// Consumes the document, returning its remote (compact) form.
33    pub fn into_remote(self) -> RemoteDocument<I> {
34        self.remote
35    }
36
37    /// Consumes the document, returning its compact form as a JSON value.
38    pub fn into_compact(self) -> jsonld_syntax::Value {
39        self.remote.into_document()
40    }
41
42    /// Consumes the document, returning its expanded form.
43    pub fn into_expanded(self) -> ExpandedDocument<I, B> {
44        self.expanded
45    }
46
47    /// Consumes the document, returning its remote and expanded forms.
48    pub fn into_parts(self) -> DocumentParts<I, B> {
49        (self.remote, self.expanded)
50    }
51
52    /// Returns the remote (compact) form of the document.
53    pub fn as_remote(&self) -> &RemoteDocument<I> {
54        &self.remote
55    }
56
57    /// Returns the compact form of the document as a JSON value.
58    pub fn as_compact(&self) -> &jsonld_syntax::Value {
59        self.remote.document()
60    }
61
62    /// Returns the expanded form of the document.
63    pub fn as_expanded(&self) -> &ExpandedDocument<I, B> {
64        &self.expanded
65    }
66}
67
68impl<I, B> Deref for Document<I, B> {
69    type Target = ExpandedDocument<I, B>;
70
71    fn deref(&self) -> &Self::Target {
72        &self.expanded
73    }
74}
75
76impl<I, B> Borrow<RemoteDocument<I>> for Document<I, B> {
77    fn borrow(&self) -> &RemoteDocument<I> {
78        &self.remote
79    }
80}
81
82impl<I, B> Borrow<jsonld_syntax::Value> for Document<I, B> {
83    fn borrow(&self) -> &jsonld_syntax::Value {
84        self.remote.document()
85    }
86}
87
88impl<I, B> Borrow<ExpandedDocument<I, B>> for Document<I, B> {
89    fn borrow(&self) -> &ExpandedDocument<I, B> {
90        &self.expanded
91    }
92}
93
94impl<I: Eq + Hash, B: Eq + Hash> PartialEq for Document<I, B> {
95    fn eq(&self, other: &Self) -> bool {
96        self.expanded.eq(&other.expanded)
97    }
98}
99
100impl<I: Eq + Hash, B: Eq + Hash> Eq for Document<I, B> {}
101
102#[cfg(feature = "serde")]
103impl<I, B> serde::Serialize for Document<I, B> {
104    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105    where
106        S: serde::Serializer,
107    {
108        self.remote.document().serialize(serializer)
109    }
110}
111
112// TODO: `Document` lost its `LinkedData` / `LinkedDataGraph` impls when the
113// serialization layer moved to `ld_core`; they need to be rewritten against
114// `ld_core`'s `Interpretation`-based API.