-
Notifications
You must be signed in to change notification settings - Fork 615
/
Copy pathkrate_publish.rs
249 lines (227 loc) · 8.69 KB
/
krate_publish.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! This module handles the expected information a crate should have
//! and manages the serialising and deserialising of this information
//! to and from structs. The serlializing is only utilised in
//! integration tests.
use std::collections::HashMap;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use crate::models::krate::MAX_NAME_LENGTH;
use crate::models::Crate;
use crate::models::DependencyKind;
use crate::models::Keyword as CrateKeyword;
#[derive(Deserialize, Serialize, Debug)]
pub struct EncodableCrateUpload {
pub name: EncodableCrateName,
pub vers: EncodableCrateVersion,
pub deps: Vec<EncodableCrateDependency>,
pub features: HashMap<EncodableFeatureName, Vec<EncodableFeature>>,
pub description: Option<String>,
pub homepage: Option<String>,
pub documentation: Option<String>,
pub readme: Option<String>,
pub readme_file: Option<String>,
#[serde(default)]
pub keywords: EncodableKeywordList,
#[serde(default)]
pub categories: EncodableCategoryList,
pub license: Option<String>,
pub license_file: Option<String>,
pub repository: Option<String>,
pub badges: Option<HashMap<String, HashMap<String, String>>>,
#[serde(default)]
pub links: Option<String>,
}
#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
pub struct EncodableCrateName(pub String);
#[derive(Serialize, Debug, Deref)]
pub struct EncodableDependencyName(pub String);
#[derive(Debug, Deref)]
pub struct EncodableCrateVersion(pub semver::Version);
#[derive(Debug, Deref)]
pub struct EncodableCrateVersionReq(pub String);
#[derive(Serialize, Debug, Deref, Default)]
pub struct EncodableKeywordList(pub Vec<EncodableKeyword>);
#[derive(Serialize, Debug, Deref)]
pub struct EncodableKeyword(pub String);
#[derive(Serialize, Debug, Deref, Default)]
pub struct EncodableCategoryList(pub Vec<EncodableCategory>);
#[derive(Serialize, Deserialize, Debug, Deref)]
pub struct EncodableCategory(pub String);
#[derive(Serialize, Debug, Deref)]
pub struct EncodableFeature(pub String);
#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
pub struct EncodableFeatureName(pub String);
#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableCrateDependency {
pub optional: bool,
pub default_features: bool,
pub name: EncodableCrateName,
pub features: Vec<EncodableFeature>,
pub version_req: EncodableCrateVersionReq,
pub target: Option<String>,
pub kind: Option<DependencyKind>,
pub explicit_name_in_toml: Option<EncodableDependencyName>,
pub registry: Option<String>,
}
impl<'de> Deserialize<'de> for EncodableCrateName {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCrateName, D::Error> {
let s = String::deserialize(d)?;
if !Crate::valid_name(&s) {
let value = de::Unexpected::Str(&s);
let expected = format!(
"a valid crate name to start with a letter, contain only letters, \
numbers, hyphens, or underscores and have at most {MAX_NAME_LENGTH} characters"
);
Err(de::Error::invalid_value(value, &expected.as_ref()))
} else {
Ok(EncodableCrateName(s))
}
}
}
impl<T: ?Sized> PartialEq<T> for EncodableCrateName
where
String: PartialEq<T>,
{
fn eq(&self, rhs: &T) -> bool {
self.0 == *rhs
}
}
impl<'de> Deserialize<'de> for EncodableDependencyName {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableDependencyName, D::Error> {
let s = String::deserialize(d)?;
if !Crate::valid_dependency_name(&s) {
let value = de::Unexpected::Str(&s);
let expected = format!(
"a valid dependency name to start with a letter or underscore, contain only letters, \
numbers, hyphens, or underscores and have at most {MAX_NAME_LENGTH} characters"
);
Err(de::Error::invalid_value(value, &expected.as_ref()))
} else {
Ok(EncodableDependencyName(s))
}
}
}
impl<'de> Deserialize<'de> for EncodableKeyword {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableKeyword, D::Error> {
let s = String::deserialize(d)?;
if !CrateKeyword::valid_name(&s) {
let value = de::Unexpected::Str(&s);
let expected = "a valid keyword specifier";
Err(de::Error::invalid_value(value, &expected))
} else {
Ok(EncodableKeyword(s))
}
}
}
impl<'de> Deserialize<'de> for EncodableFeatureName {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
if !Crate::valid_feature_name(&s) {
let value = de::Unexpected::Str(&s);
let expected = "a valid feature name containing only letters, \
numbers, '-', '+', or '_'";
Err(de::Error::invalid_value(value, &expected))
} else {
Ok(EncodableFeatureName(s))
}
}
}
impl<'de> Deserialize<'de> for EncodableFeature {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableFeature, D::Error> {
let s = String::deserialize(d)?;
if !Crate::valid_feature(&s) {
let value = de::Unexpected::Str(&s);
let expected = "a valid feature name";
Err(de::Error::invalid_value(value, &expected))
} else {
Ok(EncodableFeature(s))
}
}
}
impl<'de> Deserialize<'de> for EncodableCrateVersion {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCrateVersion, D::Error> {
let s = String::deserialize(d)?;
match semver::Version::parse(&s) {
Ok(v) => Ok(EncodableCrateVersion(v)),
Err(..) => {
let value = de::Unexpected::Str(&s);
let expected = "a valid semver";
Err(de::Error::invalid_value(value, &expected))
}
}
}
}
impl<'de> Deserialize<'de> for EncodableCrateVersionReq {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCrateVersionReq, D::Error> {
let s = String::deserialize(d)?;
match semver::VersionReq::parse(&s) {
Ok(_) => Ok(EncodableCrateVersionReq(s)),
Err(..) => {
let value = de::Unexpected::Str(&s);
let expected = "a valid version req";
Err(de::Error::invalid_value(value, &expected))
}
}
}
}
impl<'de> Deserialize<'de> for EncodableKeywordList {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableKeywordList, D::Error> {
let inner = <Vec<EncodableKeyword> as Deserialize<'de>>::deserialize(d)?;
if inner.len() > 5 {
let expected = "at most 5 keywords per crate";
return Err(de::Error::invalid_length(inner.len(), &expected));
}
for val in &inner {
if val.len() > 20 {
let expected = "a keyword with less than 20 characters";
return Err(de::Error::invalid_length(val.len(), &expected));
}
}
Ok(EncodableKeywordList(inner))
}
}
impl<'de> Deserialize<'de> for EncodableCategoryList {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableCategoryList, D::Error> {
let inner = <Vec<EncodableCategory> as Deserialize<'de>>::deserialize(d)?;
if inner.len() > 5 {
let expected = "at most 5 categories per crate";
Err(de::Error::invalid_length(inner.len(), &expected))
} else {
Ok(EncodableCategoryList(inner))
}
}
}
impl Serialize for EncodableCrateVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&(**self).to_string())
}
}
impl Serialize for EncodableCrateVersionReq {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&(**self).to_string())
}
}
use diesel::pg::Pg;
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::Text;
use std::io::Write;
impl ToSql<Text, Pg> for EncodableFeature {
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
ToSql::<Text, Pg>::to_sql(&**self, out)
}
}
#[test]
fn feature_deserializes_for_valid_features() {
use serde_json as json;
assert_ok!(json::from_str::<EncodableFeature>("\"foo\""));
assert_err!(json::from_str::<EncodableFeature>("\"\""));
assert_err!(json::from_str::<EncodableFeature>("\"/\""));
assert_err!(json::from_str::<EncodableFeature>("\"%/%\""));
assert_ok!(json::from_str::<EncodableFeature>("\"a/a\""));
assert_ok!(json::from_str::<EncodableFeature>("\"32-column-tables\""));
}