Compare commits

...

2 commits

Author SHA1 Message Date
a2ef2a2b31
Handle activity types 2024-02-11 14:51:41 +01:00
dbb677eb95
Move test files to own dir 2024-02-11 14:50:56 +01:00
3 changed files with 47 additions and 9 deletions

16
Cargo.lock generated
View file

@ -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",
]

View file

@ -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<Object,String> {
let json_input: Result<Activity,_> = 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("<p>I created a systemd playground to help people learn systemd.</p>".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( .. )));
}
}