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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! # Entities

use bytes::Bytes;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use ssh_key::PublicKey;
use std::time::Duration;
use strum::EnumString;
#[cfg(feature = "proptest")]
use test_strategy::Arbitrary;
use url::Url;
use uuid::Uuid;

/// Target triple, such as `x86_64-unknown-linux-gnu`.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "proptest", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TargetTriple(String);

impl TargetTriple {
    /// Architecture
    pub fn arch(&self) -> &str {
        self.0.split('-').nth(0).unwrap()
    }

    /// Vendor, might be `unknown`.
    pub fn vendor(&self) -> &str {
        self.0.split('-').nth(1).unwrap()
    }

    /// System (usually represents the kernel type)
    pub fn system(&self) -> &str {
        self.0.split('-').nth(2).unwrap()
    }

    /// ABI, if there are multiple.
    pub fn abi(&self) -> Option<&str> {
        self.0.split('-').nth(3)
    }
}

/// Represents a crate name
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "proptest", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CrateName(String);

/// Represents a crate version
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
//#[cfg_attr(feature = "proptest", derive(Arbitrary))]
//#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CrateVersion {
    krate: CrateName,
    version: semver::Version,
}

/// Kind of artifact.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, EnumString)]
#[cfg_attr(feature = "proptest", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[strum(serialize_all = "snake_case")]
pub enum ArtifactKind {
    /// Manifest, with a `.json` extension.
    Metadata,
    /// Tarball, with a `.tar.gz` extension.
    Tarball,
    /// Debian package, with a `.deb` extension.
    Debian,
    /// Coverage report, with a `.deb` extension.
    Coverage,
}

impl ArtifactKind {
    /// Get extension for this artifact kind.
    pub fn extension(&self) -> &'static str {
        match self {
            Self::Metadata => "metadata.json",
            Self::Tarball => "tar.gz",
            Self::Debian => "deb",
            Self::Coverage => "coverage.json",
        }
    }
}

/// Artifact identifier.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "proptest", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ArtifactId {
    /// Task that produced this artifact
    pub task: Task,
}

impl ArtifactId {
    /// Get the file name for this artifact identifier
    pub fn file_name(&self) -> String {
        self.task.file_name()
    }
}

/// Data of artifact
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ArtifactData {
    /// Raw data
    Data {
        /// Bytes
        bytes: Bytes,
    },
    /// Redirect
    Redirect {
        /// How long this link is valid for
        validity: Duration,
        /// URL to redirect to
        url: Url,
    },
}

impl ArtifactData {
    /// Get raw bytes, if exists
    pub fn bytes(&self) -> Option<&Bytes> {
        match self {
            Self::Data { bytes } => Some(bytes),
            Self::Redirect { .. } => None,
        }
    }
}

/// Builder that is registered in the database
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Builder {
    /// UUID of this builder
    pub uuid: Uuid,
    /// Public key this builder uses to sign messages and artifacts
    pub public_key: PublicKey,
    /// Comment
    pub comment: String,
    /// Enabled state
    pub enabled: bool,
}

/// Target that can be built
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TargetInfo {
    /// Name of this target
    pub name: String,
    /// Enabled status
    pub enabled: bool,
}

/// Crate
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CrateInfo {
    /// Name of this crate
    pub name: String,
    /// Is crate enabled
    pub enabled: bool,
}

/// Crate version
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct VersionInfo {
    /// Name of this crate
    pub name: String,
    /// Version
    pub version: String,
    /// Checksum
    pub checksum: String,
    /// Yanked status
    pub yanked: bool,
}

/// Job
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct JobInfo {
    /// Job UUID
    pub uuid: Uuid,
    /// Builder processing this job
    pub builder: Uuid,
    /// Name of the crate being built
    pub name: String,
    /// Version of the crate being built
    pub version: String,
    /// Triple being built
    pub triple: Option<String>,
    /// Download URL for crate
    pub url: Url,
}

/// Task
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "proptest", derive(Arbitrary))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Task {
    /// Crate being built
    #[cfg_attr(feature = "proptest", strategy("[a-z]{20}"))]
    pub krate: String,
    /// Version being built
    #[cfg_attr(feature = "proptest", strategy("[a-z]{20}"))]
    pub version: String,
    /// Triple being built
    #[cfg_attr(feature = "proptest", strategy(proptest::option::of("[a-z]{1,20}")))]
    pub triple: Option<String>,
    /// Kind of artifact to build
    pub kind: ArtifactKind,
    /// Priority for this task
    pub priority: i32,
    /// Variant to build
    #[cfg_attr(feature = "proptest", strategy(proptest::option::of("[a-z]{1,20}")))]
    pub variant: Option<String>,
}

impl Task {
    /// Get the file name for this artifact identifier
    pub fn file_name(&self) -> String {
        let Self {
            krate,
            version,
            kind,
            ..
        } = &self;
        let mut name = format!("{krate}_{version}");
        if let Some(triple) = &self.triple {
            name.push('_');
            name.push_str(triple);
        }
        if let Some(variant) = &self.variant {
            name.push('_');
            name.push_str(variant);
        }
        name.push('.');
        name.push_str(kind.extension());
        name
    }
}

#[cfg(all(test, feature = "proptest"))]
mod tests {
    use super::*;
    use test_strategy::proptest;

    #[proptest]
    fn task_path_different(left: Task, right: Task) {
        if left != right {
            assert!(left.file_name() != right.file_name());
        }
    }
}