use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FnResult {
    error: Option<String>,
    result: Option<String>,
}

impl FnResult {
    pub fn fail(message: impl Into<String>) -> Self {
        FnResult {
            result: None,
            error: Some(message.into()),
        }
    }

    pub fn success(result: impl Into<String>) -> Self {
        FnResult {
            result: Some(result.into()),
            error: None,
        }
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(&self).expect("JSResult to json error")
    }
}
