jsonld_core/document/
mod.rs1use std::{borrow::Borrow, hash::Hash, ops::Deref};
2
3use iri_rs::IriBuf;
4use rdfx::BlankIdBuf;
5
6pub mod expanded;
8pub mod flattened;
10
11pub use expanded::ExpandedDocument;
12pub use flattened::FlattenedDocument;
13
14use crate::RemoteDocument;
15
16pub type DocumentParts<I, B> = (RemoteDocument<I>, ExpandedDocument<I, B>);
18
19#[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 pub fn new(remote: RemoteDocument<I>, expanded: ExpandedDocument<I, B>) -> Self {
29 Self { remote, expanded }
30 }
31
32 pub fn into_remote(self) -> RemoteDocument<I> {
34 self.remote
35 }
36
37 pub fn into_compact(self) -> jsonld_syntax::Value {
39 self.remote.into_document()
40 }
41
42 pub fn into_expanded(self) -> ExpandedDocument<I, B> {
44 self.expanded
45 }
46
47 pub fn into_parts(self) -> DocumentParts<I, B> {
49 (self.remote, self.expanded)
50 }
51
52 pub fn as_remote(&self) -> &RemoteDocument<I> {
54 &self.remote
55 }
56
57 pub fn as_compact(&self) -> &jsonld_syntax::Value {
59 self.remote.document()
60 }
61
62 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