Format
This commit is contained in:
parent
a2ef2a2b31
commit
179264ee90
|
@ -1,7 +1,7 @@
|
||||||
use serde_json;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize,Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Actor {
|
pub struct Actor {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub object_type: ObjectType,
|
pub object_type: ObjectType,
|
||||||
|
@ -16,16 +16,15 @@ pub struct Actor {
|
||||||
pub icon: Option<Icon>, // Link to pfp
|
pub icon: Option<Icon>, // Link to pfp
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize,Debug,PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
pub struct Icon {
|
pub struct Icon {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
media_type: String,
|
media_type: String,
|
||||||
#[serde(rename = "mediaType")]
|
#[serde(rename = "mediaType")]
|
||||||
media_mime_type: String,
|
media_mime_type: String,
|
||||||
url: String,
|
url: String,
|
||||||
|
|
||||||
}
|
}
|
||||||
#[derive(Serialize, Deserialize,Debug,PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
pub enum ObjectType {
|
pub enum ObjectType {
|
||||||
Person,
|
Person,
|
||||||
Like,
|
Like,
|
||||||
|
@ -33,7 +32,7 @@ pub enum ObjectType {
|
||||||
Tombstone,
|
Tombstone,
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for ObjectType {
|
impl std::fmt::Display for ObjectType {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ObjectType::Person => write!(f, "Person"),
|
ObjectType::Person => write!(f, "Person"),
|
||||||
ObjectType::Like => write!(f, "Like"),
|
ObjectType::Like => write!(f, "Like"),
|
||||||
|
@ -50,23 +49,30 @@ pub enum Object {
|
||||||
Tombstone,
|
Tombstone,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize,Debug,PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
pub struct Activity{
|
pub struct Activity {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub activity_type: ObjectType,
|
pub activity_type: ObjectType,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn json_to_actor(input: &String)->Actor {
|
pub fn json_to_actor(input: &String) -> Actor {
|
||||||
let return_data: Actor = serde_json::from_str(&input).unwrap();
|
let return_data: Actor = serde_json::from_str(&input).unwrap();
|
||||||
return_data
|
return_data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_activity(input: &String)->Result<Object,String> {
|
pub fn parse_activity(input: &String) -> Result<Object, String> {
|
||||||
let json_input: Result<Activity,_> = serde_json::from_str(input);
|
let json_input: Result<Activity, _> = serde_json::from_str(input);
|
||||||
if json_input.is_ok() {
|
if json_input.is_ok() {
|
||||||
match json_input.as_ref().unwrap().activity_type {
|
match json_input.as_ref().unwrap().activity_type {
|
||||||
ObjectType::Person => { return Ok(Object::Person(json_to_actor(input)));},
|
ObjectType::Person => {
|
||||||
_ => { return Err(format!("Activity type {} not implemented",json_input.unwrap().activity_type)); }
|
return Ok(Object::Person(json_to_actor(input)));
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(format!(
|
||||||
|
"Activity type {} not implemented",
|
||||||
|
json_input.unwrap().activity_type
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err("Failed to parse json data".to_string())
|
Err("Failed to parse json data".to_string())
|
||||||
|
@ -84,22 +90,36 @@ mod tests {
|
||||||
let input_string = read_to_string("tests/test-json-person.json").unwrap();
|
let input_string = read_to_string("tests/test-json-person.json").unwrap();
|
||||||
let actor: Actor = json_to_actor(&input_string);
|
let actor: Actor = json_to_actor(&input_string);
|
||||||
|
|
||||||
assert_eq!(actor.object_type,ObjectType::Person);
|
assert_eq!(actor.object_type, ObjectType::Person);
|
||||||
assert_eq!(actor.name,"Sebastian Jambor".to_string());
|
assert_eq!(actor.name, "Sebastian Jambor".to_string());
|
||||||
assert_eq!(actor.preferred_username,"crepels");
|
assert_eq!(actor.preferred_username, "crepels");
|
||||||
assert_eq!(actor.following,"https://mastodon.social/users/crepels/following".to_string());
|
assert_eq!(
|
||||||
assert_eq!(actor.followers,"https://mastodon.social/users/crepels/followers".to_string());
|
actor.following,
|
||||||
assert_eq!(actor.inbox,"https://mastodon.social/users/crepels/inbox".to_string());
|
"https://mastodon.social/users/crepels/following".to_string()
|
||||||
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.followers,
|
||||||
|
"https://mastodon.social/users/crepels/followers".to_string()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
actor.inbox,
|
||||||
|
"https://mastodon.social/users/crepels/inbox".to_string()
|
||||||
|
);
|
||||||
|
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()}))
|
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]
|
#[test]
|
||||||
fn detect_activity_type() {
|
fn detect_activity_type() {
|
||||||
let input_string = read_to_string("tests/test-json-person.json").unwrap();
|
let input_string = read_to_string("tests/test-json-person.json").unwrap();
|
||||||
let result = parse_activity(&input_string).unwrap();
|
let result = parse_activity(&input_string).unwrap();
|
||||||
assert!(matches!(result, Object::Person( .. )));
|
assert!(matches!(result, Object::Person(..)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue