Skip to content

Commit

Permalink
remove csv crate
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Dec 7, 2023
1 parent eb9817f commit 123cb0b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 62 deletions.
40 changes: 0 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ edition = "2021"
byteorder = "1.5"
fnv = "1.0"
sha1 = "0.10"
csv = "1.3"
strum = "0.25"
strum_macros = "0.25"
walkdir = "2.4"
Expand Down
30 changes: 9 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,33 +215,21 @@ pub fn sha1_hash_file(file_buffer: &Vec<u8>) -> [u8; 20] {
/// Get vanilla resource path hashes https://www.cyberpunk.net/en/modding-support
pub fn get_red4_hashes() -> HashMap<u64, String> {
let csv_data = include_bytes!("metadata-resources.csv");
parse_csv_data(csv_data)
}

/// Reads the metadata-resources.csv (csv of hashes and strings) from https://www.cyberpunk.net/en/modding-support
fn parse_csv_data(csv_data: &[u8]) -> HashMap<u64, String> {
let mut reader = csv::ReaderBuilder::new().from_reader(csv_data);
let mut csv_map: HashMap<u64, String> = HashMap::new();
let mut map: HashMap<u64, String> = HashMap::new();

for result in reader.records() {
match result {
Ok(record) => {
// Assuming the CSV has two columns: String and u64
if let (Some(path), Some(hash_str)) = (record.get(0), record.get(1)) {
if let Ok(hash) = hash_str.parse::<u64>() {
csv_map.insert(hash, path.to_string());
} else {
eprintln!("Error parsing u64 value: {}", hash_str);
}
} else {
eprintln!("Malformed CSV record: {:?}", record);
let reader = std::io::BufReader::new(&csv_data[..]);
for line in std::io::BufRead::lines(reader).flatten() {
let mut split = line.split(',');
if let Some(name) = split.next() {
if let Some(hash_str) = split.next() {
if let Ok(hash) = hash_str.parse::<u64>() {
map.insert(hash, name.to_owned());
}
}
Err(err) => eprintln!("Error reading CSV record: {}", err),
}
}

csv_map
map
}

/////////////////////////////////////////////////////////////////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions tests/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mod tests {
use std::fs::create_dir_all;
use std::path::Path;
use std::time::Instant;
use std::{fs, path::PathBuf};

use red4lib::archive::write_archive;
Expand All @@ -15,6 +16,16 @@ mod tests {
get_red4_hashes,
};

#[test]
fn time_csv() {
let start = Instant::now();
let hashes = get_red4_hashes();
assert!(!hashes.is_empty());
let end = Instant::now();
let duration = end - start;
println!("Execution time csv: {:?}", duration);
}

#[test]
fn read_archive() {
let archive_path = PathBuf::from("tests").join("test1.archive");
Expand Down

0 comments on commit 123cb0b

Please sign in to comment.