tetratto-core 15.0.2

The core behind Tetratto
Documentation
use serde::{Serialize, Deserialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ActionData {
    String(String),
    Int32(i32),
    Usize(usize),
    Many(Vec<ActionData>),
    Null,
}

impl ActionData {
    pub fn read_string(self) -> String {
        match self {
            ActionData::String(x) => x,
            _ => String::default(),
        }
    }

    pub fn read_int32(self) -> i32 {
        match self {
            ActionData::Int32(x) => x,
            _ => i32::default(),
        }
    }

    pub fn read_usize(self) -> usize {
        match self {
            ActionData::Usize(x) => x,
            _ => usize::default(),
        }
    }

    pub fn read_many(self) -> Vec<ActionData> {
        match self {
            ActionData::Many(x) => x,
            _ => Vec::default(),
        }
    }
}

impl Default for ActionData {
    fn default() -> Self {
        Self::Null
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ActionType {
    /// A request to join a community.
    ///
    /// `users` table.
    CommunityJoin,
    /// A request to answer a question with a post.
    ///
    /// `questions` table.
    Answer,
    /// A request follow a private account.
    ///
    /// `users` table.
    Follow,
    /// A request for the `owner` user (sender) to send the `linked_asset` user (receiver) coins.
    ///
    /// Expects a `data` value of [`ActionData::Int32`] representing the coin amount.
    Transfer,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ActionRequest {
    pub id: usize,
    pub created: usize,
    pub owner: usize,
    pub action_type: ActionType,
    /// The ID of the asset this request links to. Should exist in the correct
    /// table for the given [`ActionType`].
    pub linked_asset: usize,
    /// Optional data attached to the action request.
    pub data: ActionData,
}

impl ActionRequest {
    /// Create a new [`ActionRequest`].
    pub fn new(
        owner: usize,
        action_type: ActionType,
        linked_asset: usize,
        data: Option<ActionData>,
    ) -> Self {
        Self {
            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
            created: unix_epoch_timestamp(),
            owner,
            action_type,
            linked_asset,
            data: match data {
                Some(x) => x,
                None => ActionData::default(),
            },
        }
    }

    /// Create a new [`ActionRequest`] with the given `id`.
    pub fn with_id(
        id: usize,
        owner: usize,
        action_type: ActionType,
        linked_asset: usize,
        data: Option<ActionData>,
    ) -> Self {
        Self {
            id,
            created: unix_epoch_timestamp(),
            owner,
            action_type,
            linked_asset,
            data: match data {
                Some(x) => x,
                None => ActionData::default(),
            },
        }
    }
}