gog-sync/src/models.rs

132 lines
3.0 KiB
Rust
Raw Normal View History

2017-03-20 12:14:49 +00:00
use chrono::UTC;
use std::fmt;
use std::hash::{Hash, Hasher};
2017-03-20 15:20:37 +00:00
use std::collections::HashMap;
2017-03-20 12:14:49 +00:00
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()
}
2017-03-20 15:20:37 +00:00
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
2017-03-20 15:20:37 +00:00
pub struct Config {
pub storage: String,
2017-03-21 21:28:03 +00:00
pub movie_storage: String,
2017-03-20 15:20:37 +00:00
#[serde(default = "default_map")]
pub games: HashMap<String, u64>,
#[serde(default = "default_map")]
pub installers: HashMap<String, u64>,
#[serde(default = "default_map")]
pub extras: HashMap<String, u64>,
#[serde(default = "default_list")]
pub os_filters: Vec<String>,
#[serde(default = "default_list")]
pub language_filters: Vec<String>,
2017-03-20 15:20:37 +00:00
}
fn default_map() -> HashMap<String, u64> {
HashMap::new()
}
fn default_list() -> Vec<String> {
Vec::new()
}
impl Config {
pub fn new() -> Config {
Config {
storage: String::from("."),
2017-03-21 21:28:03 +00:00
movie_storage: String::from("."),
games: HashMap::new(),
installers: HashMap::new(),
extras: HashMap::new(),
os_filters: Vec::new(),
language_filters: Vec::new(),
}
}
}
2017-03-20 12:14:49 +00:00
#[derive(Hash)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Game {
pub title: String,
pub cd_key: String,
#[serde(skip_deserializing)]
2017-03-21 21:28:03 +00:00
pub is_movie: bool,
#[serde(skip_deserializing)]
2017-03-20 12:14:49 +00:00
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()
}