use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub enum JsonResult<A> {
    Ok(A),
    Error(JsonError),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JsonError {
    reason: String,
}

impl<A> JsonResult<A> {
    pub fn ok(data: A) -> Json<JsonResult<A>> {
        Json(JsonResult::Ok(data))
    }

    pub fn err(reason: String) -> Json<JsonResult<A>> {
        Json(JsonResult::Error(JsonError { reason }))
    }
}
