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
176
177
178
179
180
181
182
183
184
185
186
187
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;

use crate::{
    error::{Error, Result},
    resources::user::Username,
};

lazy_static! {
    static ref FULL_NAME_REGEX: Regex =
        Regex::new("^[A-Za-z0-9-_]{1,256}/[A-Za-z0-9-_]{1,256}$").unwrap();
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Bucket {
    pub id: Id,
    pub name: Name,
    pub owner: Username,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    #[serde(default)]
    pub transform_tag: Option<TransformTag>,
}

impl Bucket {
    pub fn full_name(&self) -> FullName {
        FullName(format!("{}/{}", self.owner.0, self.name.0))
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Name(pub String);

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FullName(pub String);

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct Id(pub String);

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModelFamily(pub String);

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TransformTag(pub String);

impl FromStr for TransformTag {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self> {
        Ok(Self(string.to_owned()))
    }
}

// TODO(mcobzarenco)[3963]: Make `Identifier` into a trait (ensure it still implements
// `FromStr` so we can take T: Identifier as a clap command line argument).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Identifier {
    Id(Id),
    FullName(FullName),
}

impl From<FullName> for Identifier {
    fn from(full_name: FullName) -> Self {
        Identifier::FullName(full_name)
    }
}

impl From<Id> for Identifier {
    fn from(id: Id) -> Self {
        Identifier::Id(id)
    }
}

impl FromStr for Identifier {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self> {
        if string.chars().all(|c| c.is_digit(16)) {
            Ok(Identifier::Id(Id(string.into())))
        } else if FULL_NAME_REGEX.is_match(string) {
            Ok(Identifier::FullName(FullName(string.into())))
        } else {
            Err(Error::BadBucketIdentifier {
                identifier: string.into(),
            })
        }
    }
}

impl Display for FullName {
    fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
        write!(formatter, "{}", self.0)
    }
}

impl Display for Id {
    fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
        write!(formatter, "{}", self.0)
    }
}

impl Display for Identifier {
    fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
        match *self {
            Identifier::Id(ref id) => Display::fmt(id, formatter),
            Identifier::FullName(ref full_name) => Display::fmt(full_name, formatter),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct NewBucket<'request> {
    pub bucket_type: BucketType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<&'request str>,
    pub transform_tag: &'request TransformTag,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct CreateRequest<'request> {
    pub bucket: NewBucket<'request>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct CreateResponse {
    pub bucket: Bucket,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct GetAvailableResponse {
    pub buckets: Vec<Bucket>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct GetResponse {
    pub bucket: Bucket,
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum BucketType {
    #[serde(rename = "emails")]
    Emails,
}

impl FromStr for BucketType {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self> {
        match string {
            "emails" => Ok(Self::Emails),
            _ => Err(Error::BadBucketType {
                bucket_type: string.into(),
            }),
        }
    }
}

impl Default for BucketType {
    fn default() -> Self {
        Self::Emails
    }
}

impl Display for BucketType {
    fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
        match *self {
            Self::Emails => write!(formatter, "emails"),
        }
    }
}

impl FromStr for FullName {
    type Err = Error;
    fn from_str(string: &str) -> Result<Self> {
        if FULL_NAME_REGEX.is_match(string) {
            Ok(FullName(string.into()))
        } else {
            Err(Error::BadBucketName {
                name: string.into(),
            })
        }
    }
}