use serde_json;
use std::fmt;
#[derive(Debug, PartialEq)]
pub enum QueryResult {
Boolean(bool),
JSON(serde_json::Value),
Result(Result<(), String>),
U64(u64),
}
pub fn get_boolean_from_query_result(result: QueryResult) -> bool {
match result {
QueryResult::Boolean(b) => b,
_ => {
eprintln!("Expected QueryResult::Boolean");
false
}
}
}
pub fn get_json_from_query_result(result: QueryResult) -> serde_json::Value {
match result {
QueryResult::JSON(json) => json,
_ => {
eprintln!("Expected QueryResult::JSON");
serde_json::Value::Null
}
}
}
pub fn get_result_from_query_result(result: QueryResult) -> Result<(), String> {
match result {
QueryResult::Result(res) => res,
_ => {
eprintln!("Expected QueryResult::Result");
Err("Expected QueryResult::Result".to_string())
}
}
}
pub fn get_u64_from_query_result(result: QueryResult) -> u64 {
match result {
QueryResult::U64(i) => i,
_ => {
eprintln!("Expected QueryResult::U64");
0
}
}
}
impl fmt::Display for QueryResult {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
QueryResult::Boolean(b) => write!(f, "{}", b),
QueryResult::JSON(json) => write!(f, "{}", json),
QueryResult::Result(res) => match res {
Ok(_) => write!(f, "Success"),
Err(e) => write!(f, "Error: {}", e),
},
QueryResult::U64(i) => write!(f, "{}", i),
}
}
}