Skip to content

Commit c22a17e

Browse files
committed
Support workspace version for Rustup
1 parent 776973f commit c22a17e

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/rustup.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ use crate::config::Channel;
99
use crate::curl_helper::BodyExt;
1010
use crate::{run, Context};
1111

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+
1232
impl Context {
1333
/// Promote a `rustup` release
1434
///
@@ -108,21 +128,6 @@ impl Context {
108128
fn get_next_rustup_version_from_github(&self, sha: &str) -> anyhow::Result<String> {
109129
println!("Getting next Rustup version from Cargo.toml...");
110130

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-
126131
let url =
127132
format!("https://api.github.com/repos/rust-lang/rustup/contents/Cargo.toml?ref={sha}");
128133

@@ -131,12 +136,9 @@ impl Context {
131136
client.useragent("rust-lang/promote-release")?;
132137

133138
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)?;
136140

137-
let toml: CargoToml = toml::from_str(&cargo_toml)?;
138-
139-
Ok(toml.package.version)
141+
Ok(toml.workspace.package.version)
140142
}
141143

142144
fn download_rustup_artifacts(&mut self, sha: &str) -> Result<PathBuf, Error> {
@@ -223,3 +225,29 @@ version = '{}'
223225
)))
224226
}
225227
}
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

Comments
 (0)