uv/crates/uv-build-frontend/src/pipreqs.rs
Ahmed Ilyas e7b55fefbf
Add extra-build-dependencies hint for any missing module on build failure (#15252)
Alternative to https://github.com/astral-sh/uv/pull/15251.

As suggested in
https://github.com/astral-sh/uv/issues/15118#issuecomment-3175735416

## Test Plan

`cargo test`

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2025-08-14 12:00:57 -05:00

32 lines
1.1 KiB
Rust

use std::str::FromStr;
use std::sync::LazyLock;
use rustc_hash::FxHashMap;
use uv_normalize::PackageName;
/// A mapping from module name to PyPI package name.
pub(crate) struct ModuleMap<'a>(FxHashMap<&'a str, PackageName>);
impl<'a> ModuleMap<'a> {
/// Generate a [`ModuleMap`] from a string representation, encoded in `${module}:{package}` format.
fn from_str(source: &'a str) -> Self {
let mut mapping = FxHashMap::default();
for line in source.lines() {
if let Some((module, package)) = line.split_once(':') {
let module = module.trim();
let package = PackageName::from_str(package.trim()).unwrap();
mapping.insert(module, package);
}
}
Self(mapping)
}
/// Look up a PyPI package name for a given module name.
pub(crate) fn lookup(&self, module: &str) -> Option<&PackageName> {
self.0.get(module)
}
}
/// A mapping from module name to PyPI package name.
pub(crate) static MODULE_MAPPING: LazyLock<ModuleMap> =
LazyLock::new(|| ModuleMap::from_str(include_str!("pipreqs/mapping")));