1
+ use std:: str:: FromStr ;
2
+
3
+ use chrono:: { DateTime , NaiveDate , Utc } ;
1
4
use conduit:: { Request , Response } ;
5
+ use semver;
2
6
use serde_json;
3
7
4
8
use app:: RequestApp ;
@@ -8,7 +12,8 @@ use owner::rights;
8
12
use user:: RequestUser ;
9
13
use util:: { human, CargoResult , RequestUtils } ;
10
14
use version:: version_and_crate;
11
- use views:: EncodableVersionBuildInfoUpload ;
15
+ use views:: { EncodableMaxVersionBuildInfo , EncodableVersionBuildInfoUpload ,
16
+ ParsedRustChannelVersion } ;
12
17
13
18
use schema:: * ;
14
19
@@ -24,6 +29,80 @@ pub struct BuildInfo {
24
29
pub passed : bool ,
25
30
}
26
31
32
+ /// The columns to select from the `build_info` table. The table also stores `created_at` and
33
+ /// `updated_at` metadata for each row, but we're not displaying those anywhere so we're not
34
+ /// bothering to select them.
35
+ pub const BUILD_INFO_FIELDS : (
36
+ build_info:: version_id ,
37
+ build_info:: rust_version ,
38
+ build_info:: target ,
39
+ build_info:: passed ,
40
+ ) = (
41
+ build_info:: version_id,
42
+ build_info:: rust_version,
43
+ build_info:: target,
44
+ build_info:: passed,
45
+ ) ;
46
+
47
+ #[ derive( Debug ) ]
48
+ /// The maximum version of Rust from each channel that a crate version successfully builds with.
49
+ /// Used for summarizing this information in badge form on crate list pages.
50
+ pub struct MaxBuildInfo {
51
+ pub stable : Option < semver:: Version > ,
52
+ pub beta : Option < NaiveDate > ,
53
+ pub nightly : Option < NaiveDate > ,
54
+ }
55
+
56
+ impl MaxBuildInfo {
57
+ /// Encode stable semver number as a string and beta and nightly as times appropriate for
58
+ /// JSON.
59
+ pub fn encode ( self ) -> EncodableMaxVersionBuildInfo {
60
+ fn naive_date_to_rfc3339 ( date : NaiveDate ) -> String {
61
+ DateTime :: < Utc > :: from_utc ( date. and_hms ( 0 , 0 , 0 ) , Utc ) . to_rfc3339 ( )
62
+ }
63
+
64
+ EncodableMaxVersionBuildInfo {
65
+ stable : self . stable . map ( |v| v. to_string ( ) ) ,
66
+ beta : self . beta . map ( naive_date_to_rfc3339) ,
67
+ nightly : self . nightly . map ( naive_date_to_rfc3339) ,
68
+ }
69
+ }
70
+ }
71
+
72
+ impl BuildInfo {
73
+ /// From a set of build information data, Find the largest or latest Rust versions that we know
74
+ /// about for each channel. Stable uses the largest semver version number; beta and nightly use
75
+ /// the latest date.
76
+ pub fn max < I > ( build_infos : I ) -> CargoResult < MaxBuildInfo >
77
+ where
78
+ I : IntoIterator < Item = BuildInfo > ,
79
+ {
80
+ let build_infos = build_infos
81
+ . into_iter ( )
82
+ . map ( |bi| ParsedRustChannelVersion :: from_str ( & bi. rust_version ) )
83
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
84
+
85
+ let stable = build_infos
86
+ . iter ( )
87
+ . filter_map ( ParsedRustChannelVersion :: as_stable)
88
+ . max ( ) ;
89
+ let beta = build_infos
90
+ . iter ( )
91
+ . filter_map ( ParsedRustChannelVersion :: as_beta)
92
+ . max ( ) ;
93
+ let nightly = build_infos
94
+ . iter ( )
95
+ . filter_map ( ParsedRustChannelVersion :: as_nightly)
96
+ . max ( ) ;
97
+
98
+ Ok ( MaxBuildInfo {
99
+ stable : stable. cloned ( ) ,
100
+ beta : beta. cloned ( ) ,
101
+ nightly : nightly. cloned ( ) ,
102
+ } )
103
+ }
104
+ }
105
+
27
106
/// Handles the `POST /crates/:crate_id/:version/build_info` route for the
28
107
/// `cargo publish-build-info` command to report on which versions of Rust
29
108
/// a crate builds with.
0 commit comments