-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathviews.rs
1197 lines (1042 loc) · 36.4 KB
/
views.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use chrono::{DateTime, Utc};
use crate::external_urls::remove_blocked_urls;
use crate::models::{
ApiToken, Category, Crate, Dependency, DependencyKind, Keyword, Owner, ReverseDependency, Team,
TopVersions, User, Version, VersionDownload, VersionOwnerAction,
};
use crates_io_github as github;
pub mod krate_publish;
pub use self::krate_publish::{EncodableCrateDependency, PublishMetadata};
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = Category)]
pub struct EncodableCategory {
/// An opaque identifier for the category.
#[schema(example = "game-development")]
pub id: String,
/// The name of the category.
#[schema(example = "Game development")]
pub category: String,
/// The "slug" of the category.
///
/// See <https://crates.io/category_slugs>.
#[schema(example = "game-development")]
pub slug: String,
/// A description of the category.
#[schema(example = "Libraries for creating games.")]
pub description: String,
/// The date and time this category was created.
#[schema(example = "2019-12-13T13:46:41Z")]
pub created_at: DateTime<Utc>,
/// The total number of crates that have this category.
#[schema(example = 42)]
pub crates_cnt: i32,
/// The subcategories of this category.
///
/// This field is only present when the category details are queried,
/// but not when listing categories.
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(no_recursion, example = json!([]))]
pub subcategories: Option<Vec<EncodableCategory>>,
/// The parent categories of this category.
///
/// This field is only present when the category details are queried,
/// but not when listing categories.
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(no_recursion, example = json!([]))]
pub parent_categories: Option<Vec<EncodableCategory>>,
}
impl From<Category> for EncodableCategory {
fn from(category: Category) -> Self {
let Category {
crates_cnt,
category,
slug,
description,
created_at,
..
} = category;
Self {
id: slug.clone(),
slug,
description,
created_at,
crates_cnt,
category: category.rsplit("::").collect::<Vec<_>>()[0].to_string(),
subcategories: None,
parent_categories: None,
}
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, utoipa::ToSchema)]
#[schema(as = LegacyCrateOwnerInvitation)]
pub struct EncodableCrateOwnerInvitationV1 {
/// The ID of the user who was invited to be a crate owner.
#[schema(example = 42)]
pub invitee_id: i32,
/// The ID of the user who sent the invitation.
#[schema(example = 3)]
pub inviter_id: i32,
/// The username of the user who sent the invitation.
#[schema(example = "ghost")]
pub invited_by_username: String,
/// The name of the crate that the user was invited to be an owner of.
#[schema(example = "serde")]
pub crate_name: String,
/// The ID of the crate that the user was invited to be an owner of.
#[schema(example = 123)]
pub crate_id: i32,
/// The date and time this invitation was created.
#[schema(example = "2019-12-13T13:46:41Z")]
pub created_at: DateTime<Utc>,
/// The date and time this invitation will expire.
#[schema(example = "2020-01-13T13:46:41Z")]
pub expires_at: DateTime<Utc>,
}
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, utoipa::ToSchema)]
#[schema(as = CrateOwnerInvitation)]
pub struct EncodableCrateOwnerInvitation {
/// The ID of the user who was invited to be a crate owner.
#[schema(example = 42)]
pub invitee_id: i32,
/// The ID of the user who sent the invitation.
#[schema(example = 3)]
pub inviter_id: i32,
/// The ID of the crate that the user was invited to be an owner of.
#[schema(example = 123)]
pub crate_id: i32,
/// The name of the crate that the user was invited to be an owner of.
#[schema(example = "serde")]
pub crate_name: String,
/// The date and time this invitation was created.
#[schema(example = "2019-12-13T13:46:41Z")]
pub created_at: DateTime<Utc>,
/// The date and time this invitation will expire.
#[schema(example = "2020-01-13T13:46:41Z")]
pub expires_at: DateTime<Utc>,
}
#[derive(Deserialize, Serialize, Debug, Copy, Clone, utoipa::ToSchema)]
pub struct InvitationResponse {
/// The opaque identifier for the crate this invitation is for.
#[schema(example = 42)]
pub crate_id: i32,
/// Whether the invitation was accepted.
#[schema(example = true)]
pub accepted: bool,
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
pub struct EncodableDependency {
/// An opaque identifier for the dependency.
#[schema(example = 169)]
pub id: i32,
/// The ID of the version this dependency belongs to.
#[schema(example = 42)]
pub version_id: i32,
/// The name of the crate this dependency points to.
#[schema(example = "serde")]
pub crate_id: String,
/// The version requirement for this dependency.
#[schema(example = "^1")]
pub req: String,
/// Whether this dependency is optional.
pub optional: bool,
/// Whether default features are enabled for this dependency.
#[schema(example = true)]
pub default_features: bool,
/// The features explicitly enabled for this dependency.
pub features: Vec<String>,
/// The target platform for this dependency, if any.
pub target: Option<String>,
/// The type of dependency this is (normal, dev, or build).
#[schema(value_type = String, example = "normal")]
pub kind: DependencyKind,
/// The total number of downloads for the crate this dependency points to.
#[schema(example = 123_456)]
pub downloads: i64,
}
impl EncodableDependency {
pub fn from_dep(dependency: Dependency, crate_name: &str) -> Self {
Self::encode(dependency, crate_name, None)
}
pub fn from_reverse_dep(rev_dep: ReverseDependency, crate_name: &str) -> Self {
let dependency = rev_dep.dependency;
Self::encode(dependency, crate_name, Some(rev_dep.crate_downloads))
}
// `downloads` need only be specified when generating a reverse dependency
fn encode(dependency: Dependency, crate_name: &str, downloads: Option<i64>) -> Self {
Self {
id: dependency.id,
version_id: dependency.version_id,
crate_id: crate_name.into(),
req: dependency.req,
optional: dependency.optional,
default_features: dependency.default_features,
features: dependency.features,
target: dependency.target,
kind: dependency.kind,
downloads: downloads.unwrap_or(0),
}
}
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = VersionDownload)]
pub struct EncodableVersionDownload {
/// The ID of the version this download count is for.
#[schema(example = 42)]
pub version: i32,
/// The number of downloads for this version on the given date.
#[schema(example = 123)]
pub downloads: i32,
/// The date this download count is for.
#[schema(example = "2019-12-13")]
pub date: String,
}
impl From<VersionDownload> for EncodableVersionDownload {
fn from(download: VersionDownload) -> Self {
Self {
version: download.version_id,
downloads: download.downloads,
date: download.date.to_string(),
}
}
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = Keyword)]
pub struct EncodableKeyword {
/// An opaque identifier for the keyword.
#[schema(example = "http")]
pub id: String,
/// The keyword itself.
#[schema(example = "http")]
pub keyword: String,
/// The date and time this keyword was created.
#[schema(example = "2017-01-06T14:23:11Z")]
pub created_at: DateTime<Utc>,
/// The total number of crates that have this keyword.
#[schema(example = 42)]
pub crates_cnt: i32,
}
impl From<Keyword> for EncodableKeyword {
fn from(keyword: Keyword) -> Self {
let Keyword {
crates_cnt,
keyword,
created_at,
..
} = keyword;
Self {
id: keyword.clone(),
created_at,
crates_cnt,
keyword,
}
}
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = Crate)]
pub struct EncodableCrate {
/// An opaque identifier for the crate.
#[schema(example = "serde")]
pub id: String,
/// The name of the crate.
#[schema(example = "serde")]
pub name: String,
/// The date and time this crate was last updated.
#[schema(example = "2019-12-13T13:46:41Z")]
pub updated_at: DateTime<Utc>,
/// The list of version IDs belonging to this crate.
#[schema(example = json!(null))]
pub versions: Option<Vec<i32>>,
/// The list of keywords belonging to this crate.
#[schema(example = json!(null))]
pub keywords: Option<Vec<String>>,
/// The list of categories belonging to this crate.
#[schema(example = json!(null))]
pub categories: Option<Vec<String>>,
#[schema(deprecated, value_type = Vec<Object>, example = json!([]))]
pub badges: [(); 0],
/// The date and time this crate was created.
#[schema(example = "2019-12-13T13:46:41Z")]
pub created_at: DateTime<Utc>,
/// The total number of downloads for this crate.
#[schema(example = 123_456_789)]
pub downloads: i64,
/// The total number of downloads for this crate in the last 90 days.
#[schema(example = 456_789)]
pub recent_downloads: Option<i64>,
/// The "default" version of this crate.
///
/// This version will be displayed by default on the crate's page.
#[schema(example = "1.3.0")]
pub default_version: Option<String>,
/// The total number of versions for this crate.
#[schema(example = 13)]
pub num_versions: i32,
/// Whether all versions of this crate have been yanked.
pub yanked: bool,
/// The highest version number for this crate.
#[schema(deprecated, example = "2.0.0-beta.1")]
pub max_version: String,
/// The most recently published version for this crate.
#[schema(deprecated, example = "1.2.3")]
pub newest_version: String,
/// The highest version number for this crate that is not a pre-release.
#[schema(deprecated, example = "1.3.0")]
pub max_stable_version: Option<String>,
/// Description of the crate.
#[schema(example = "A generic serialization/deserialization framework")]
pub description: Option<String>,
/// The URL to the crate's homepage, if set.
#[schema(example = "https://serde.rs")]
pub homepage: Option<String>,
/// The URL to the crate's documentation, if set.
#[schema(example = "https://docs.rs/serde")]
pub documentation: Option<String>,
/// The URL to the crate's repository, if set.
#[schema(example = "https://github.com/serde-rs/serde")]
pub repository: Option<String>,
/// Links to other API endpoints related to this crate.
pub links: EncodableCrateLinks,
/// Whether the crate name was an exact match.
#[schema(deprecated)]
pub exact_match: bool,
}
impl EncodableCrate {
#[allow(clippy::too_many_arguments)]
pub fn from(
krate: Crate,
default_version: Option<&str>,
num_versions: i32,
yanked: Option<bool>,
top_versions: Option<&TopVersions>,
versions: Option<Vec<i32>>,
keywords: Option<&[Keyword]>,
categories: Option<&[Category]>,
exact_match: bool,
downloads: i64,
recent_downloads: Option<i64>,
) -> Self {
let Crate {
name,
created_at,
updated_at,
description,
homepage,
documentation,
repository,
..
} = krate;
let versions_link = match versions {
Some(..) => None,
None => Some(format!("/api/v1/crates/{name}/versions")),
};
let keyword_ids = keywords.map(|kws| kws.iter().map(|kw| kw.keyword.clone()).collect());
let category_ids = categories.map(|cats| cats.iter().map(|cat| cat.slug.clone()).collect());
let homepage = remove_blocked_urls(homepage);
let documentation = remove_blocked_urls(documentation);
let repository = remove_blocked_urls(repository);
let default_version = default_version.map(ToString::to_string);
if default_version.is_none() {
let message = format!("Crate `{name}` has no default version");
sentry::capture_message(&message, sentry::Level::Info);
}
let yanked = yanked.unwrap_or_default();
let max_version = top_versions
.and_then(|v| v.highest.as_ref())
.map(|v| v.to_string())
.unwrap_or_else(|| "0.0.0".to_string());
let newest_version = top_versions
.and_then(|v| v.newest.as_ref())
.map(|v| v.to_string())
.unwrap_or_else(|| "0.0.0".to_string());
let max_stable_version = top_versions
.and_then(|v| v.highest_stable.as_ref())
.map(|v| v.to_string());
// the total number of downloads is eventually consistent, but can lag
// behind the number of "recent downloads". to hide this inconsistency
// we will use the "recent downloads" as "total downloads" in case it is
// higher.
let downloads = if matches!(recent_downloads, Some(x) if x > downloads) {
recent_downloads.unwrap()
} else {
downloads
};
EncodableCrate {
id: name.clone(),
name: name.clone(),
updated_at,
created_at,
downloads,
recent_downloads,
versions,
keywords: keyword_ids,
categories: category_ids,
badges: [],
default_version,
num_versions,
yanked,
max_version,
newest_version,
max_stable_version,
documentation,
homepage,
exact_match,
description,
repository,
links: EncodableCrateLinks {
version_downloads: format!("/api/v1/crates/{name}/downloads"),
versions: versions_link,
owners: Some(format!("/api/v1/crates/{name}/owners")),
owner_team: Some(format!("/api/v1/crates/{name}/owner_team")),
owner_user: Some(format!("/api/v1/crates/{name}/owner_user")),
reverse_dependencies: format!("/api/v1/crates/{name}/reverse_dependencies"),
},
}
}
#[allow(clippy::too_many_arguments)]
pub fn from_minimal(
krate: Crate,
default_version: Option<&str>,
num_versions: i32,
yanked: Option<bool>,
top_versions: Option<&TopVersions>,
exact_match: bool,
downloads: i64,
recent_downloads: Option<i64>,
) -> Self {
Self::from(
krate,
default_version,
num_versions,
yanked,
top_versions,
None,
None,
None,
exact_match,
downloads,
recent_downloads,
)
}
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = CrateLinks)]
pub struct EncodableCrateLinks {
/// The API path to this crate's download statistics.
#[schema(example = "/api/v1/crates/serde/downloads")]
pub version_downloads: String,
/// The API path to this crate's versions.
#[schema(example = "/api/v1/crates/serde/versions")]
pub versions: Option<String>,
/// The API path to this crate's owners.
#[schema(example = "/api/v1/crates/serde/owners")]
pub owners: Option<String>,
/// The API path to this crate's team owners.
#[schema(example = "/api/v1/crates/serde/owner_team")]
pub owner_team: Option<String>,
/// The API path to this crate's user owners.
#[schema(example = "/api/v1/crates/serde/owner_user")]
pub owner_user: Option<String>,
/// The API path to this crate's reverse dependencies.
#[schema(example = "/api/v1/crates/serde/reverse_dependencies")]
pub reverse_dependencies: String,
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = Owner)]
pub struct EncodableOwner {
/// The opaque identifier for the team or user, depending on the `kind` field.
#[schema(example = 42)]
pub id: i32,
/// The login name of the team or user.
#[schema(example = "ghost")]
pub login: String,
/// The kind of the owner (`user` or `team`).
#[schema(example = "user")]
pub kind: String,
/// The URL to the owner's profile.
#[schema(example = "https://github.com/ghost")]
pub url: Option<String>,
/// The display name of the team or user.
#[schema(example = "Kate Morgan")]
pub name: Option<String>,
/// The avatar URL of the team or user.
#[schema(example = "https://avatars2.githubusercontent.com/u/1234567?v=4")]
pub avatar: Option<String>,
}
impl From<Owner> for EncodableOwner {
fn from(owner: Owner) -> Self {
match owner {
Owner::User(User {
id,
name,
gh_login,
gh_avatar,
..
}) => {
let url = format!("https://github.com/{gh_login}");
Self {
id,
login: gh_login,
avatar: gh_avatar,
url: Some(url),
name,
kind: String::from("user"),
}
}
Owner::Team(Team {
id,
name,
login,
avatar,
..
}) => {
let url = github::team_url(&login);
Self {
id,
login,
url: Some(url),
avatar,
name,
kind: String::from("team"),
}
}
}
}
}
#[derive(Serialize, Debug, utoipa::ToSchema)]
#[schema(as = Team)]
pub struct EncodableTeam {
/// An opaque identifier for the team.
#[schema(example = 42)]
pub id: i32,
/// The login name of the team.
#[schema(example = "github:rust-lang:crates-io")]
pub login: String,
/// The display name of the team.
#[schema(example = "Crates.io team")]
pub name: Option<String>,
/// The avatar URL of the team.
#[schema(example = "https://avatars2.githubusercontent.com/u/1234567?v=4")]
pub avatar: Option<String>,
/// The GitHub profile URL of the team.
#[schema(example = "https://github.com/rust-lang")]
pub url: Option<String>,
}
impl From<Team> for EncodableTeam {
fn from(team: Team) -> Self {
let Team {
id,
name,
login,
avatar,
..
} = team;
let url = github::team_url(&login);
EncodableTeam {
id,
login,
name,
avatar,
url: Some(url),
}
}
}
#[derive(Serialize, Debug, utoipa::ToSchema)]
pub struct EncodableApiTokenWithToken {
#[serde(flatten)]
pub token: ApiToken,
/// The plaintext API token.
///
/// Only available when the token is created.
#[serde(rename = "token")]
#[schema(example = "a1b2c3d4e5f6g7h8i9j0")]
pub plaintext: String,
}
#[derive(Deserialize, Serialize, Debug, utoipa::ToSchema)]
pub struct OwnedCrate {
/// The opaque identifier of the crate.
#[schema(example = 123)]
pub id: i32,
/// The name of the crate.
#[schema(example = "serde")]
pub name: String,
#[schema(deprecated)]
pub email_notifications: bool,
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
pub struct EncodableMe {
/// The authenticated user.
pub user: EncodablePrivateUser,
/// The crates that the authenticated user owns.
#[schema(inline)]
pub owned_crates: Vec<OwnedCrate>,
}
#[derive(Deserialize, Serialize, Debug, utoipa::ToSchema)]
#[schema(as = AuthenticatedUser)]
pub struct EncodablePrivateUser {
/// An opaque identifier for the user.
#[schema(example = 42)]
pub id: i32,
/// The user's login name.
#[schema(example = "ghost")]
pub login: String,
/// Whether the user's email address has been verified.
#[schema(example = true)]
pub email_verified: bool,
/// Whether the user's email address verification email has been sent.
#[schema(example = true)]
pub email_verification_sent: bool,
/// The user's display name, if set.
#[schema(example = "Kate Morgan")]
pub name: Option<String>,
/// The user's email address, if set.
#[schema(example = "kate@morgan.dev")]
pub email: Option<String>,
/// The user's avatar URL, if set.
#[schema(example = "https://avatars2.githubusercontent.com/u/1234567?v=4")]
pub avatar: Option<String>,
/// The user's GitHub profile URL.
#[schema(example = "https://github.com/ghost")]
pub url: Option<String>,
/// Whether the user is a crates.io administrator.
#[schema(example = false)]
pub is_admin: bool,
/// Whether the user has opted in to receive publish notifications via email.
#[schema(example = true)]
pub publish_notifications: bool,
}
impl EncodablePrivateUser {
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
pub fn from(
user: User,
email: Option<String>,
email_verified: bool,
email_verification_sent: bool,
) -> Self {
let User {
id,
name,
gh_login,
gh_avatar,
is_admin,
publish_notifications,
..
} = user;
let url = format!("https://github.com/{gh_login}");
EncodablePrivateUser {
id,
email,
email_verified,
email_verification_sent,
avatar: gh_avatar,
login: gh_login,
name,
url: Some(url),
is_admin,
publish_notifications,
}
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, utoipa::ToSchema)]
#[schema(as = User)]
pub struct EncodablePublicUser {
/// An opaque identifier for the user.
#[schema(example = 42)]
pub id: i32,
/// The user's login name.
#[schema(example = "ghost")]
pub login: String,
/// The user's display name, if set.
#[schema(example = "Kate Morgan")]
pub name: Option<String>,
/// The user's avatar URL, if set.
#[schema(example = "https://avatars2.githubusercontent.com/u/1234567?v=4")]
pub avatar: Option<String>,
/// The user's GitHub profile URL.
#[schema(example = "https://github.com/ghost")]
pub url: String,
}
/// Converts a `User` model into an `EncodablePublicUser` for JSON serialization.
impl From<User> for EncodablePublicUser {
fn from(user: User) -> Self {
let User {
id,
name,
gh_login,
gh_avatar,
..
} = user;
let url = format!("https://github.com/{gh_login}");
EncodablePublicUser {
id,
avatar: gh_avatar,
login: gh_login,
name,
url,
}
}
}
#[derive(Deserialize, Serialize, Debug, utoipa::ToSchema)]
pub struct EncodableAuditAction {
/// The action that was performed.
#[schema(example = "publish")]
pub action: String,
/// The user who performed the action.
pub user: EncodablePublicUser,
/// The date and time the action was performed.
#[schema(example = "2019-12-13T13:46:41Z")]
pub time: DateTime<Utc>,
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = Version)]
pub struct EncodableVersion {
/// An opaque identifier for the version.
#[schema(example = 42)]
pub id: i32,
/// The name of the crate.
#[serde(rename = "crate")]
#[schema(example = "serde")]
pub krate: String,
/// The version number.
#[schema(example = "1.0.0")]
pub num: String,
/// The API path to download the crate.
#[schema(example = "/api/v1/crates/serde/1.0.0/download")]
pub dl_path: String,
/// The API path to download the crate's README file as HTML code.
#[schema(example = "/api/v1/crates/serde/1.0.0/readme")]
pub readme_path: String,
/// The date and time this version was last updated (i.e. yanked or unyanked).
#[schema(example = "2019-12-13T13:46:41Z")]
pub updated_at: DateTime<Utc>,
/// The date and time this version was created.
#[schema(example = "2019-12-13T13:46:41Z")]
pub created_at: DateTime<Utc>,
/// The total number of downloads for this version.
#[schema(example = 123_456)]
pub downloads: i32,
/// The features defined by this version.
#[schema(value_type = Object)]
pub features: serde_json::Value,
/// Whether this version has been yanked.
#[schema(example = false)]
pub yanked: bool,
/// The message given when this version was yanked, if any.
#[schema(example = "Security vulnerability")]
pub yank_message: Option<String>,
/// The name of the native library this version links with, if any.
#[schema(example = "git2")]
pub lib_links: Option<String>,
/// The license of this version of the crate.
#[schema(example = "MIT")]
pub license: Option<String>,
/// Links to other API endpoints related to this version.
pub links: EncodableVersionLinks,
/// The size of the compressed crate file in bytes.
#[schema(example = 1_234)]
pub crate_size: i32,
/// The user who published this version.
///
/// This field may be `null` if the version was published before crates.io
/// started recording this information.
pub published_by: Option<EncodablePublicUser>,
/// A list of actions performed on this version.
#[schema(inline)]
pub audit_actions: Vec<EncodableAuditAction>,
/// The SHA256 checksum of the compressed crate file encoded as a
/// hexadecimal string.
#[schema(example = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60")]
pub checksum: String,
/// The minimum version of the Rust compiler required to compile
/// this version, if set.
#[schema(example = "1.31")]
pub rust_version: Option<String>,
/// Whether this version can be used as a library.
#[schema(example = true)]
pub has_lib: Option<bool>,
/// The names of the binaries provided by this version, if any.
#[schema(example = json!([]))]
pub bin_names: Option<Vec<Option<String>>>,
/// The Rust Edition used to compile this version, if set.
#[schema(example = "2021")]
pub edition: Option<String>,
/// The description of this version of the crate.
#[schema(example = "A generic serialization/deserialization framework")]
pub description: Option<String>,
/// The URL to the crate's homepage, if set.
#[schema(example = "https://serde.rs")]
pub homepage: Option<String>,
/// The URL to the crate's documentation, if set.
#[schema(example = "https://docs.rs/serde")]
pub documentation: Option<String>,
/// The URL to the crate's repository, if set.
#[schema(example = "https://github.com/serde-rs/serde")]
pub repository: Option<String>,
}
impl EncodableVersion {
pub fn from(
version: Version,
crate_name: &str,
published_by: Option<User>,
audit_actions: Vec<(VersionOwnerAction, User)>,
) -> Self {
let Version {
id,
num,
updated_at,
created_at,
downloads,
features,
yanked,
yank_message,
links: lib_links,
license,
crate_size,
checksum,
rust_version,
has_lib,
bin_names,
edition,
description,
homepage,
documentation,
repository,
..
} = version;
let links = EncodableVersionLinks {
dependencies: format!("/api/v1/crates/{crate_name}/{num}/dependencies"),
version_downloads: format!("/api/v1/crates/{crate_name}/{num}/downloads"),
authors: format!("/api/v1/crates/{crate_name}/{num}/authors"),
};
Self {
dl_path: format!("/api/v1/crates/{crate_name}/{num}/download"),
readme_path: format!("/api/v1/crates/{crate_name}/{num}/readme"),
num,
id,
krate: crate_name.to_string(),
updated_at,
created_at,
downloads,
features,
yanked,
yank_message,
lib_links,
license,
links,
crate_size,
checksum,
rust_version,
has_lib,
bin_names,
edition,
description,
homepage,
documentation,
repository,
published_by: published_by.map(User::into),
audit_actions: audit_actions
.into_iter()
.map(|(audit_action, user)| EncodableAuditAction {
action: audit_action.action.into(),
user: user.into(),
time: audit_action.time,
})
.collect(),
}
}
}
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
#[schema(as = VersionLinks)]
pub struct EncodableVersionLinks {
/// The API path to download this version's dependencies.
#[schema(example = "/api/v1/crates/serde/1.0.0/dependencies")]
pub dependencies: String,
/// The API path to download this version's download numbers.
#[schema(example = "/api/v1/crates/serde/1.0.0/downloads")]