Implement serialization for ID types.

This commit is contained in:
Jimmy Cuadra 2016-07-26 00:19:12 -07:00
parent d05822199a
commit d560ccebeb
3 changed files with 107 additions and 0 deletions

33
Cargo.lock generated
View File

@ -4,6 +4,8 @@ version = "0.1.0"
dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -15,6 +17,11 @@ dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dtoa"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "idna"
version = "0.1.0"
@ -25,6 +32,11 @@ dependencies = [
"unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itoa"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -57,6 +69,11 @@ dependencies = [
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "0.1.73"
@ -74,6 +91,22 @@ name = "regex-syntax"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "0.8.0-rc3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_json"
version = "0.8.0-rc1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread-id"
version = "2.0.0"

View File

@ -13,4 +13,8 @@ version = "0.1.0"
[dependencies]
lazy_static = "0.2.1"
regex = "0.1.73"
serde = "0.8.0-rc3"
url = "1.1.1"
[dev-dependencies]
serde_json = "0.8.0-rc1"

View File

@ -7,11 +7,16 @@
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate serde;
extern crate url;
#[cfg(test)]
extern crate serde_json;
use std::fmt::{Display, Formatter, Result as FmtResult};
use regex::Regex;
use serde::{Serialize, Serializer};
use url::{ParseError, Url};
pub use url::Host;
@ -327,8 +332,33 @@ impl Display for UserId {
}
}
impl Serialize for EventId {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
impl Serialize for RoomAliasId {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
impl Serialize for RoomId {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
impl Serialize for UserId {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
serializer.serialize_str(&self.to_string())
}
}
#[cfg(test)]
mod tests {
use serde_json::to_string;
use super::{Error, EventId, RoomAliasId, RoomId, UserId};
#[test]
@ -341,6 +371,16 @@ mod tests {
);
}
#[test]
fn serialize_valid_event_id() {
assert_eq!(
to_string(
&EventId::new("$39hvsi03hlne:example.com").expect("Failed to create EventId.")
).expect("Failed to convert EventId to JSON."),
r#""$39hvsi03hlne:example.com""#
);
}
#[test]
fn valid_event_id_with_explicit_standard_port() {
assert_eq!(
@ -403,6 +443,16 @@ mod tests {
);
}
#[test]
fn serialize_valid_room_alias_id() {
assert_eq!(
to_string(
&RoomAliasId::new("#ruma:example.com").expect("Failed to create RoomAliasId.")
).expect("Failed to convert RoomAliasId to JSON."),
r##""#ruma:example.com""##
);
}
#[test]
fn valid_room_alias_id_with_explicit_standard_port() {
assert_eq!(
@ -464,6 +514,16 @@ mod tests {
);
}
#[test]
fn serialize_valid_room_id() {
assert_eq!(
to_string(
&RoomId::new("!29fhd83h92h0:example.com").expect("Failed to create RoomId.")
).expect("Failed to convert RoomId to JSON."),
r#""!29fhd83h92h0:example.com""#
);
}
#[test]
fn valid_room_id_with_explicit_standard_port() {
assert_eq!(
@ -526,6 +586,16 @@ mod tests {
);
}
#[test]
fn serialize_valid_user_id() {
assert_eq!(
to_string(
&UserId::new("@carl:example.com").expect("Failed to create UserId.")
).expect("Failed to convert UserId to JSON."),
r#""@carl:example.com""#
);
}
#[test]
fn valid_user_id_with_explicit_standard_port() {
assert_eq!(