Skip to content

Commit

Permalink
fix: move mapping retrieve in CustomMappign struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed May 23, 2024
1 parent 4784870 commit 18e4429
Showing 1 changed file with 1 addition and 62 deletions.
63 changes: 1 addition & 62 deletions src/pypi_mapping/custom_pypi_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ use reqwest_middleware::ClientWithMiddleware;
use std::{collections::HashMap, sync::Arc};
use url::Url;

Check failure on line 6 in src/pypi_mapping/custom_pypi_mapping.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

unused import: `async_once_cell::OnceCell`

Check failure on line 6 in src/pypi_mapping/custom_pypi_mapping.rs

View workflow job for this annotation

GitHub Actions / Build Binary | Linux-x86_64

unused import: `async_once_cell::OnceCell`

Check failure on line 6 in src/pypi_mapping/custom_pypi_mapping.rs

View workflow job for this annotation

GitHub Actions / Build Binary | Linux-aarch64

unused import: `async_once_cell::OnceCell`

Check failure on line 6 in src/pypi_mapping/custom_pypi_mapping.rs

View workflow job for this annotation

GitHub Actions / Build Binary | macOS-x86

unused import: `async_once_cell::OnceCell`

Check failure on line 6 in src/pypi_mapping/custom_pypi_mapping.rs

View workflow job for this annotation

GitHub Actions / Build Binary | macOS-arm

unused import: `async_once_cell::OnceCell`
use async_once_cell::OnceCell;

use crate::pypi_mapping::MappingLocation;

use super::{
build_pypi_purl_from_package_record, is_conda_forge_record, prefix_pypi_name_mapping,
CustomMapping, MappingMap, Reporter,
CustomMapping, Reporter,
};

pub async fn fetch_mapping_from_url<T>(
Expand Down Expand Up @@ -45,63 +41,6 @@ where
Ok(mapping_by_name)
}

pub async fn fetch_custom_mapping(
client: &ClientWithMiddleware,
mapping_url: &MappingMap,
) -> miette::Result<&'static HashMap<String, HashMap<String, Option<String>>>> {
static MAPPING: OnceCell<HashMap<String, HashMap<String, Option<String>>>> = OnceCell::new();
MAPPING
.get_or_try_init(async {
let mut mapping_url_to_name: HashMap<String, HashMap<String, Option<String>>> =
Default::default();

for (name, url) in mapping_url.iter() {
// Fetch the mapping from the server or from the local

match url {
MappingLocation::Url(url) => {
let response = client
.get(url.clone())
.send()
.await
.into_diagnostic()
.context(format!(
"failed to download pypi mapping from {} location",
url.as_str()
))?;

if !response.status().is_success() {
return Err(miette::miette!(
"Could not request mapping located at {:?}",
url.as_str()
));
}

let mapping_by_name = fetch_mapping_from_url(client, url).await?;

mapping_url_to_name.insert(name.to_string(), mapping_by_name);
}
MappingLocation::Path(path) => {
let contents = std::fs::read_to_string(path)
.into_diagnostic()
.context(format!("mapping on {path:?} could not be loaded"))?;
let data: HashMap<String, Option<String>> = serde_json::from_str(&contents)
.into_diagnostic()
.context(format!(
"Failed to parse JSON mapping located at {}",
path.display()
))?;

mapping_url_to_name.insert(name.to_string(), data);
}
}
}

Ok(mapping_url_to_name)
})
.await
}

/// Amend the records with pypi purls if they are not present yet.
pub async fn amend_pypi_purls(
client: &ClientWithMiddleware,
Expand Down

0 comments on commit 18e4429

Please sign in to comment.