-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
models.rs
230 lines (203 loc) · 5.85 KB
/
models.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
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
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use url::Url;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetadataDependency {
pub name: String,
pub version_req: VersionReq,
pub features: Vec<String>,
pub optional: bool,
pub default_features: bool,
pub target: Option<String>,
pub kind: Option<String>,
pub registry: Option<Url>,
pub explicit_name_in_toml: Option<String>,
}
impl Into<Dependency> for MetadataDependency {
#[tracing::instrument(skip(self))]
fn into(self) -> Dependency {
Dependency {
name: self.name,
req: self.version_req,
features: self.features,
optional: self.optional,
default_features: self.default_features,
target: self.target,
kind: self.kind,
registry: self.registry,
package: self.explicit_name_in_toml,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Dependency {
pub name: String,
pub req: VersionReq,
pub features: Vec<String>,
pub optional: bool,
pub default_features: bool,
pub target: Option<String>,
pub kind: Option<String>,
pub registry: Option<Url>,
pub package: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Metadata {
pub name: String,
pub vers: Version,
pub deps: Vec<MetadataDependency>,
pub features: HashMap<String, Vec<String>>,
pub authors: Vec<String>,
pub description: Option<String>,
pub documentation: Option<String>,
pub homepage: Option<url::Url>,
pub readme: Option<String>,
pub readme_file: Option<String>,
pub keywords: Vec<String>,
pub categories: Vec<String>,
pub license: Option<String>,
pub license_file: Option<String>,
pub repository: Option<url::Url>,
pub badges: HashMap<String, HashMap<String, String>>,
pub links: Option<String>,
#[serde(default)]
pub yanked: bool,
}
impl Metadata {
#[tracing::instrument(skip(self, checksum))]
pub fn to_package(&self, checksum: impl Into<String>) -> Package {
Package {
name: self.name.clone(),
vers: self.vers.clone(),
deps: self.deps.iter().map(Clone::clone).map(Into::into).collect(),
cksum: checksum.into(),
features: self.features.clone(),
yanked: false,
links: self.links.clone(),
}
}
#[tracing::instrument(skip(self))]
pub fn to_searched(&self) -> SearchedMetadata {
SearchedMetadata {
name: self.name.clone(),
max_version: self.vers.clone(),
description: self.description.as_ref().cloned().unwrap_or_default(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct SearchedMetadata {
pub name: String,
pub max_version: Version,
pub description: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Package {
pub name: String,
pub vers: Version,
pub deps: Vec<Dependency>,
pub cksum: String,
pub features: HashMap<String, Vec<String>>,
pub yanked: bool,
pub links: Option<String>,
}
impl Package {
#[tracing::instrument(skip(self))]
pub fn to_json_string(&self) -> serde_json::Result<String> {
serde_json::to_string(self)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct User {
pub id: u32,
pub login: String,
pub name: Option<String>,
}
impl User {
#[tracing::instrument(skip(id, login, name))]
pub fn new(id: u32, login: impl Into<String>, name: Option<impl Into<String>>) -> User {
User {
id,
login: login.into(),
name: name.map(Into::into),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Entry {
versions: HashMap<Version, Metadata>,
owner_ids: Vec<u32>,
}
impl Entry {
#[tracing::instrument(skip(self))]
pub fn is_empty(&self) -> bool {
self.versions.is_empty() && self.owner_ids.is_empty()
}
#[tracing::instrument(skip(self))]
pub fn versions(&self) -> &HashMap<Version, Metadata> {
&self.versions
}
#[tracing::instrument(skip(self, version))]
pub fn package_mut(&mut self, version: &Version) -> Option<&mut Metadata> {
self.versions.get_mut(version)
}
#[tracing::instrument(skip(self))]
pub fn latest_version(&self) -> Option<&Version> {
self.versions().keys().max()
}
#[tracing::instrument(skip(self))]
pub fn versions_mut(&mut self) -> &mut HashMap<Version, Metadata> {
&mut self.versions
}
#[tracing::instrument(skip(self))]
pub fn owner_ids(&self) -> &[u32] {
&self.owner_ids
}
#[tracing::instrument(skip(self))]
pub fn owner_ids_mut(&mut self) -> &mut Vec<u32> {
&mut self.owner_ids
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct Owners {
#[serde(rename = "users")]
pub logins: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Query {
#[serde(rename = "q")]
pub string: String,
#[serde(default = "query_limit_default", rename = "per_page")]
pub limit: usize,
}
const fn query_limit_default() -> usize {
10
}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Count {
total: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct Search {
crates: Vec<SearchedMetadata>,
meta: Count,
}
impl Search {
#[tracing::instrument(skip(crates, total))]
pub fn new(crates: Vec<SearchedMetadata>, total: usize) -> Search {
Search {
crates,
meta: Count { total },
}
}
}
#[derive(Clone, Deserialize)]
pub struct Credential {
pub password: String,
}
#[derive(Clone, Deserialize)]
pub struct ChangePassword {
pub old_password: String,
pub new_password: String,
}