@@ -79,6 +79,25 @@ impl Value {
79
79
}
80
80
}
81
81
82
+ /// If the `Value` is a `Integer`, returns a the associated `Integer` data as `Ok`.
83
+ /// Returns `Err(Self)` otherwise.
84
+ ///
85
+ /// ```
86
+ /// # use ciborium::{Value, value::Integer};
87
+ /// #
88
+ /// let value = Value::Integer(17.into());
89
+ /// assert_eq!(value.into_integer(), Ok(Integer::from(17)));
90
+ ///
91
+ /// let value = Value::Bool(true);
92
+ /// assert_eq!(value.into_integer(), Err(Value::Bool(true)));
93
+ /// ```
94
+ pub fn into_integer ( self ) -> Result < Integer , Self > {
95
+ match self {
96
+ Value :: Integer ( int) => Ok ( int) ,
97
+ other => Err ( other) ,
98
+ }
99
+ }
100
+
82
101
/// Returns true if the `Value` is a `Bytes`. Returns false otherwise.
83
102
///
84
103
/// ```
@@ -127,6 +146,25 @@ impl Value {
127
146
}
128
147
}
129
148
149
+ /// If the `Value` is a `Bytes`, returns a the associated `Vec<u8>` data as `Ok`.
150
+ /// Returns `Err(Self)` otherwise.
151
+ ///
152
+ /// ```
153
+ /// # use ciborium::Value;
154
+ /// #
155
+ /// let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
156
+ /// assert_eq!(value.into_bytes(), Ok(vec![104, 101, 108, 108, 111]));
157
+ ///
158
+ /// let value = Value::Bool(true);
159
+ /// assert_eq!(value.into_bytes(), Err(Value::Bool(true)));
160
+ /// ```
161
+ pub fn into_bytes ( self ) -> Result < Vec < u8 > , Self > {
162
+ match self {
163
+ Value :: Bytes ( vec) => Ok ( vec) ,
164
+ other => Err ( other) ,
165
+ }
166
+ }
167
+
130
168
/// Returns true if the `Value` is a `Float`. Returns false otherwise.
131
169
///
132
170
/// ```
@@ -158,6 +196,25 @@ impl Value {
158
196
}
159
197
}
160
198
199
+ /// If the `Value` is a `Float`, returns a the associated `f64` data as `Ok`.
200
+ /// Returns `Err(Self)` otherwise.
201
+ ///
202
+ /// ```
203
+ /// # use ciborium::Value;
204
+ /// #
205
+ /// let value = Value::Float(17.);
206
+ /// assert_eq!(value.into_float(), Ok(17.));
207
+ ///
208
+ /// let value = Value::Bool(true);
209
+ /// assert_eq!(value.into_float(), Err(Value::Bool(true)));
210
+ /// ```
211
+ pub fn into_float ( self ) -> Result < f64 , Self > {
212
+ match self {
213
+ Value :: Float ( f) => Ok ( f) ,
214
+ other => Err ( other) ,
215
+ }
216
+ }
217
+
161
218
/// Returns true if the `Value` is a `Text`. Returns false otherwise.
162
219
///
163
220
/// ```
@@ -207,6 +264,25 @@ impl Value {
207
264
}
208
265
}
209
266
267
+ /// If the `Value` is a `String`, returns a the associated `String` data as `Ok`.
268
+ /// Returns `Err(Self)` otherwise.
269
+ ///
270
+ /// ```
271
+ /// # use ciborium::Value;
272
+ /// #
273
+ /// let value = Value::Text(String::from("hello"));
274
+ /// assert_eq!(value.into_text().as_deref(), Ok("hello"));
275
+ ///
276
+ /// let value = Value::Bool(true);
277
+ /// assert_eq!(value.into_text(), Err(Value::Bool(true)));
278
+ /// ```
279
+ pub fn into_text ( self ) -> Result < String , Self > {
280
+ match self {
281
+ Value :: Text ( s) => Ok ( s) ,
282
+ other => Err ( other) ,
283
+ }
284
+ }
285
+
210
286
/// Returns true if the `Value` is a `Bool`. Returns false otherwise.
211
287
///
212
288
/// ```
@@ -237,6 +313,25 @@ impl Value {
237
313
}
238
314
}
239
315
316
+ /// If the `Value` is a `Bool`, returns a the associated `bool` data as `Ok`.
317
+ /// Returns `Err(Self)` otherwise.
318
+ ///
319
+ /// ```
320
+ /// # use ciborium::Value;
321
+ /// #
322
+ /// let value = Value::Bool(false);
323
+ /// assert_eq!(value.into_bool(), Ok(false));
324
+ ///
325
+ /// let value = Value::Float(17.);
326
+ /// assert_eq!(value.into_bool(), Err(Value::Float(17.)));
327
+ /// ```
328
+ pub fn into_bool ( self ) -> Result < bool , Self > {
329
+ match self {
330
+ Value :: Bool ( b) => Ok ( b) ,
331
+ other => Err ( other) ,
332
+ }
333
+ }
334
+
240
335
/// Returns true if the `Value` is a `Null`. Returns false otherwise.
241
336
///
242
337
/// ```
@@ -302,6 +397,25 @@ impl Value {
302
397
}
303
398
}
304
399
400
+ /// If the `Value` is a `Tag`, returns a the associated pair of `u64` and `Box<value>` data as `Ok`.
401
+ /// Returns `Err(Self)` otherwise.
402
+ ///
403
+ /// ```
404
+ /// # use ciborium::Value;
405
+ /// #
406
+ /// let value = Value::Tag(7, Box::new(Value::Float(12.)));
407
+ /// assert_eq!(value.into_tag(), Ok((7, Box::new(Value::Float(12.)))));
408
+ ///
409
+ /// let value = Value::Bool(true);
410
+ /// assert_eq!(value.into_tag(), Err(Value::Bool(true)));
411
+ /// ```
412
+ pub fn into_tag ( self ) -> Result < ( u64 , Box < Value > ) , Self > {
413
+ match self {
414
+ Value :: Tag ( tag, value) => Ok ( ( tag, value) ) ,
415
+ other => Err ( other) ,
416
+ }
417
+ }
418
+
305
419
/// Returns true if the `Value` is an Array. Returns false otherwise.
306
420
///
307
421
/// ```
@@ -366,6 +480,30 @@ impl Value {
366
480
}
367
481
}
368
482
483
+ /// If the `Value` is a `Array`, returns a the associated `Vec<Value>` data as `Ok`.
484
+ /// Returns `Err(Self)` otherwise.
485
+ ///
486
+ /// ```
487
+ /// # use ciborium::{Value, value::Integer};
488
+ /// #
489
+ /// let mut value = Value::Array(
490
+ /// vec![
491
+ /// Value::Integer(17.into()),
492
+ /// Value::Float(18.),
493
+ /// ]
494
+ /// );
495
+ /// assert_eq!(value.into_array(), Ok(vec![Value::Integer(17.into()), Value::Float(18.)]));
496
+ ///
497
+ /// let value = Value::Bool(true);
498
+ /// assert_eq!(value.into_array(), Err(Value::Bool(true)));
499
+ /// ```
500
+ pub fn into_array ( self ) -> Result < Vec < Value > , Self > {
501
+ match self {
502
+ Value :: Array ( vec) => Ok ( vec) ,
503
+ other => Err ( other) ,
504
+ }
505
+ }
506
+
369
507
/// Returns true if the `Value` is a Map. Returns false otherwise.
370
508
///
371
509
/// ```
@@ -433,6 +571,29 @@ impl Value {
433
571
_ => None ,
434
572
}
435
573
}
574
+
575
+ /// If the `Value` is a `Map`, returns a the associated `Vec<(Value, Value)>` data as `Ok`.
576
+ /// Returns `Err(Self)` otherwise.
577
+ ///
578
+ /// ```
579
+ /// # use ciborium::Value;
580
+ /// #
581
+ /// let mut value = Value::Map(
582
+ /// vec![
583
+ /// (Value::Text(String::from("key")), Value::Float(18.)),
584
+ /// ]
585
+ /// );
586
+ /// assert_eq!(value.into_map(), Ok(vec![(Value::Text(String::from("key")), Value::Float(18.))]));
587
+ ///
588
+ /// let value = Value::Bool(true);
589
+ /// assert_eq!(value.into_map(), Err(Value::Bool(true)));
590
+ /// ```
591
+ pub fn into_map ( self ) -> Result < Vec < ( Value , Value ) > , Self > {
592
+ match self {
593
+ Value :: Map ( map) => Ok ( map) ,
594
+ other => Err ( other) ,
595
+ }
596
+ }
436
597
}
437
598
438
599
macro_rules! implfrom {
0 commit comments