use serde_json::Value;

pub mod game;
pub mod log;
pub mod manager;
pub mod room;

pub fn get_i32(row: &Value, key: &str) -> Result<i32, String> {
    row.get(key)
        .and_then(|value| value.as_i64())
        .and_then(|value| i32::try_from(value).ok())
        .ok_or_else(|| format!("{key}字段格式错误"))
}

pub fn get_string(row: &Value, key: &str) -> Result<String, String> {
    row.get(key)
        .and_then(|value| value.as_str())
        .map(|value| value.to_string())
        .ok_or_else(|| format!("{key}字段格式错误"))
}
