···
36
36
/// # use pretty_assertions::assert_eq;
37
37
/// #
38
38
/// let store = MemoryStore::new(); // NOTE: completely blank through entire example
39
39
-
/// let missing_cid: Cid = CidGeneric::try_from(
40
40
-
/// "bafyreickxqyrg7hhhdm2z24kduovd4k4vvbmfmenzn7nc6pxg6qzjm2v44",
41
41
-
/// )
39
39
+
/// let missing_cid: Cid = CidGeneric::try_from("bafyreickxqyrg7hhhdm2z24kduovd4k4vvbmfmenzn7nc6pxg6qzjm2v44")
42
40
/// .unwrap();
43
41
///
44
42
/// let dag = ipld!({"a": 1, "b": missing_cid});
···
177
175
pub fn stub(mut self, ipld: Ipld) -> Box<I> {
178
176
self.inliner
179
177
.deref_mut()
180
180
-
.resolve(InlineIpld::new(self.needs, ipld).into());
178
178
+
.resolve(InlineIpld::new(Some(self.needs), ipld).into());
181
179
182
180
self.inliner
183
181
}
···
19
19
/// # use inline_ipld::{cid, InlineIpld};
20
20
/// # use libipld::{ipld, Ipld};
21
21
/// #
22
22
-
/// let inlined = InlineIpld::new_inherit_link(ipld!([1, 2, 3]));
22
22
+
/// let inlined = InlineIpld::new(None, ipld!([1, 2, 3]));
23
23
/// let ipld = ipld!({"a": 1, "b": inlined});
24
24
/// assert_eq!(ipld, ipld!({"a": 1, "b": {"/": {".": [1, 2, 3]}}}));
25
25
/// ```
···
61
61
///
62
62
/// Note that [`new`][Self::new] *does not* check that the [`Cid`] actually corresponds to the associated [`Ipld`].
63
63
///
64
64
+
/// [`new`][Self::new] wraps the enclosed [`Ipld`] in the correct delimiters.
65
65
+
/// If the `cid` field is omitted, the extracted IPLD link will inherit the CID configuration
66
66
+
/// of its parent during extraction. // FIXME actually implement this feature whoops!
67
67
+
///
64
68
/// # Arguments
65
69
///
66
66
-
/// * `cid` - The [`Cid`] for the `"&"` field
70
70
+
/// * `cid` - An optional [`Cid`] for the `"&"` field
67
71
/// * `ipld` - The [`Ipld`] to inline
72
72
+
///
73
73
+
/// # Examples
74
74
+
///
75
75
+
/// ```
76
76
+
/// # use inline_ipld::{cid, InlineIpld};
77
77
+
/// # use libipld::{ipld, Ipld};
78
78
+
/// #
79
79
+
/// let observed = InlineIpld::new(None, ipld!([1, 2, 3]));
80
80
+
/// assert_eq!(observed, ipld!({"/": {".": ipld!([1, 2, 3])}}));
81
81
+
/// ```
68
82
///
69
83
/// ```
70
84
/// # use inline_ipld::{cid, InlineIpld};
···
72
86
/// # use std::str::FromStr;
73
87
/// #
74
88
/// let cid: Cid = FromStr::from_str("bafyreihscx57i276zr5pgnioa5omevods6eseu5h4mllmow6csasju6eqi").unwrap();
75
75
-
/// assert_eq!(InlineIpld::new(cid, ipld!([1, 2, 3])), ipld!({
89
89
+
/// assert_eq!(InlineIpld::new(Some(cid), ipld!([1, 2, 3])), ipld!({
76
90
/// "/": {
77
91
/// ".": ipld!([1, 2, 3]),
78
92
/// "&": cid
79
93
/// }
80
94
/// }));
81
95
/// ```
82
82
-
pub fn new(cid: Cid, ipld: Ipld) -> Self {
83
83
-
InlineIpld {
84
84
-
cid: Some(cid),
85
85
-
ipld: ipld!({
86
86
-
"/": {
87
87
-
"&": Ipld::Link(cid),
88
88
-
".": ipld
89
89
-
}
90
90
-
}),
91
91
-
}
92
92
-
}
93
93
-
94
94
-
/// Create inline-delimited [`Ipld`], but omit the `"&"` field
95
95
-
///
96
96
-
/// [`new_inherit_link`][Self::new_inherit_link] wraps the enclosed [`Ipld`] in the correct delimiters.
97
97
-
/// It omits the `"&"` field. Per the [spec], this causes the extracted
98
98
-
/// IPLD link to inherit the coedec and CID encoding of its parent.
99
96
///
100
97
/// [spec]: https://github.com/ucan-wg/ipld-inline-links
101
101
-
///
102
102
-
/// # Arguments
103
103
-
///
104
104
-
/// * `ipld` - The [`Ipld`] to wrap in the inlined delimiter
105
105
-
///
106
106
-
/// # Examples
107
107
-
///
108
108
-
/// ```
109
109
-
/// # use inline_ipld::{cid, InlineIpld};
110
110
-
/// # use libipld::{ipld, Ipld};
111
111
-
/// #
112
112
-
/// let observed = InlineIpld::new_inherit_link(ipld!([1, 2, 3]));
113
113
-
/// assert_eq!(observed, ipld!({"/": {".": ipld!([1, 2, 3])}}));
114
114
-
/// ```
115
115
-
pub fn new_inherit_link(ipld: Ipld) -> Self {
98
98
+
pub fn new(cid: Option<Cid>, ipld: Ipld) -> Self {
99
99
+
let inner = match cid {
100
100
+
Some(link) => ipld!({".": ipld, "&": link}),
101
101
+
None => ipld!({ ".": ipld }),
102
102
+
};
103
103
+
116
104
InlineIpld {
117
117
-
cid: None,
118
118
-
ipld: ipld!({"/": {".": ipld!(ipld)}}),
105
105
+
cid,
106
106
+
ipld: ipld!({ "/": inner }),
119
107
}
120
108
}
121
109
···
124
112
/// Use with caution: non-inlined [`Ipld`] can be maked as inlined with this function.
125
113
///
126
114
/// This is used in place of [`from`][`From::from`] to highlight that no conversion or checks happen.
127
127
-
/// If conversion is desired, use [`Self::new_inherit_link`].
115
115
+
/// If conversion is desired, use [`Self::new`] with the `cid` field set to [`None`].
128
116
///
129
117
/// # Arguments
130
118
///
···
170
158
/// #
171
159
/// let ipld = ipld!({"a": 1, "b": {"/": {".": [1, 2, 3]}}});
172
160
/// let cid = cid::new(&ipld, DagCborCodec, &Sha2_256, Version::V1);
173
173
-
/// let inlined = InlineIpld::new(cid, ipld);
161
161
+
/// let inlined = InlineIpld::new(Some(cid), ipld);
174
162
/// assert_eq!(inlined.cid(), Some(cid));
175
163
/// ```
176
164
pub fn cid(&self) -> Option<Cid> {