dalloriam-cloud-protocol 0.3.1

protocol library for personal cloud
Documentation
use serde::{Deserialize, Serialize};

#[cfg(feature = "sqlx")]
use sqlx::{FromRow, Type};

use time::OffsetDateTime;

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "sqlx", derive(Type))]
#[cfg_attr(feature = "sqlx", sqlx(rename_all = "lowercase"))]
pub enum GameStatus {
    /// A game that I want to play, but didn't buy yet.
    Wishlist,

    /// A game that I bought, but didn't get around to.
    Backlog,

    /// A game that I intend to play very soon.
    UpNext,

    /// A game that I am currently playing through.
    Playing,

    /// A game that I "finished" (I reached my objectives in the game), but that I still play
    /// from time to time.
    RegularRotation,

    /// A game that I finished, and don't play anymore.
    Finished,

    /// A game that I didn't finish and don't play anymore.
    Abandoned,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct CreateGameRequest {
    /// The IGDB ID of this game.
    pub igdb_id: String,

    /// The status of this game.
    pub status: GameStatus,

    /// The rating of this game.
    ///
    /// This is typically only set when the game is [RegularRotation, Finished, Abandoned].
    pub rating: Option<i16>,

    /// The moment at which the game was finished, if it was finished.
    pub finished_at: Option<OffsetDateTime>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ListGamesResponse {
    pub games: Vec<GameResult>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct GameInfo {
    pub igdb_id: u64,
    pub name: String,
    pub rating: Option<f64>,
    pub slug: Option<String>,
    pub cover: Option<String>,
    pub genres: Option<Vec<String>>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "sqlx", derive(FromRow))]
pub struct Game {
    /// The IGDB ID of this game.
    pub igdb_id: String,

    /// The status of this game.
    pub status: GameStatus,

    /// The rating of this game.
    ///
    /// This is typically only set when the game is [RegularRotation, Finished, Abandoned].
    pub rating: Option<i16>,

    /// The ID of the user this game belongs to.
    #[serde(skip_serializing)]
    pub user_email: String,

    /// The moment at which the game was finished, if it was finished.
    pub finished_at: Option<OffsetDateTime>,
}

impl Game {
    pub fn from_request(req: CreateGameRequest, user_id: String) -> Self {
        Self {
            igdb_id: req.igdb_id,
            status: req.status,
            rating: req.rating,
            finished_at: req.finished_at,
            user_email: user_id,
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct GameResult {
    pub status: GameStatus,
    pub rating: Option<i16>,
    pub finished_at: Option<OffsetDateTime>,
    pub info: GameInfo,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SearchGamesResponse {
    pub games: Vec<GameInfo>,
}