Skip to content

Commit

Permalink
introduce some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Apr 7, 2024
1 parent a9abeac commit 4e22aa3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,25 @@ pub fn load_details(repository: &str) -> Details {
Details::new()
}

fn create_repo_details_folders() {
let _res = fs::create_dir_all(repo_details_root());
let _res = fs::create_dir_all(repo_details_root().join("github"));
let _res = fs::create_dir_all(repo_details_root().join("gitlab"));
fn create_repo_details_folders() -> Result<(), Box<dyn std::error::Error>> {
fs::create_dir_all(repo_details_root())?;
fs::create_dir_all(repo_details_root().join("github"))?;
fs::create_dir_all(repo_details_root().join("gitlab"))?;

Ok(())
}

pub fn save_details(repository: &str, details: &Details) {
/// # Errors
///
/// Will return Err if can't create folders.
pub fn save_details(repository: &str, details: &Details) -> Result<(), Box<dyn std::error::Error>> {
log::info!("save_details for '{repository}'");

create_repo_details_folders();
create_repo_details_folders()?;

let (host, owner, repo) = get_owner_and_repo(repository);
if owner.is_empty() {
return; // this should never happen
return Ok(()); // this should never happen
}

let _res = fs::create_dir_all(repo_details_root().join(&host).join(&owner));
Expand All @@ -333,6 +338,8 @@ pub fn save_details(repository: &str, details: &Details) {
let content = serde_json::to_string(&details).unwrap();
let mut file = File::create(details_path).unwrap();
writeln!(&mut file, "{content}").unwrap();

Ok(())
}

/// # Errors
Expand Down
16 changes: 13 additions & 3 deletions src/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ fn main() {
log::info!("Starting the VCS processor {}", args.limit);

let crates: Vec<Crate> = ok_or_exit!(read_crates(0), 3);
collect_data_from_vcs(&crates, args.limit);
match collect_data_from_vcs(&crates, args.limit) {
Ok(()) => {}
Err(err) => {
log::error!("{err}");
}
}

log::info!("Ending the VCS processor");
}

fn collect_data_from_vcs(crates: &Vec<Crate>, limit: u32) {
fn collect_data_from_vcs(
crates: &Vec<Crate>,
limit: u32,
) -> Result<(), Box<dyn std::error::Error>> {
log::info!("process collect_data_from_vcs start");
log::info!("Total number of crates: {}", crates.len());
if 0 < limit {
Expand Down Expand Up @@ -97,12 +105,14 @@ fn collect_data_from_vcs(crates: &Vec<Crate>, limit: u32) {
}

env::set_current_dir(&current_dir).unwrap();
save_details(&krate.repository, &details);
save_details(&krate.repository, &details)?;

count += 1;
}

save_rustfm(&rustfmt);

Ok(())
}

fn collect_data_about_rustfmt(details: &mut Details, rustfmt: &mut Vec<String>, krate: &Crate) {
Expand Down

0 comments on commit 4e22aa3

Please sign in to comment.