Skip to content
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,4 @@ pub use crate::value::Value;

#[cfg(feature = "tags")]
pub use tag::EncodeCborTag;
pub use tag::TaggedString;
36 changes: 36 additions & 0 deletions src/tag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde::de::{Deserialize, Deserializer};

/// Wrapper struct to handle encoding Cbor semantic tags.
#[derive(Deserialize)]
Expand Down Expand Up @@ -38,3 +39,38 @@ impl<T: Serialize> Serialize for EncodeCborTag<T> {
state.end()
}
}

/// TaggedString
#[derive(Clone, Debug, PartialEq)]
pub struct TaggedString {
/// Tag
pub tag: u64,
/// Raw String to be tagged
pub data: String,
}

impl TaggedString {
/// Returns tag
pub fn tag(&self) -> u64 {
return self.tag;
}
}

impl Serialize for TaggedString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
EncodeCborTag::new(self.tag, &self.data).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for TaggedString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let wrapper = EncodeCborTag::deserialize(deserializer)?;
Ok(TaggedString {tag: wrapper.tag(), data: wrapper.value() })
}
}
4 changes: 4 additions & 0 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod ser;

use std::cmp::{Ord, Ordering, PartialOrd};
use std::collections::BTreeMap;
use crate::tag::TaggedString;

#[doc(inline)]
pub use self::de::from_value;
Expand Down Expand Up @@ -53,6 +54,8 @@ pub enum Value {
Map(BTreeMap<Value, Value>),
/// Semantic Tag
Tag(u64),
/// Tagged String
TaggedString(TaggedString),
// The hidden variant allows the enum to be extended
// with variants for tags and simple values.
#[doc(hidden)]
Expand Down Expand Up @@ -150,6 +153,7 @@ impl Value {
Array(_) => 4,
Map(_) => 5,
Tag(_) => 6,
TaggedString(_) => 8,
__Hidden => unreachable!(),
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl serde::Serialize for Value {
Value::Text(ref v) => serializer.serialize_str(&v),
Value::Array(ref v) => v.serialize(serializer),
Value::Tag(v) => serializer.serialize_u64(v),
Value::TaggedString(ref v) => {
use serde::ser::SerializeStruct;

let mut s = serializer.serialize_struct("EncodeCborTag", 2)?;
s.serialize_field("__cbor_tag_ser_tag", &Value::Tag(v.tag))?;
s.serialize_field("__cbor_tag_ser_data", &v.data.to_owned())?;
return s.end();
},
Value::Map(ref v) => {
if v.len() == 2 {
use serde::ser::SerializeStruct;
Expand Down
27 changes: 27 additions & 0 deletions tests/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,31 @@ mod std_tests {
serde_cbor::de::from_slice(&[0xd9, 0xd9, 0xf6, 0x83, 0x01, 0x02, 0x03]).unwrap();
assert_eq!(value, (55798, (1, 2, 3)));
}


#[cfg(feature = "tags")]
#[test]
fn test_tagged_string() {
/* D8 20 # tag(32)
67 # text(7)
6578616D706C65 # "example" */
use serde_cbor::TaggedString;
const CBOR_ENCODED: [u8;10] = [0xd8,0x20,0x67,0x65,0x78,0x61,0x6d,0x70,0x6c,0x65];

let tagged_string = TaggedString {tag: 32, data: String::from("example")};

// Serialize tagged string (CBOR encode)
let ser_result = serde_cbor::to_vec(&tagged_string).unwrap();
assert_eq!(ser_result, CBOR_ENCODED);
println!("{:?}", ser_result);


// Deserialize tagged string (CBOR decode)
let de_result: (TaggedString) =
serde_cbor::de::from_slice_with_scratch(&CBOR_ENCODED, &mut []).unwrap();
assert_eq!(32, de_result.tag);
assert_eq!(String::from("example"), de_result.data);
println!("{:?}", de_result);
}

}