Handle errors better
This commit is contained in:
parent
179264ee90
commit
e912277605
1 changed files with 11 additions and 7 deletions
|
@ -55,18 +55,22 @@ pub struct Activity {
|
|||
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 json_to_actor(input: &String) -> Result<Actor, serde_json::Error> {
|
||||
let actor_result: Result<Actor, serde_json::Error> = serde_json::from_str(&input);
|
||||
match actor_result {
|
||||
Ok(successful_actor) => Ok(successful_actor),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
ObjectType::Person => match json_to_actor(input) {
|
||||
Ok(successful_actor) => Ok(Object::Person(successful_actor)),
|
||||
Err(error) => Err(format!("Error while trying to parse actor {}", error)),
|
||||
},
|
||||
_ => {
|
||||
return Err(format!(
|
||||
"Activity type {} not implemented",
|
||||
|
@ -88,7 +92,7 @@ mod tests {
|
|||
#[test]
|
||||
fn parse_json_into_actor() {
|
||||
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).unwrap();
|
||||
|
||||
assert_eq!(actor.object_type, ObjectType::Person);
|
||||
assert_eq!(actor.name, "Sebastian Jambor".to_string());
|
||||
|
|
Loading…
Reference in a new issue