Skip to content

Commit fd8c2e1

Browse files
dodomorandienarxbot
authored andcommitted
Add into_* methods to Value
1 parent ff7e212 commit fd8c2e1

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

ciborium/src/value/mod.rs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ impl Value {
7979
}
8080
}
8181

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+
82101
/// Returns true if the `Value` is a `Bytes`. Returns false otherwise.
83102
///
84103
/// ```
@@ -127,6 +146,25 @@ impl Value {
127146
}
128147
}
129148

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+
130168
/// Returns true if the `Value` is a `Float`. Returns false otherwise.
131169
///
132170
/// ```
@@ -158,6 +196,25 @@ impl Value {
158196
}
159197
}
160198

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+
161218
/// Returns true if the `Value` is a `Text`. Returns false otherwise.
162219
///
163220
/// ```
@@ -207,6 +264,25 @@ impl Value {
207264
}
208265
}
209266

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+
210286
/// Returns true if the `Value` is a `Bool`. Returns false otherwise.
211287
///
212288
/// ```
@@ -237,6 +313,25 @@ impl Value {
237313
}
238314
}
239315

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+
240335
/// Returns true if the `Value` is a `Null`. Returns false otherwise.
241336
///
242337
/// ```
@@ -302,6 +397,25 @@ impl Value {
302397
}
303398
}
304399

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+
305419
/// Returns true if the `Value` is an Array. Returns false otherwise.
306420
///
307421
/// ```
@@ -366,6 +480,30 @@ impl Value {
366480
}
367481
}
368482

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+
369507
/// Returns true if the `Value` is a Map. Returns false otherwise.
370508
///
371509
/// ```
@@ -433,6 +571,29 @@ impl Value {
433571
_ => None,
434572
}
435573
}
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+
}
436597
}
437598

438599
macro_rules! implfrom {

0 commit comments

Comments
 (0)