Repository: tauri-apps/tauri
File: crates/tauri-bundler/src/bundle/windows/util.rs (Rust)

Code before:

fn generate_alternative_url(url: &str) -> crate::Result<Option<(ureq::Agent, String)>> {
  if !url.starts_with("https://github.com/") {
    return Ok(None);
  }

  let mirror_url = generate_mirror_url_from_template(url);
  let cdn_url = generate_mirror_url_from_base(url)?;

  Ok(
    mirror_url
      .or(cdn_url)
      .map(|alternative_url| (ureq::AgentBuilder::new().build(), alternative_url)),
  )
}

fn create_agent_and_url(url: &str) -> crate::Result<(ureq::Agent, String)> {
  generate_alternative_url(url)?.map_or_else(
    || {
      Ok((
        ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
        url.to_owned(),
      ))
    },
    Ok,
  )
}

pub fn download(url: &str) -> crate::Result<Vec<u8>> {
  log::info!(action = "Downloading"; "{}", url);

  let (agent, final_url) = create_agent_and_url(url)?;

  let response = agent.get(&final_url).call().map_err(Box::new)?;
  let mut bytes = Vec::new();
  response.into_reader().read_to_end(&mut bytes)?;
  Ok(bytes)
}

#[derive(Clone, Copy)]
pub enum HashAlgorithm {
  #[cfg(target_os = "windows")]
  Sha256,
  Sha1,
}

/// Function used to download a file and checks SHA256 to verify the download.
pub fn download_and_verify(
  url: &str,
  hash: &str,
  hash_algorithm: HashAlgorithm,

Review comment (suggestion):
I generally dislike `map_or_else` since it is a badly named function and can be confusing at first glance, I would re-write this function without it
```suggestion
  generate_alternative_url(url).map(|alt_url| {
    alt_url.unwrap_or_else(|| (
        ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
        url.to_owned(),
      ))
  })
```

Code after:
fn generate_github_alternative_url(url: &str) -> Option<(ureq::Agent, String)> {
  if !url.starts_with("https://github.com/") {
    return None;
  }

  generate_github_mirror_url_from_template(url)
    .or_else(|| generate_github_mirror_url_from_base(url))
    .map(|alt_url| (ureq::AgentBuilder::new().build(), alt_url))
}

fn create_agent_and_url(url: &str) -> (ureq::Agent, String) {
  generate_github_alternative_url(url).unwrap_or((
    ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
    url.to_owned(),
  ))
}

pub fn download(url: &str) -> crate::Result<Vec<u8>> {
  let (agent, final_url) = create_agent_and_url(url);

  log::info!(action = "Downloading"; "{}", final_url);

  let response = agent.get(&final_url).call().map_err(Box::new)?;
  let mut bytes = Vec::new();
  response.into_reader().read_to_end(&mut bytes)?;
  Ok(bytes)
}

#[derive(Clone, Copy)]
pub enum HashAlgorithm {
  #[cfg(target_os = "windows")]
  Sha256,
  Sha1,
}

/// Function used to download a file and checks SHA256 to verify the download.
pub fn download_and_verify(
  url: &str,
  hash: &str,
  hash_algorithm: HashAlgorithm,
) -> crate::Result<Vec<u8>> {
  let data = download(url)?;
  log::info!("validating hash");
  verify_hash(&data, hash, hash_algorithm)?;
  Ok(data)
}

pub fn verify_hash(data: &[u8], hash: &str, hash_algorithm: HashAlgorithm) -> crate::Result<()> {
  match hash_algorithm {
    #[cfg(target_os = "windows")]
    HashAlgorithm::Sha256 => {