90 lines
1.9 KiB
Rust
90 lines
1.9 KiB
Rust
![]() |
use chrono::UTC;
|
||
|
use std::fmt;
|
||
|
use std::hash::{Hash, Hasher};
|
||
|
use std::collections::hash_map::DefaultHasher;
|
||
|
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
pub struct Token {
|
||
|
pub access_token: String,
|
||
|
pub expires_in: u16,
|
||
|
pub token_type: String,
|
||
|
pub scope: String,
|
||
|
pub session_id: String,
|
||
|
pub refresh_token: String,
|
||
|
pub user_id: String,
|
||
|
#[serde(default = "timestamp")]
|
||
|
pub timestamp: i64,
|
||
|
}
|
||
|
|
||
|
impl Token {
|
||
|
pub fn is_expired(&self) -> bool {
|
||
|
let now = UTC::now().timestamp();
|
||
|
|
||
|
now > self.timestamp + self.expires_in as i64
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn timestamp() -> i64 {
|
||
|
UTC::now().timestamp()
|
||
|
}
|
||
|
|
||
|
#[derive(Hash)]
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
#[serde(rename_all = "camelCase")]
|
||
|
pub struct Game {
|
||
|
pub title: String,
|
||
|
pub cd_key: String,
|
||
|
#[serde(skip_deserializing)]
|
||
|
pub installers: Vec<Installer>,
|
||
|
pub extras: Vec<Extra>,
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Game {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
write!(f, "({})", self.title)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Hash)]
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
#[serde(rename_all = "camelCase")]
|
||
|
pub struct Installer {
|
||
|
pub manual_url: String,
|
||
|
pub version: String,
|
||
|
pub os: String,
|
||
|
pub language: String,
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Installer {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
write!(f,
|
||
|
"({}, {}, {}, {})",
|
||
|
self.manual_url,
|
||
|
self.version,
|
||
|
self.os,
|
||
|
self.language)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Hash)]
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
#[serde(rename_all = "camelCase")]
|
||
|
pub struct Extra {
|
||
|
pub manual_url: String,
|
||
|
pub name: String,
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Extra {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
write!(f, "({}, {})", self.manual_url, self.name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn get_hash<T>(obj: &T) -> u64
|
||
|
where T: Hash
|
||
|
{
|
||
|
let mut s = DefaultHasher::new();
|
||
|
obj.hash(&mut s);
|
||
|
s.finish()
|
||
|
}
|