diff options
-rw-r--r-- | src/stream/verb.rs | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/src/stream/verb.rs b/src/stream/verb.rs index 75a8dac..1eb9345 100644 --- a/src/stream/verb.rs +++ b/src/stream/verb.rs @@ -11,8 +11,10 @@ use serde::Deserialize; #[derive(Debug, PartialEq)] pub enum Verb { - Post, + Announce, + Create, Like, + Post, Update, } @@ -25,6 +27,12 @@ impl<'de> Deserialize<'de> for Verb { } } +/** + * Visitor used to deserialize an ActivityStreams verb from an Activity. + * + * Accepts both ActivityStream 1.0 and ActivityStreams 2.0 representations + * of activities. + */ struct VerbVisitor; impl<'de> serde::de::Visitor<'de> for VerbVisitor { @@ -38,15 +46,19 @@ impl<'de> serde::de::Visitor<'de> for VerbVisitor { where E: serde::de::Error, { - match s { - "http://activitystrea.ms/schema/1.0/post" => - Ok(Verb::Post), - "http://activitystrea.ms/schema/1.0/like" => + match s.to_lowercase().as_ref() { + "announce" | "http://activitystrea.ms/schema/1.0/announce" => + Ok(Verb::Announce), + "create" | "http://activitystrea.ms/schema/1.0/create" => + Ok(Verb::Create), + "like" | "http://activitystrea.ms/schema/1.0/like" => Ok(Verb::Like), - "http://activitystrea.ms/schema/1.0/update" => + "post" | "http://activitystrea.ms/schema/1.0/post" => + Ok(Verb::Post), + "update" | "http://activitystrea.ms/schema/1.0/update" => Ok(Verb::Update), _ => - Err(E::custom(format!("unknown activity streams verb"))) + Err(E::custom(format!("unknown activity streams verb: {}", s))) } } } @@ -57,15 +69,17 @@ mod test { use super::*; #[test] - fn deserialize_activitustreams_verb_from_json() { + fn deserialize_activitustreams_1_0_verb_from_json() { #[derive(Debug, Deserialize, PartialEq)] struct VerbTest { verb: Verb } let verbs = vec![ - (Verb::Post, "post"), + (Verb::Announce, "announce"), + (Verb::Create, "create"), (Verb::Like, "like"), + (Verb::Post, "post"), (Verb::Update, "update") ]; @@ -77,4 +91,33 @@ mod test { } } + + #[test] + fn deserialize_activitustreams_2_0_verb_from_json() { + #[derive(Debug, Deserialize, PartialEq)] + struct VerbTest { + verb: Verb + } + + let verbs = vec![ + (Verb::Announce, "Announce"), + (Verb::Announce, "announce"), + (Verb::Create, "Create"), + (Verb::Create, "create"), + (Verb::Like, "Like"), + (Verb::Like, "like"), + (Verb::Post, "Post"), + (Verb::Post, "post"), + (Verb::Update, "Update"), + (Verb::Update, "update") + ]; + + for v in verbs { + let verb: VerbTest = serde_json::from_str( + &format!(r#"{{"verb": "{}"}}"#, v.1) + ).unwrap(); + assert_eq!(v.0, verb.verb); + } + + } } |