eeg_certs/backend/src/json_result.rs
2023-11-21 17:04:13 -05:00

26 lines
628 B
Rust

use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
/// Represents a JSON response that can be either an error or a success
/// Is used as a helper to return JSON responses from the backend
#[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 }))
}
}