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
use super::DependencyKind;
use chrono::{DateTime, Utc};
use semver::{Version, VersionReq};
#[cfg(feature = "client")]
use serde::Deserialize;
#[cfg(feature = "server")]
use serde::Serialize;
use std::collections::HashMap;

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct RegistryConfig {
    pub dl: String,
    pub api: String,
}

#[derive(Debug)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct CompletedPublication {
    /// Optional object of warnings to display to the user.
    pub warnings: Option<CompletedPublicationWarnings>,
}

#[derive(Debug)]
#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct CompletedPublicationWarnings {
    /// Array of strings of categories that are invalid and ignored.
    pub invalid_categories: Vec<String>,
    /// Array of strings of badge names that are invalid and ignored.
    pub invalid_badges: Vec<String>,
    /// Array of strings of arbitrary warnings to display to the user.
    pub other: Vec<String>,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
#[derive(Clone, Eq, PartialEq)]
pub struct CrateVersion {
    /// The name of the package.
    ///
    /// This must only contain alphanumeric, `-`, or `_` characters.
    pub name: String,
    /// The version of the package this row is describing.
    ///
    /// This must be a valid version number according to the Semantic Versioning 2.0.0 spec at
    /// https://semver.org/.
    pub vers: Version,
    /// Array of direct dependencies of the package.
    pub deps: Vec<Dependency>,
    /// A SHA256 checksum of the `.crate` file.
    pub cksum: String,
    /// Set of features defined for the package.
    ///
    /// Each feature maps to an array of features or dependencies it enables.
    pub features: HashMap<String, Vec<String>>,
    /// Boolean of whether or not this version has been yanked.
    pub yanked: bool,
    /// The `links` string value from the package's manifest, or null if not specified.
    ///
    /// This field is optional and defaults to null.
    pub links: Option<String>,
    /// An unsigned 32-bit integer value indicating the schema version of this entry.
    ///
    /// If this not specified, it should be interpreted as the default of 1.
    ///
    /// Cargo (starting with version 1.51) will ignore versions it does not recognize.
    /// This provides a method to safely introduce changes to index entries and allow older
    /// versions of cargo to ignore newer entries it doesn't understand. Versions older than 1.51
    /// ignore this field, and thus may misinterpret the meaning of the index entry.
    ///
    /// The current values are:
    ///
    /// * 1: The schema as documented here, not including newer additions.
    ///      This is honored in Rust version 1.51 and newer.
    /// * 2: The addition of the `features2` field.
    ///      This is honored in Rust version 1.60 and newer.
    #[cfg_attr(feature = "client", serde(default = "default_v"))]
    pub v: u32,
    /// This optional field contains features with new, extended syntax.
    ///
    /// Specifically, namespaced features (`dep:`) and weak dependencies (`pkg?/feat`).
    ///
    /// This is separated from `features` because versions older than 1.19 will fail to load due to
    /// not being able to parse the new syntax, even with a `Cargo.lock` file.
    ///
    /// Cargo will merge any values listed here with the "features" field.
    ///
    /// If this field is included, the "v" field should be set to at least 2.
    ///
    /// Registries are not required to use this field for extended feature syntax, they are allowed
    /// to include those in the "features" field. Using this is only necessary if the registry
    /// wants to support cargo versions older than 1.19, which in practice is only crates.io since
    /// those older versions do not support other registries.
    #[cfg_attr(feature = "client", serde(default))]
    pub features2: HashMap<String, Vec<String>>,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
#[derive(Clone, Eq, PartialEq)]
pub struct Dependency {
    /// Name of the dependency.
    ///
    /// If the dependency is renamed from the original package name, this is the new name.
    /// The original package name is stored in the `package` field.
    pub name: String,
    /// The SemVer requirement for this dependency.
    ///
    /// This must be a valid version requirement defined at
    /// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html.
    pub req: VersionReq,
    /// Array of features (as strings) enabled for this dependency.
    pub features: Vec<String>,
    /// Boolean of whether or not this is an optional dependency.
    pub optional: bool,
    /// Boolean of whether or not default features are enabled.
    pub default_features: bool,
    /// The target platform for the dependency.
    ///
    /// null if not a target dependency. Otherwise, a string such as "cfg(windows)".
    pub target: Option<String>,
    /// The dependency kind.
    ///
    /// "dev", "build", or "normal".
    /// Note: this is a required field, but a small number of entries exist in the crates.io index
    /// with either a missing or null kind` field due to implementation bugs.
    pub kind: DependencyKind,
    /// The URL of the index of the registry where this dependency is from as a string.
    ///
    /// If not specified or null, it is assumed the dependency is in the current registry.
    pub registry: Option<String>,
    /// If the dependency is renamed, this is a string of the actual package name.
    ///
    /// If not specified or null, this dependency is not
    /// renamed.
    pub package: Option<String>,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct ListAll {
    pub results: Vec<ListAllCrateEntry>,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct ListAllCrateEntry {
    /// Name of the crate.
    pub name: String,
    /// List of published versions of the crate.
    pub versions: Vec<ListAllCrateVersion>,
    /// Textual description of the crate.
    pub description: String,
    /// Date and time that this crate was created.
    pub created_at: DateTime<Utc>,
    /// Date and time that this crate was last updated.
    pub updated_at: DateTime<Utc>,
    /// Optional homepage for the crate.
    pub homepage: Option<String>,
    /// Optional repository link.
    pub repository: Option<String>,
    /// Optional documentation link.
    pub documentation: Option<String>,
    /// List of keywords for the crate.
    pub keywords: Vec<String>,
    /// List of categories for the crate.
    pub categories: Vec<String>,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct ListAllCrateVersion {
    pub version: Version,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct SearchResults {
    /// Array of results.
    pub crates: Vec<SearchResultsEntry>,
    pub meta: SearchResultsMeta,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct SearchResultsMeta {
    /// Total number of results available on the server.
    pub total: usize,
}

#[cfg_attr(feature = "client", derive(Deserialize))]
#[cfg_attr(feature = "server", derive(Serialize))]
pub struct SearchResultsEntry {
    /// Name of the crate.
    pub name: String,
    /// The highest version available.
    pub max_version: Version,
    /// Textual description of the crate.
    pub description: String,
}

#[cfg(feature = "client")]
fn default_v() -> u32 {
    1
}

impl CrateVersion {
    /// "Normalize" a crate version such that any functionally equivalent versions will be identical.
    pub fn normalize(&mut self) {
        self.normalize_features();
        self.normalize_dependencies();
    }

    fn normalize_dependencies(&mut self) {
        for d in self.deps.iter_mut() {
            d.features.sort();
        }

        self.deps.sort_by_key(|x| {
            (
                x.registry.clone(),
                x.name.clone(),
                x.package.clone(),
                x.req.to_string(),
                x.kind.clone(),
                x.target.clone(),
            )
        });
    }

    fn normalize_features(&mut self) {
        let mut features_2 = HashMap::new();

        std::mem::swap(&mut features_2, &mut self.features2);

        for (k, mut v) in features_2.into_iter() {
            v.sort();

            self.features.insert(k, v);
        }
    }
}