diff --git a/Cargo.lock b/Cargo.lock index 68bd447..8a476da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,14 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "nushtyu" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "proc-macro2" version = "1.0.78" @@ -79,11 +87,3 @@ name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - -[[package]] -name = "yaskey" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", -] diff --git a/src/ap/mod.rs b/src/ap/mod.rs index b576098..e6eac78 100644 --- a/src/ap/mod.rs +++ b/src/ap/mod.rs @@ -32,6 +32,16 @@ pub enum ObjectType { Note, Tombstone, } +impl std::fmt::Display for ObjectType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{ + match self { + ObjectType::Person => write!(f, "Person"), + ObjectType::Like => write!(f, "Like"), + ObjectType::Note => write!(f, "Note"), + ObjectType::Tombstone => write!(f, "Tombstone"), + } + } +} pub enum Object { Person(Actor), @@ -40,18 +50,38 @@ pub enum Object { Tombstone, } +#[derive(Serialize, Deserialize,Debug,PartialEq)] +pub struct Activity{ + #[serde(rename = "type")] + pub activity_type: ObjectType, +} + pub fn json_to_actor(input: &String)->Actor { let return_data: Actor = serde_json::from_str(&input).unwrap(); return_data } + +pub fn parse_activity(input: &String)->Result { + let json_input: Result = serde_json::from_str(input); + if json_input.is_ok() { + match json_input.as_ref().unwrap().activity_type { + ObjectType::Person => { return Ok(Object::Person(json_to_actor(input)));}, + _ => { return Err(format!("Activity type {} not implemented",json_input.unwrap().activity_type)); } + } + } else { + Err("Failed to parse json data".to_string()) + } +} + #[cfg(test)] mod tests { use super::*; use std::fs::read_to_string; + #[test] fn parse_json_into_actor() { - let input_string = read_to_string("test-json.json").unwrap(); + let input_string = read_to_string("tests/test-json-person.json").unwrap(); let actor: Actor = json_to_actor(&input_string); assert_eq!(actor.object_type,ObjectType::Person); @@ -63,5 +93,13 @@ mod tests { assert_eq!(actor.outbox,"https://mastodon.social/users/crepels/outbox".to_string()); assert_eq!(actor.summary,Some("

I created a systemd playground to help people learn systemd.

".to_string())); assert_eq!(actor.icon,Some(Icon{media_type: "Image".to_string(), media_mime_type: "image/png".to_string(), url: "https://files.mastodon.social/accounts/avatars/108/227/485/389/961/502/original/1213d525278ae01d.png".to_string()})) + + } + + #[test] + fn detect_activity_type() { + let input_string = read_to_string("tests/test-json-person.json").unwrap(); + let result = parse_activity(&input_string).unwrap(); + assert!(matches!(result, Object::Person( .. ))); } }