openlegends-server 0.3.0

OpenLegends Game Server
Documentation
pub mod error;
pub use error::Error;

use serde::{Deserialize, Serialize};

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

impl Default for Response {
    fn default() -> Self {
        Self::new()
    }
}

impl Response {
    pub fn new() -> Self {
        Self {
            version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }

    pub fn from_json(json: &str) -> Result<Self, Error> {
        match serde_json::from_str::<Response>(json) {
            Ok(data) => Ok(Self {
                version: data.version,
            }),
            Err(e) => Err(Error::Json(e)),
        }
    }

    pub fn to_json(&self) -> Result<String, Error> {
        match serde_json::to_string(&self) {
            Ok(json) => Ok(json),
            Err(e) => Err(Error::Json(e)),
        }
    }

    pub fn version(&self) -> &str {
        &self.version
    }
}