Debian Codegraph @

src/decoding/object.rs

raw symbols SCIP
1 use craterust-analyzer cargo bendy 0.6.1 crate/::{
2 decodingrust-analyzer cargo bendy 0.6.1 decoding/::{DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#, ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#},
3 state_trackerrust-analyzer cargo bendy 0.6.1 state_tracker/::Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#,
4 };
5
6 /// An object read from a decoder
7 pub enum Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#<'obj, 'ser: 'obj> {
8 /// A list of arbitrary objects
9 Listrust-analyzer cargo bendy 0.6.1 decoding/object/Object#List#(ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>),
10 /// A map of string-valued keys to arbitrary objects
11 Dictrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Dict#(DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>),
12 /// An unparsed integer
13 Integerrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Integer#(&'ser str),
14 /// A byte string
15 Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(&'ser [u8]),
16 }
17
18 impl<'obj, 'ser: 'obj> Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#<'obj, 'ser> {
19 pub fn into_tokenrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]into_token().(self) -> Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#<'ser> {
20 match self {
21 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Listrust-analyzer cargo bendy 0.6.1 decoding/object/Object#List#(_) => Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Listrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#List#,
22 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Dictrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Dict#(_) => Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Dictrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Dict#,
23 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(bytes) => Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(bytes),
24 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Integerrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Integer#(num) => Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Numrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Num#(num),
25 }
26 }
27
28 /// Try to treat the object as a byte string, mapping [`Object::Bytes(v)`] into
29 /// [`Ok(v)`]. Any other variant returns the given default value.
30 ///
31 /// Default arguments passed into `bytes_or` are eagerly evaluated; if you
32 /// are passing the result of a function call, it is recommended to use
33 /// [`bytes_or_else`], which is lazily evaluated.
34 ///
35 /// [`Object::Bytes(v)`]: self::Object::Bytes
36 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
37 /// [`bytes_or_else`]: self::Object::bytes_or_else
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// use bendy::decoding::Object;
43 ///
44 /// let x = Object::Bytes(b"foo");
45 /// assert_eq!(Ok(&b"foo"[..]), x.bytes_or(Err("failure")));
46 ///
47 /// let x = Object::Integer("foo");
48 /// assert_eq!(Err("failure"), x.bytes_or(Err("failure")));
49 /// ```
50 pub fn bytes_orrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]bytes_or().<ErrorT>(
51 self,
52 default: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser [u8], ErrorT>,
53 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser [u8], ErrorT> {
54 match self {
55 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
56 _ => default,
57 }
58 }
59
60 /// Try to treat the object as a byte string, mapping [`Object::Bytes(v)`] into
61 /// [`Ok(v)`]. Any other variant is passed into the given fallback method.
62 ///
63 /// [`Object::Bytes(v)`]: self::Object::Bytes
64 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
65 ///
66 /// # Examples
67 ///
68 /// ```
69 /// use bendy::decoding::Object;
70 ///
71 /// let x = Object::Bytes(b"foo");
72 /// assert_eq!(
73 /// Ok(&b"foo"[..]),
74 /// x.bytes_or_else(|obj| Err(obj.into_token().name()))
75 /// );
76 ///
77 /// let x = Object::Integer("foo");
78 /// assert_eq!(
79 /// Err("Num"),
80 /// x.bytes_or_else(|obj| Err(obj.into_token().name()))
81 /// );
82 /// ```
83 pub fn bytes_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]bytes_or_else().<ErrorT>(
84 self,
85 op: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Selfrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser [u8], ErrorT>,
86 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser [u8], ErrorT> {
87 match self {
88 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
89 _ => op(self),
90 }
91 }
92
93 /// Try to treat the object as a byte string, mapping [`Object::Bytes(v)`] into
94 /// [`Ok(v)`]. Any other variant results in an [`ErrorKind::UnexpectedToken`].
95 ///
96 /// [`Object::Bytes(v)`]: self::Object::Bytes
97 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
98 /// [`ErrorKind::UnexpectedToken`]: crate::decoding::ErrorKind::UnexpectedToken
99 ///
100 /// # Examples
101 ///
102 /// ```
103 /// use bendy::decoding::Object;
104 ///
105 /// let x = Object::Bytes(b"foo");
106 /// assert_eq!(b"foo", x.try_into_bytes().unwrap());
107 ///
108 /// let x = Object::Integer("foo");
109 /// assert!(x.try_into_bytes().is_err());
110 /// ```
111 pub fn try_into_bytesrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_bytes().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser [u8], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> {
112 self.bytes_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]bytes_or_else().(|obj| Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_tokenrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_token().("String", obj.into_tokenrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]into_token().().namerust-analyzer cargo bendy 0.6.1 state_tracker/token/impl#[`Token<'a>`]name().())))
113 }
114
115 /// Try to treat the object as an integer and return the internal string representation,
116 /// mapping [`Object::Integer(v)`] into [`Ok(v)`]. Any other variant returns the given
117 /// default value.
118 ///
119 /// Default arguments passed into `integer_or` are eagerly evaluated; if you are passing
120 /// the result of a function call, it is recommended to use [`integer_or_else`], which
121 /// is lazily evaluated.
122 ///
123 /// [`Object::Integer(v)`]: self::Object::Integer
124 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
125 /// [`integer_or_else`]: self::Object::integer_or_else
126 ///
127 /// # Examples
128 ///
129 /// ```
130 /// use bendy::decoding::Object;
131 ///
132 /// let x = Object::Integer("123");
133 /// assert_eq!(Ok(&"123"[..]), x.integer_or(Err("failure")));
134 ///
135 /// let x = Object::Bytes(b"foo");
136 /// assert_eq!(Err("failure"), x.integer_or(Err("failure")));
137 /// ```
138 pub fn integer_orrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]integer_or().<ErrorT>(
139 self,
140 default: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser str, ErrorT>,
141 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser str, ErrorT> {
142 match self {
143 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Integerrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Integer#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
144 _ => default,
145 }
146 }
147
148 /// Try to treat the object as an integer and return the internal string representation,
149 /// mapping [`Object::Integer(v)`] into [`Ok(v)`]. Any other variant is passed into the
150 /// given fallback method.
151 ///
152 /// [`Object::Integer(v)`]: self::Object::Integer
153 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
154 ///
155 /// # Examples
156 ///
157 /// ```
158 /// use bendy::decoding::Object;
159 ///
160 /// let x = Object::Integer("123");
161 /// assert_eq!(
162 /// Ok(&"123"[..]),
163 /// x.integer_or_else(|obj| Err(obj.into_token().name()))
164 /// );
165 ///
166 /// let x = Object::Bytes(b"foo");
167 /// assert_eq!(
168 /// Err("String"),
169 /// x.integer_or_else(|obj| Err(obj.into_token().name()))
170 /// );
171 /// ```
172 pub fn integer_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]integer_or_else().<ErrorT>(
173 self,
174 op: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Selfrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser str, ErrorT>,
175 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser str, ErrorT> {
176 match self {
177 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Integerrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Integer#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
178 _ => op(self),
179 }
180 }
181
182 /// Try to treat the object as an integer and return the internal string representation,
183 /// mapping [`Object::Integer(v)`] into [`Ok(v)`]. Any other variant results in an
184 /// [`ErrorKind::UnexpectedToken`].
185 ///
186 /// [`Object::Integer(v)`]: self::Object::Integer
187 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
188 /// [`ErrorKind::UnexpectedToken`]: crate::decoding::ErrorKind::UnexpectedToken
189 ///
190 /// # Examples
191 ///
192 /// ```
193 /// use bendy::decoding::Object;
194 ///
195 /// let x = Object::Integer("123");
196 /// assert_eq!("123", x.try_into_integer().unwrap());
197 ///
198 /// let x = Object::Bytes(b"foo");
199 /// assert!(x.try_into_integer().is_err());
200 /// ```
201 pub fn try_into_integerrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_integer().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'ser str, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> {
202 self.integer_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]integer_or_else().(|obj| Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_tokenrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_token().("Num", obj.into_tokenrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]into_token().().namerust-analyzer cargo bendy 0.6.1 state_tracker/token/impl#[`Token<'a>`]name().())))
203 }
204
205 /// Try to treat the object as a list and return the internal list content decoder,
206 /// mapping [`Object::List(v)`] into [`Ok(v)`]. Any other variant returns the given
207 /// default value.
208 ///
209 /// Default arguments passed into `list_or` are eagerly evaluated; if you are passing
210 /// the result of a function call, it is recommended to use [`list_or_else`], which is
211 /// lazily evaluated.
212 ///
213 /// [`Object::List(v)`]: self::Object::List
214 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
215 /// [`list_or_else`]: self::Object::list_or_else
216 ///
217 /// # Examples
218 ///
219 /// ```
220 /// use bendy::decoding::{Decoder, Object};
221 ///
222 /// let mut list_decoder = Decoder::new(b"le");
223 /// let x = list_decoder.next_object().unwrap().unwrap();
224 ///
225 /// assert!(x.list_or(Err("failure")).is_ok());
226 ///
227 /// let x = Object::Bytes(b"foo");
228 /// assert_eq!("failure", x.list_or(Err("failure")).unwrap_err());
229 /// ```
230 pub fn list_orrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]list_or().<ErrorT>(
231 self,
232 default: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>, ErrorT>,
233 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>, ErrorT> {
234 match self {
235 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Listrust-analyzer cargo bendy 0.6.1 decoding/object/Object#List#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
236 _ => default,
237 }
238 }
239
240 /// Try to treat the object as a list and return the internal list content decoder,
241 /// mapping [`Object::List(v)`] into [`Ok(v)`]. Any other variant is passed into the
242 /// given fallback method.
243 ///
244 /// [`Object::List(v)`]: self::Object::List
245 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
246 ///
247 /// # Examples
248 ///
249 /// ```
250 /// use bendy::decoding::{Decoder, Object};
251 ///
252 /// let mut list_decoder = Decoder::new(b"le");
253 /// let x = list_decoder.next_object().unwrap().unwrap();
254 ///
255 /// assert!(x.list_or_else(|obj| Err(obj.into_token().name())).is_ok());
256 ///
257 /// let x = Object::Bytes(b"foo");
258 /// assert_eq!(
259 /// "String",
260 /// x.list_or_else(|obj| Err(obj.into_token().name()))
261 /// .unwrap_err()
262 /// );
263 /// ```
264 pub fn list_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]list_or_else().<ErrorT>(
265 self,
266 op: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Selfrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>, ErrorT>,
267 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>, ErrorT> {
268 match self {
269 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Listrust-analyzer cargo bendy 0.6.1 decoding/object/Object#List#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
270 _ => op(self),
271 }
272 }
273
274 /// Try to treat the object as a list and return the internal list content decoder,
275 /// mapping [`Object::List(v)`] into [`Ok(v)`]. Any other variant results in an
276 /// [`ErrorKind::UnexpectedToken`].
277 ///
278 /// [`Object::List(v)`]: self::Object::List
279 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
280 /// [`ErrorKind::UnexpectedToken`]: crate::decoding::ErrorKind::UnexpectedToken
281 ///
282 /// # Examples
283 ///
284 /// ```
285 /// use bendy::decoding::{Decoder, Object};
286 ///
287 /// let mut list_decoder = Decoder::new(b"le");
288 /// let x = list_decoder.next_object().unwrap().unwrap();
289 ///
290 /// assert!(x.try_into_list().is_ok());
291 ///
292 /// let x = Object::Bytes(b"foo");
293 /// assert!(x.try_into_list().is_err());
294 /// ```
295 pub fn try_into_listrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_list().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#<'obj, 'ser>, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> {
296 self.list_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]list_or_else().(|obj| Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_tokenrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_token().("List", obj.into_tokenrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]into_token().().namerust-analyzer cargo bendy 0.6.1 state_tracker/token/impl#[`Token<'a>`]name().())))
297 }
298
299 /// Try to treat the object as a dictionary and return the internal dictionary content
300 /// decoder, mapping [`Object::Dict(v)`] into [`Ok(v)`]. Any other variant returns the
301 /// given default value.
302 ///
303 /// Default arguments passed to `dictionary_or` are eagerly evaluated; if you are passing
304 /// the result of a function call, it is recommended to use [`dictionary_or_else`], which
305 /// is lazily evaluated.
306 ///
307 /// [`Object::Dict(v)`]: self::Object::Dict
308 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
309 /// [`dictionary_or_else`]: self::Object::dictionary_or_else
310 ///
311 /// # Examples
312 ///
313 /// ```
314 /// use bendy::decoding::{Decoder, Object};
315 ///
316 /// let mut dict_decoder = Decoder::new(b"de");
317 /// let x = dict_decoder.next_object().unwrap().unwrap();
318 ///
319 /// assert!(x.dictionary_or(Err("failure")).is_ok());
320 ///
321 /// let x = Object::Bytes(b"foo");
322 /// assert_eq!("failure", x.dictionary_or(Err("failure")).unwrap_err());
323 /// ```
324 pub fn dictionary_orrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]dictionary_or().<ErrorT>(
325 self,
326 default: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>, ErrorT>,
327 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>, ErrorT> {
328 match self {
329 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Dictrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Dict#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
330 _ => default,
331 }
332 }
333
334 /// Try to treat the object as a dictionary and return the internal dictionary content
335 /// decoder, mapping [`Object::Dict(v)`] into [`Ok(v)`]. Any other variant is passed
336 /// into the given fallback method.
337 ///
338 /// [`Object::Dict(v)`]: self::Object::Dict
339 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
340 ///
341 /// # Examples
342 ///
343 /// ```
344 /// use bendy::decoding::{Decoder, Object};
345 ///
346 /// let mut dict_decoder = Decoder::new(b"de");
347 /// let x = dict_decoder.next_object().unwrap().unwrap();
348 ///
349 /// assert!(
350 /// x.dictionary_or_else(|obj| Err(obj.into_token().name()))
351 /// .is_ok()
352 /// );
353 ///
354 /// let x = Object::Bytes(b"foo");
355 /// assert_eq!(
356 /// "String",
357 /// x.dictionary_or_else(|obj| Err(obj.into_token().name()))
358 /// .unwrap_err()
359 /// );
360 /// ```
361 pub fn dictionary_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]dictionary_or_else().<ErrorT>(
362 self,
363 op: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Selfrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>, ErrorT>,
364 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>, ErrorT> {
365 match self {
366 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Dictrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Dict#(content) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content),
367 _ => op(self),
368 }
369 }
370
371 /// Try to treat the object as a dictionary and return the internal dictionary content
372 /// decoder, mapping [`Object::Dict(v)`] into [`Ok(v)`]. Any other variant results in
373 /// an [`ErrorKind::UnexpectedToken`].
374 ///
375 /// [`Object::Dict(v)`]: self::Object::Dict
376 /// [`Ok(v)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Ok
377 /// [`ErrorKind::UnexpectedToken`]: crate::decoding::ErrorKind::UnexpectedToken
378 ///
379 /// # Examples
380 ///
381 /// ```
382 /// use bendy::decoding::{Decoder, Object};
383 ///
384 /// let mut dict_decoder = Decoder::new(b"de");
385 /// let x = dict_decoder.next_object().unwrap().unwrap();
386 ///
387 /// assert!(x.try_into_dictionary().is_ok());
388 ///
389 /// let x = Object::Bytes(b"foo");
390 /// assert!(x.try_into_dictionary().is_err());
391 /// ```
392 pub fn try_into_dictionaryrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_dictionary().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#<'obj, 'ser>, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> {
393 self.dictionary_or_elserust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]dictionary_or_else().(|obj| Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_tokenrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_token().("Dict", obj.into_tokenrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]into_token().().namerust-analyzer cargo bendy 0.6.1 state_tracker/token/impl#[`Token<'a>`]name().())))
394 }
395 }