Debian Codegraph @

src/resource.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
2 borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#,
3 syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicPtrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicPtr#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#},
4 };
5
6 use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::Valuerust-analyzer cargo serde_json 1.0.145 value/Value#;
7
8 use craterust-analyzer cargo referencing 0.37.4 crate/::{Anchorrust-analyzer cargo referencing 0.37.4 anchors/Anchor#, Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#, Errorrust-analyzer cargo referencing 0.37.4 error/Error#, Resolvedrust-analyzer cargo referencing 0.37.4 resolver/Resolved#, Resolverrust-analyzer cargo referencing 0.37.4 resolver/Resolver#, Segmentsrust-analyzer cargo referencing 0.37.4 segments/Segments#};
9
10 pub(craterust-analyzer cargo referencing 0.37.4 crate/) trait JsonSchemaResourcerust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource# {
11 fn contentsrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#contents().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#;
12 fn draftrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#;
13 fn idrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#id().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
14 self.draftrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#draft().()
15 .id_ofrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]id_of().(self.contentsrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#contents().())
16 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|id| id.trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().('#'))
17 }
18 }
19
20 /// An owned document with a concrete interpretation under a JSON Schema specification.
21 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
22 pub struct Resourcerust-analyzer cargo referencing 0.37.4 resource/Resource# {
23 contentsrust-analyzer cargo referencing 0.37.4 resource/Resource#contents.: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
24 draftrust-analyzer cargo referencing 0.37.4 resource/Resource#draft.: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#,
25 }
26
27 impl Resourcerust-analyzer cargo referencing 0.37.4 resource/Resource# {
28 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn newrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]new().(contents: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, draft: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#) -> Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource] {
29 Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource] { contentsrust-analyzer cargo referencing 0.37.4 resource/Resource#contents., draftrust-analyzer cargo referencing 0.37.4 resource/Resource#draft. }
30 }
31 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn into_innerrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]into_inner().(self) -> (Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#) {
32 (self.draftrust-analyzer cargo referencing 0.37.4 resource/Resource#draft., self.contentsrust-analyzer cargo referencing 0.37.4 resource/Resource#contents.)
33 }
34 /// Resource contents.
35 #[must_use]
36 pub fn contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]contents().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.145 value/Value# {
37 &self.contentsrust-analyzer cargo referencing 0.37.4 resource/Resource#contents.
38 }
39 /// JSON Schema draft under which this contents is interpreted.
40 #[must_use]
41 pub fn draftrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft# {
42 self.draftrust-analyzer cargo referencing 0.37.4 resource/Resource#draft.
43 }
44 /// Create a resource with automatically detecting specification which applies to the contents.
45 ///
46 /// Unknown `$schema` values are treated as `Draft::Unknown`.
47 #[must_use]
48 pub fn from_contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]from_contents().(contents: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#) -> Resourcerust-analyzer cargo referencing 0.37.4 resource/Resource# {
49 Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#::defaultrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft][Default]default().().detectrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]detect().(&contents).create_resourcerust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]create_resource().(contents)
50 }
51 }
52
53 /// A borrowed document with a concrete interpretation under a JSON Schema specification.
54 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
55 pub struct ResourceRefrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#<'a> {
56 contentsrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#contents.: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
57 draftrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#draft.: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#,
58 }
59
60 impl<'a> ResourceRefrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#<'a> {
61 #[must_use]
62 pub fn newrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]new().(contents: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, draft: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#) -> Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`] {
63 Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`] { contentsrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#contents., draftrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#draft. }
64 }
65 #[must_use]
66 pub fn contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]contents().(&self) -> &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value# {
67 self.contentsrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#contents.
68 }
69 #[must_use]
70 pub fn draftrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft# {
71 self.draftrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#draft.
72 }
73
74 /// Create a resource-ref with automatically detecting specification which applies to the contents.
75 ///
76 /// Unknown `$schema` values are treated as `Draft::Unknown`.
77 #[must_use]
78 pub fn from_contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]from_contents().(contents: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#) -> Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`] {
79 let draft = Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#::defaultrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft][Default]default().().detectrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]detect().(contents);
80 Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]::newrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]new().(contents, draft)
81 }
82
83 #[must_use]
84 pub fn idrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'a>`]id().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
85 JsonSchemaResourcerust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#::idrust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource#id().(self)
86 }
87 }
88
89 impl JsonSchemaResourcerust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource# for ResourceRefrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#<'_> {
90 fn contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'_>`][JsonSchemaResource]contents().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.145 value/Value# {
91 self.contentsrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#contents.
92 }
93
94 fn draftrust-analyzer cargo referencing 0.37.4 resource/impl#[`ResourceRef<'_>`][JsonSchemaResource]draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft# {
95 self.draftrust-analyzer cargo referencing 0.37.4 resource/ResourceRef#draft.
96 }
97 }
98
99 /// A pointer to a pinned resource.
100 pub(craterust-analyzer cargo referencing 0.37.4 crate/) struct InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr# {
101 contentsrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#contents.: AtomicPtrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicPtr#<Valuerust-analyzer cargo serde_json 1.0.145 value/Value#>,
102 draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#,
103 }
104
105 impl Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr# {
106 fn clonerust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][Clone]clone().(&self) -> Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][Clone] {
107 Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][Clone] {
108 contentsrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#contents.: AtomicPtrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicPtr#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[`AtomicPtr<T>`]new().(self.contentsrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#contents..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[`AtomicPtr<T>`]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#)),
109 draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.: self.draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.,
110 }
111 }
112 }
113
114 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr# {
115 fn fmtrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][Debug]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
116 f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("InnerResourcePtr")
117 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("contents", self.contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]contents().())
118 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("draft", &self.draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.)
119 .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().()
120 }
121 }
122
123 impl InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr# {
124 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn newrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]new().(contents: *const Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, draft: Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#) -> Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr] {
125 Selfrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr] {
126 contentsrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#contents.: AtomicPtrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicPtr#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[`AtomicPtr<T>`]new().(contents.cast_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast_mut().()),
127 draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.,
128 }
129 }
130
131 #[allow(unsafe_code)]
132 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]contents().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.145 value/Value# {
133 // SAFETY: The pointer is valid as long as the registry exists
134 unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.contentsrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#contents..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[`AtomicPtr<T>`]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) }
135 }
136
137 #[inline]
138 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft# {
139 self.draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.
140 }
141
142 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn anchorsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]anchors().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = Anchorrust-analyzer cargo referencing 0.37.4 anchors/Anchor#> + '_ {
143 self.draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]draft().().anchorsrust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]anchors().(self.contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]contents().())
144 }
145
146 pub(craterust-analyzer cargo referencing 0.37.4 crate/) fn pointerrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]pointer().<'r>(
147 &'r self,
148 pointer: &str,
149 mut resolver: Resolverrust-analyzer cargo referencing 0.37.4 resolver/Resolver#<'r>,
150 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Resolvedrust-analyzer cargo referencing 0.37.4 resolver/Resolved#<'r>, Errorrust-analyzer cargo referencing 0.37.4 error/Error#> {
151 // INVARIANT: Pointer always starts with `/`
152 let mut contents = self.contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]contents().();
153 let mut segments = Segmentsrust-analyzer cargo referencing 0.37.4 segments/Segments#::newrust-analyzer cargo referencing 0.37.4 segments/impl#[`Segments<'a>`]new().();
154 let original_pointer = pointer;
155 let pointer = percent_encodingrust-analyzer cargo percent-encoding 2.3.2 crate/::percent_decode_strrust-analyzer cargo percent-encoding 2.3.2 percent_decode_str().(&pointer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)
156 .decode_utf8rust-analyzer cargo percent-encoding 2.3.2 impl#[`PercentDecode<'a>`]decode_utf8().()
157 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::invalid_percent_encodingrust-analyzer cargo referencing 0.37.4 error/impl#[Error]invalid_percent_encoding().(original_pointer, err))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
158 for segment in pointer.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('/') {
159 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(array) = contents.as_arrayrust-analyzer cargo serde_json 1.0.145 value/impl#[Value]as_array().() {
160 let idx = segment
161 .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<usize>()
162 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| Errorrust-analyzer cargo referencing 0.37.4 error/Error#::invalid_array_indexrust-analyzer cargo referencing 0.37.4 error/impl#[Error]invalid_array_index().(original_pointer, segment, err))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
163 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) = array.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(idx) {
164 contents = next;
165 } else {
166 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo referencing 0.37.4 error/Error#::pointer_to_nowhererust-analyzer cargo referencing 0.37.4 error/impl#[Error]pointer_to_nowhere().(original_pointer));
167 }
168 segments.pushrust-analyzer cargo referencing 0.37.4 segments/impl#[`Segments<'a>`]push().(idx);
169 } else {
170 let segment = unescape_segmentrust-analyzer cargo referencing 0.37.4 resource/unescape_segment().(segment);
171 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) = contents.getrust-analyzer cargo serde_json 1.0.145 value/impl#[Value]get().(segment.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, T>`][`AsRef<T>`]as_ref().()) {
172 contents = next;
173 } else {
174 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo referencing 0.37.4 error/Error#::pointer_to_nowhererust-analyzer cargo referencing 0.37.4 error/impl#[Error]pointer_to_nowhere().(original_pointer));
175 }
176 segments.pushrust-analyzer cargo referencing 0.37.4 segments/impl#[`Segments<'a>`]push().(segment);
177 }
178 let last = &resolver;
179 let new_resolver = self.draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]draft().().maybe_in_subresourcerust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]maybe_in_subresource().(
180 &segments,
181 &resolver,
182 &InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#::newrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]new().(contents, self.draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]draft().()),
183 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
184 if new_resolver rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().last {
185 segments = Segmentsrust-analyzer cargo referencing 0.37.4 segments/Segments#::newrust-analyzer cargo referencing 0.37.4 segments/impl#[`Segments<'a>`]new().();
186 }
187 resolver = new_resolver;
188 }
189 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Resolvedrust-analyzer cargo referencing 0.37.4 resolver/Resolved#::newrust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolved<'r>`]new().(contents, resolver, self.draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]draft().()))
190 }
191 }
192
193 impl JsonSchemaResourcerust-analyzer cargo referencing 0.37.4 resource/JsonSchemaResource# for InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr# {
194 fn contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][JsonSchemaResource]contents().(&self) -> &Valuerust-analyzer cargo serde_json 1.0.145 value/Value# {
195 self.contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]contents().()
196 }
197
198 fn draftrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr][JsonSchemaResource]draft().(&self) -> Draftrust-analyzer cargo referencing 0.37.4 specification/Draft# {
199 self.draftrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#draft.
200 }
201 }
202
203 /// Unescape JSON Pointer path segments by converting `~1` to `/` and `~0` to `~`.
204 #[must_use]
205 pub fn unescape_segmentrust-analyzer cargo referencing 0.37.4 resource/unescape_segment().(mut segment: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> {
206 // Naively, checking for `~` and then replacing implies two passes
207 // over the input buffer. First, search in the first `contains('~')` call
208 // and then replacing `~1` & `~0` at once in a single pass.
209 //
210 // This implementation is ~3x faster than the naive one.
211 //
212 // **NOTE**: Heavily inspired by the implementation in `boon`:
213 // `https://github.com/santhosh-tekuri/boon/blob/fb09df2db19be75c32c0970b4bdedf1655f5f943/src/util.rs#L31`
214 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut tilde_idx) = segment.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('~') else {
215 return Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(segment);
216 };
217
218 let mut buffer = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(segment.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
219 loop {
220 let (before, after) = segment.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(tilde_idx);
221 buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(before);
222 segment = &after[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
223 let next_char_size = match segment.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() {
224 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('1') => {
225 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('/');
226 1
227 }
228 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('0') => {
229 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('~');
230 1
231 }
232 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) => {
233 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('~');
234 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(next);
235 next.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().()
236 }
237 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
238 buffer.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('~');
239 break;
240 }
241 };
242 segment = &segment[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().next_char_size..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
243 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next_tilde_idx) = segment.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('~') else {
244 buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(segment);
245 break;
246 };
247 tilde_idx = next_tilde_idx;
248 }
249 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(buffer)
250 }
251
252 #[cfg(test)]
253 mod testsrust-analyzer cargo referencing 0.37.4 resource/tests/ {
254 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#, syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#};
255
256 use craterust-analyzer cargo referencing 0.37.4 crate/::{resourcerust-analyzer cargo referencing 0.37.4 resource/::InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#, Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#, Registryrust-analyzer cargo referencing 0.37.4 registry/Registry#};
257
258 use superrust-analyzer cargo referencing 0.37.4 resource/::unescape_segmentrust-analyzer cargo referencing 0.37.4 resource/unescape_segment().;
259 use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::jsonrust-analyzer cargo serde_json 1.0.145 macros/json!;
260 use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:;
261
262 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("abc")]
263 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a~0b")]
264 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a~1b")]
265 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~01")]
266 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~10")]
267 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a~0~1b")]
268 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~"; "single tilde")]
269 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~~"; "double tilde")]
270 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~~~~~"; "many tildas")]
271 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~2")]
272 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a~c")]
273 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~0~1~")]
274 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("")]
275 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a/d")]
276 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("a~01b")]
277 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("🌟~0🌠~1🌡️"; "Emojis with escapes")]
278 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~🌟"; "Tilde followed by emoji")]
279 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("Café~0~1"; "Accented characters with escapes")]
280 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~é"; "Tilde followed by accented character")]
281 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("αβγ"; "Greek")]
282 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~αβγ"; "Tilde followed by Greek")]
283 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("∀∂∈ℝ∧∪≡∞"; "Mathematical symbols")]
284 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~∀∂∈ℝ∧∪≡∞"; "Tilde followed by mathematical symbols")]
285 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("¡¢£¤¥¦§¨©"; "Special characters")]
286 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~¡¢£¤¥¦§¨©"; "Tilde followed by special characters")]
287 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("\u{10FFFF}"; "Highest valid Unicode code point")]
288 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:("~\u{10FFFF}"; "Tilde followed by highest valid Unicode code point")]
289 fn test_unescape_segment_equivalencerust-analyzer cargo referencing 0.37.4 resource/tests/test_unescape_segment_equivalence().(input: &str) {
290 let unescaped = unescape_segmentrust-analyzer cargo referencing 0.37.4 resource/unescape_segment().(input);
291 let double_replaced = input.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("~1", "/").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("~0", "~");
292 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(unescaped, double_replaced, "Failed for: {input}");
293 }
294
295 fn create_test_registryrust-analyzer cargo referencing 0.37.4 resource/tests/create_test_registry().() -> Registryrust-analyzer cargo referencing 0.37.4 registry/Registry# {
296 let schema = Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#::Draft202012rust-analyzer cargo referencing 0.37.4 specification/Draft#Draft202012#.create_resourcerust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]create_resource().(jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({
297 "type": "object",
298 "properties": {
299 "foo": { "type": "string" },
300 "bar": { "type": "array", "items": [{"type": "number"}, {"type": "boolean"}] }
301 }
302 }));
303 Registryrust-analyzer cargo referencing 0.37.4 registry/Registry#::try_newrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_new().("http://example.com", schema).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid resources")
304 }
305
306 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
307 fn test_empty_refrust-analyzer cargo referencing 0.37.4 resource/tests/test_empty_ref().() {
308 let schema = Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#::Draft202012rust-analyzer cargo referencing 0.37.4 specification/Draft#Draft202012#.create_resourcerust-analyzer cargo referencing 0.37.4 specification/impl#[Draft]create_resource().(jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({
309 "type": "object",
310 "properties": {
311 "foo": { "type": "string" }
312 }
313 }));
314 let registry =
315 Registryrust-analyzer cargo referencing 0.37.4 registry/Registry#::try_newrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_new().("http://example.com", schema.clonerust-analyzer cargo referencing 0.37.4 resource/impl#[Resource][Clone]clone().()).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid resources");
316 let resolver = registry
317 .try_resolverrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_resolver().("http://example.com")
318 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid base URI");
319
320 let resolved = resolver.lookuprust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolver<'r>`]lookup().("#").expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Lookup failed");
321 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(resolved.contentsrust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolved<'r>`]contents().(), schema.contentsrust-analyzer cargo referencing 0.37.4 resource/impl#[Resource]contents().());
322 }
323
324 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
325 fn test_inner_resource_ptr_debugrust-analyzer cargo referencing 0.37.4 resource/tests/test_inner_resource_ptr_debug().() {
326 let value = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]pin().(jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({
327 "foo": "bar",
328 "number": 42
329 }));
330
331 let ptr = InnerResourcePtrrust-analyzer cargo referencing 0.37.4 resource/InnerResourcePtr#::newrust-analyzer cargo referencing 0.37.4 resource/impl#[InnerResourcePtr]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::addr_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/addr_of!!(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/impl#[`Pin<Ptr>`][Deref]deref().value), Draftrust-analyzer cargo referencing 0.37.4 specification/Draft#::Draft202012rust-analyzer cargo referencing 0.37.4 specification/Draft#Draft202012#);
332
333 let expected = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
334 "InnerResourcePtr {{ contents: {:?}, draft: Draft202012 }}",
335 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/impl#[`Pin<Ptr>`][Deref]deref().value
336 );
337 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{ptr:?}"), expected);
338 }
339
340 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
341 fn test_percent_encoded_non_utf8rust-analyzer cargo referencing 0.37.4 resource/tests/test_percent_encoded_non_utf8().() {
342 let registry = create_test_registryrust-analyzer cargo referencing 0.37.4 resource/tests/create_test_registry().();
343 let resolver = registry
344 .try_resolverrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_resolver().("http://example.com")
345 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid base URI");
346
347 let result = resolver.lookuprust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolver<'r>`]lookup().("#/%FF");
348 let error = result.expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("Should fail");
349 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
350 error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
351 "Invalid percent encoding in pointer '/%FF': the decoded bytes do not represent valid UTF-8"
352 );
353 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.sourcerust-analyzer cargo referencing 0.37.4 error/impl#[Error][Error]source().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
354 }
355
356 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
357 fn test_array_index_as_stringrust-analyzer cargo referencing 0.37.4 resource/tests/test_array_index_as_string().() {
358 let registry = create_test_registryrust-analyzer cargo referencing 0.37.4 resource/tests/create_test_registry().();
359 let resolver = registry
360 .try_resolverrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_resolver().("http://example.com")
361 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid base URI");
362
363 let result = resolver.lookuprust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolver<'r>`]lookup().("#/properties/bar/items/one");
364 let error = result.expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("Should fail");
365 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
366 error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
367 "Failed to parse array index 'one' in pointer '/properties/bar/items/one'"
368 );
369 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.sourcerust-analyzer cargo referencing 0.37.4 error/impl#[Error][Error]source().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
370 }
371
372 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
373 fn test_array_index_out_of_boundsrust-analyzer cargo referencing 0.37.4 resource/tests/test_array_index_out_of_bounds().() {
374 let registry = create_test_registryrust-analyzer cargo referencing 0.37.4 resource/tests/create_test_registry().();
375 let resolver = registry
376 .try_resolverrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_resolver().("http://example.com")
377 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid base URI");
378
379 let result = resolver.lookuprust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolver<'r>`]lookup().("#/properties/bar/items/2");
380 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
381 result.expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("Should fail").to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
382 "Pointer '/properties/bar/items/2' does not exist"
383 );
384 }
385
386 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
387 fn test_unknown_propertyrust-analyzer cargo referencing 0.37.4 resource/tests/test_unknown_property().() {
388 let registry = create_test_registryrust-analyzer cargo referencing 0.37.4 resource/tests/create_test_registry().();
389 let resolver = registry
390 .try_resolverrust-analyzer cargo referencing 0.37.4 registry/impl#[Registry]try_resolver().("http://example.com")
391 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Invalid base URI");
392
393 let result = resolver.lookuprust-analyzer cargo referencing 0.37.4 resolver/impl#[`Resolver<'r>`]lookup().("#/properties/baz");
394 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
395 result.expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("Should fail").to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
396 "Pointer '/properties/baz' does not exist"
397 );
398 }
399 }