Skip to content

Commit

Permalink
Modify code according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
default committed Jul 24, 2024
1 parent 200114e commit 0e2f946
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ features = [
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
]
version = "0.52.0"
version = "0.52.0"
17 changes: 8 additions & 9 deletions src/rustup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::path::Path;

use anyhow::{Context, Result};

use crate::utils::cli::download_from_start;
use crate::utils::{HostTriple, Process};
use crate::utils::HostTriple;

const RUSTUP_DIST_SERVER: &str = "https://mirrors.tuna.tsinghua.edu.cn/rustup";
const RUSTUP_UPDATE_ROOT: &str = "https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup";
Expand All @@ -16,8 +18,8 @@ pub struct Rustup {
}

impl Rustup {
pub fn new(process: &Process) -> Self {
let host_triple = match HostTriple::from_host(process) {
pub fn new() -> Self {
let host_triple = match HostTriple::from_host() {
Some(host_triple) => host_triple,
None => panic!("Failed to get local host triple."),
};
Expand All @@ -26,15 +28,12 @@ impl Rustup {
}
}

pub fn download(&self, dest: &Path) {
pub fn download(&self, dest: &Path) -> Result<()> {
let download_url = url::Url::parse(&format!(
"{}/{}/{}/{}",
RUSTUP_UPDATE_ROOT, "dist", self.triple, RUSTUP_INIT
))
.expect("Failed to init rustup download url");
match download_from_start(RUSTUP_INIT, &download_url, dest) {
Ok(_) => (),
Err(e) => panic!("Failed to download rustup, cause: {:?}", e),
}
.context("Failed to init rustup download url")?;
download_from_start(RUSTUP_INIT, &download_url, dest).context("Failed to download rustup")
}
}
18 changes: 0 additions & 18 deletions src/utils/process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::process::{Command, Output};
Expand Down Expand Up @@ -83,20 +82,3 @@ pub fn forward_output(output: Output) -> Result<()> {
std::io::stderr().write_all(&output.stderr)?;
Ok(())
}

#[derive(Clone, Debug)]
pub enum Process {
OsProcess,
}

impl Process {
pub fn os() -> Self {
Self::OsProcess
}

pub fn var(&self, key: &str) -> Result<String, env::VarError> {
match self {
Process::OsProcess => env::var(key),
}
}
}
8 changes: 3 additions & 5 deletions src/utils/triple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{fmt, ops::Deref};

use crate::utils::Process;
use std::{env, fmt, ops::Deref};

// Linux hosts don't indicate clib in uname, however binaries only
// run on boxes with the same clib, as expected.
Expand Down Expand Up @@ -39,7 +37,7 @@ impl HostTriple {
Self(name.into())
}

pub(crate) fn from_host(process: &Process) -> Option<Self> {
pub(crate) fn from_host() -> Option<Self> {
#[cfg(windows)]
fn inner() -> Option<HostTriple> {
use std::mem;
Expand Down Expand Up @@ -156,7 +154,7 @@ impl HostTriple {
host_triple.map(HostTriple::new)
}

if let Ok(triple) = process.var("XUANWU_OVERRIDE_HOST_TRPLE") {
if let Ok(triple) = env::var("XUANWU_OVERRIDE_HOST_TRPLE") {
Some(Self(triple))
} else {
inner()
Expand Down

0 comments on commit 0e2f946

Please sign in to comment.