shared/domain/
animation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Types for animations.

use crate::api::endpoints::PathPart;

use super::{meta::AnimationStyleId, Publish};
use chrono::{DateTime, Utc};
use macros::make_path_parts;
use serde::{Deserialize, Serialize};
#[cfg(feature = "backend")]
use sqlx::postgres::PgRow;

/// Animation Kinds
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "backend", derive(sqlx::Type))]
#[repr(i16)]
pub enum AnimationKind {
    /// Gif Animation
    Gif = 0,
    /// Spritesheet Animation
    Spritesheet = 1,
}

impl AnimationKind {
    /// returns `self` in a string representation.
    #[must_use]
    pub const fn to_str(self) -> &'static str {
        match self {
            Self::Gif => "gif",
            Self::Spritesheet => "spritesheet",
        }
    }
}

wrap_uuid! {
    /// Wrapper type around [`Uuid`], represents the ID of an animation.
    pub struct AnimationId
}

make_path_parts!(AnimationGetPath => "/v1/animation/{}" => AnimationId);

/// Response for getting a single animation file.
#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationResponse {
    /// The animation's metadata.
    pub metadata: AnimationMetadata,
}

/// Over the wire representation of an animation's metadata.
#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationMetadata {
    /// The animation's ID.
    pub id: AnimationId,

    /// The name of the animation.
    pub name: String,

    /// The description of the animation.
    pub description: String,

    /// Is the animation premium?
    pub is_premium: bool,

    /// When the animation should be considered published (if at all).
    pub publish_at: Option<DateTime<Utc>>,

    /// The styles associated with the animation.
    pub styles: Vec<AnimationStyleId>,

    /// What kind of animation this is.
    pub kind: AnimationKind,

    /// Should the animation loop?
    pub is_looping: bool,

    /// When the animation was originally created.
    pub created_at: DateTime<Utc>,

    /// When the animation was last updated.
    pub updated_at: Option<DateTime<Utc>>,
}

// HACK: can't get `Vec<_>` directly from the DB. See `[crate::domain::image::ImageMetadata]`
#[cfg(feature = "backend")]
impl<'r> sqlx::FromRow<'r, PgRow> for AnimationMetadata {
    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
        let DbAnimation {
            id,
            kind,
            name,
            description,
            is_premium,
            publish_at,
            styles,
            is_looping,
            created_at,
            updated_at,
        } = DbAnimation::from_row(row)?;

        Ok(Self {
            id,
            kind,
            name,
            description,
            is_premium,
            publish_at,
            styles: styles.into_iter().map(|(it,)| it).collect(),
            is_looping,
            created_at,
            updated_at,
        })
    }
}

#[cfg_attr(feature = "backend", derive(sqlx::FromRow))]
#[cfg(feature = "backend")]
struct DbAnimation {
    pub id: AnimationId,
    pub kind: AnimationKind,
    pub name: String,
    pub description: String,
    pub is_premium: bool,
    pub publish_at: Option<DateTime<Utc>>,
    pub styles: Vec<(AnimationStyleId,)>,
    pub is_looping: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: Option<DateTime<Utc>>,
}

make_path_parts!(AnimationCreatePath => "/v1/animation");

// todo: # errors doc section
/// Request to create a new animation.
#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationCreateRequest {
    /// The name of the animation.
    pub name: String,

    /// The description of the animation.
    pub description: String,

    /// Is the animation premium?
    pub is_premium: bool,

    /// When to publish the animation.
    ///
    /// If [`Some`] publish the animation according to the `Publish`. Otherwise, don't publish it.
    pub publish_at: Option<Publish>,

    /// The styles associated with the animation.
    pub styles: Vec<AnimationStyleId>,

    /// What kind of animation this is.
    pub kind: AnimationKind,

    /// Should the animation loop?
    pub is_looping: bool,
}

make_path_parts!(AnimationUploadPath => "/v1/animation/{}/raw" => AnimationId);

/// Request to indicate the size of an user library image for upload.
#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationUploadRequest {
    /// The size of the image to be uploaded in bytes.
    pub file_size: usize,
}

/// URL to upload an user library image, supports resumable uploading.
#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationUploadResponse {
    /// The session URI used for uploading, including the query for uploader ID
    pub session_uri: String,
}

make_path_parts!(AnimationDeletePath => "/v1/animation/{}" => AnimationId);