use serde::{Serialize, Deserialize};
use core::fmt;
use core::ops::Deref;
pub mod results;
pub mod typed;
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct VndbError {
pub id: String,
pub msg: String,
}
impl VndbError {
pub fn from_str(error: &str) -> serde_json::Result<Self> {
serde_json::from_str(error)
}
}
impl fmt::Display for VndbError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error(id='{}')={}", self.id, self.msg)
}
}
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct DBstats {
pub tags: u64,
pub releases: u64,
pub producers: u64,
pub chars: u64,
pub vn: u64,
pub traits: u64
}
#[derive(Clone, Debug)]
pub struct Results {
inner: serde_json::Value
}
impl Results {
pub fn from_str(results: &str) -> serde_json::Result<Self> {
Ok(Self {
inner: serde_json::from_str(results)?
})
}
#[inline(always)]
fn to<'de, T: Deserialize<'de>>(&'de self) -> serde_json::Result<T> {
T::deserialize(&self.inner)
}
#[inline]
pub fn vn(&self) -> serde_json::Result<typed::VN> {
self.to()
}
#[inline]
pub fn release(&self) -> serde_json::Result<typed::Release> {
self.to()
}
#[inline]
pub fn producer(&self) -> serde_json::Result<typed::Producer> {
self.to()
}
#[inline]
pub fn character(&self) -> serde_json::Result<typed::Character> {
self.to()
}
#[inline]
pub fn user(&self) -> serde_json::Result<typed::User> {
self.to()
}
#[inline]
pub fn vote_list(&self) -> serde_json::Result<typed::VoteList> {
self.to()
}
#[inline]
pub fn vn_list(&self) -> serde_json::Result<typed::VnList> {
self.to()
}
#[inline]
pub fn u_list(&self) -> serde_json::Result<typed::UList> {
self.to()
}
}
impl Deref for Results {
type Target = serde_json::Value;
fn deref(&self) -> &Self::Target {
&self.inner
}
}