@@ -9,6 +9,26 @@ use crate::config::Channel;
9
9
use crate :: curl_helper:: BodyExt ;
10
10
use crate :: { run, Context } ;
11
11
12
+ #[ derive( Deserialize ) ]
13
+ struct Content {
14
+ content : String ,
15
+ }
16
+
17
+ #[ derive( Deserialize ) ]
18
+ struct CargoToml {
19
+ workspace : Workspace ,
20
+ }
21
+
22
+ #[ derive( Deserialize ) ]
23
+ struct Workspace {
24
+ package : Package ,
25
+ }
26
+
27
+ #[ derive( Deserialize ) ]
28
+ struct Package {
29
+ version : String ,
30
+ }
31
+
12
32
impl Context {
13
33
/// Promote a `rustup` release
14
34
///
@@ -108,21 +128,6 @@ impl Context {
108
128
fn get_next_rustup_version_from_github ( & self , sha : & str ) -> anyhow:: Result < String > {
109
129
println ! ( "Getting next Rustup version from Cargo.toml..." ) ;
110
130
111
- #[ derive( Deserialize ) ]
112
- struct Content {
113
- content : String ,
114
- }
115
-
116
- #[ derive( Deserialize ) ]
117
- struct CargoToml {
118
- package : Package ,
119
- }
120
-
121
- #[ derive( Deserialize ) ]
122
- struct Package {
123
- version : String ,
124
- }
125
-
126
131
let url =
127
132
format ! ( "https://api.github.com/repos/rust-lang/rustup/contents/Cargo.toml?ref={sha}" ) ;
128
133
@@ -131,12 +136,9 @@ impl Context {
131
136
client. useragent ( "rust-lang/promote-release" ) ?;
132
137
133
138
let content: Content = client. without_body ( ) . send_with_response ( ) ?;
134
- let decoded_content = base64:: decode ( content. content . replace ( '\n' , "" ) ) ?;
135
- let cargo_toml = String :: from_utf8 ( decoded_content) ?;
139
+ let toml = decode_and_deserialize_cargo_toml ( & content. content ) ?;
136
140
137
- let toml: CargoToml = toml:: from_str ( & cargo_toml) ?;
138
-
139
- Ok ( toml. package . version )
141
+ Ok ( toml. workspace . package . version )
140
142
}
141
143
142
144
fn download_rustup_artifacts ( & mut self , sha : & str ) -> Result < PathBuf , Error > {
@@ -223,3 +225,29 @@ version = '{}'
223
225
) ) )
224
226
}
225
227
}
228
+
229
+ fn decode_and_deserialize_cargo_toml ( base64_encoded_toml : & str ) -> Result < CargoToml , Error > {
230
+ let decoded_content = base64:: decode ( base64_encoded_toml. replace ( '\n' , "" ) ) ?;
231
+ let content_as_string = String :: from_utf8 ( decoded_content) ?;
232
+
233
+ toml:: from_str ( & content_as_string) . map_err ( Into :: into)
234
+ }
235
+
236
+ #[ cfg( test) ]
237
+ mod tests {
238
+ use crate :: rustup:: decode_and_deserialize_cargo_toml;
239
+
240
+ #[ test]
241
+ fn decode_cargo_toml ( ) {
242
+ let base64_encoded_toml = base64:: encode (
243
+ r#"
244
+ [workspace.package]
245
+ version = "1.2.3"
246
+ "# ,
247
+ ) ;
248
+
249
+ let toml = decode_and_deserialize_cargo_toml ( & base64_encoded_toml) . unwrap ( ) ;
250
+
251
+ assert_eq ! ( toml. workspace. package. version, "1.2.3" ) ;
252
+ }
253
+ }
0 commit comments