Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Fixups for conversions #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,42 @@ impl_from!(Value::Integer, u64);
// u128 omitted because not all numbers fit in CBOR serialization
impl_from!(Value::Float, f32);
impl_from!(Value::Float, f64);
impl_from!(Value::Bytes, Vec<u8>);
impl_from!(Value::Text, String);
// TODO: figure out if these impls should be more generic or removed.
impl_from!(Value::Array, Vec<Value>);
impl_from!(Value::Map, BTreeMap<Value, Value>);
impl_from!(Value::Text, &str);

impl<T: Clone> From<&[T]> for Value
where
Value: From<T>,
{
fn from(value: &[T]) -> Self {
Value::Array(value.iter().map(|x| Value::from(x.clone())).collect())
}
}

impl<T> From<Vec<T>> for Value
where
Value: From<T>,
{
fn from(mut value: Vec<T>) -> Self {
Value::Array(value.drain(..).map(Value::from).collect())
}
}

impl<T, U> From<BTreeMap<T, U>> for Value
where
Value: From<T>,
Value: From<U>,
{
fn from(value: BTreeMap<T, U>) -> Self {
let mut map = BTreeMap::new();

for (key, val) in value {
map.insert(Value::from(key), Value::from(val));
}

Value::Map(map)
}
}

impl Value {
fn major_type(&self) -> u8 {
Expand Down
61 changes: 61 additions & 0 deletions tests/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! This set of tests confirms that the `From` conversions and `to_value()`
//! conversions produce the same output.

#![cfg(feature = "std")]

use serde_cbor::{value::to_value, Value};
use std::collections::BTreeMap;

#[test]
fn bool() {
assert_eq!(Value::from(true), to_value(true).unwrap());
assert_eq!(Value::from(false), to_value(false).unwrap());
}

#[test]
fn integer() {
assert_eq!(Value::from(127u8), to_value(127u8).unwrap());
assert_eq!(Value::from(127u16), to_value(127u16).unwrap());
assert_eq!(Value::from(127u32), to_value(127u32).unwrap());
assert_eq!(Value::from(127u64), to_value(127u64).unwrap());
assert_eq!(Value::from(127i8), to_value(127i8).unwrap());
assert_eq!(Value::from(127i16), to_value(127i16).unwrap());
assert_eq!(Value::from(127i32), to_value(127i32).unwrap());
assert_eq!(Value::from(127i64), to_value(127i64).unwrap());
}

#[test]
fn float() {
assert_eq!(Value::from(7.8f32), to_value(7.8f32).unwrap());
assert_eq!(Value::from(7.8f64), to_value(7.8f64).unwrap());
}

#[test]
fn bytes() {
let bytes = vec![0u8, 1u8, 2u8];

assert_eq!(Value::from(bytes.clone()), to_value(bytes).unwrap());
}

#[test]
fn string() {
let string = "foo".to_owned();

assert_eq!(Value::from(string.clone()), to_value(string).unwrap());
}

#[test]
fn array() {
let array: Vec<Value> = vec![1.into(), 2.into(), 3.into()];

assert_eq!(Value::from(array.clone()), to_value(array).unwrap());
}

#[test]
fn map() {
let mut map = BTreeMap::new();

map.insert(Value::from(1), Value::from(2));

assert_eq!(Value::from(map.clone()), to_value(map).unwrap());
}