Skip to content

Commit

Permalink
separate the add_cargo_toml_to_crates
Browse files Browse the repository at this point in the history
to avoid loading the Cargo.toml file from each crate when it
is not really needed
  • Loading branch information
szabgab committed Apr 16, 2024
1 parent 2d1fe5d commit c9d5ff3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use once_cell::sync::Lazy;
use regex::Regex;

use rust_digger::{
build_path, collected_data_root, get_owner_and_repo, load_details, percentage, read_crates,
Crate, CratesByOwner, Owners, Repo, User,
add_cargo_toml_to_crates, build_path, collected_data_root, get_owner_and_repo, load_details,
percentage, read_crates, Crate, CratesByOwner, Owners, Repo, User,
};

const URL: &str = "https://rust-digger.code-maven.com";
Expand Down Expand Up @@ -52,7 +52,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let (owner_by_crate_id, crates_by_owner): (Owners, CratesByOwner) = read_crate_owners()?;
let mut users = read_users(args.limit)?;
read_teams(&mut users, args.limit)?;
let mut crates: Vec<Crate> = read_crates(args.limit)?;
let mut crates: Vec<Crate> = add_cargo_toml_to_crates(read_crates(args.limit)?)?;

//dbg!(&crates_by_owner);

Expand Down
32 changes: 21 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,26 @@ pub fn read_versions() -> Result<Vec<CrateVersion>, Box<dyn Error>> {
Ok(versions)
}

pub fn add_cargo_toml_to_crates(crates: Vec<Crate>) -> Result<Vec<Crate>, Box<dyn Error>> {
let released_crates = load_cargo_toml_released_crates()?;
let cargo_of_crate: HashMap<String, Cargo> = released_crates
.iter()
.map(|krate| (krate.package.name.clone(), krate.clone()))
.collect::<HashMap<_, _>>();

let updated_crates = crates
.into_iter()
.map(|mut krate| {
krate.cargo = cargo_of_crate
.contains_key(&krate.name)
.then(|| cargo_of_crate[&krate.name].clone());
krate
})
.collect::<Vec<Crate>>();

Ok(updated_crates)
}

/// # Errors
///
/// Will return `Err` if can't open `crates.csv` or if it is not a
Expand All @@ -462,12 +482,6 @@ pub fn read_crates(limit: u32) -> Result<Vec<Crate>, Box<dyn Error>> {
let filepath = get_db_dump_folder().join("data/crates.csv");
log::info!("Start reading {filepath:?}");

let released_crates = load_cargo_toml_released_crates()?;
let cargo_of_crate: HashMap<String, Cargo> = released_crates
.iter()
.map(|krate| (krate.package.name.clone(), krate.clone()))
.collect::<HashMap<_, _>>();

let mut crates: Vec<Crate> = vec![];
let mut count = 0;
let file = File::open(&filepath)?;
Expand All @@ -479,11 +493,7 @@ pub fn read_crates(limit: u32) -> Result<Vec<Crate>, Box<dyn Error>> {
break;
}

let mut krate: Crate = result?;

krate.cargo = cargo_of_crate
.contains_key(&krate.name)
.then(|| cargo_of_crate[&krate.name].clone());
let krate: Crate = result?;

crates.push(krate);
}
Expand Down

0 comments on commit c9d5ff3

Please sign in to comment.