You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB
Rust

use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::process;
fn apply_renames(filename: &String, renames: &HashMap<String, String>) -> Result<()> {
let mut outputs = Vec::new();
let pls_file = File::open(filename)?;
for entry in BufReader::new(pls_file).lines() {
let entry = entry?;
let new = renames.get(&entry).unwrap_or(&entry).to_string();
outputs.push(new);
}
let data = outputs.join("\n");
fs::write(filename, data).expect("unable to write");
Ok(())
}
fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
println!("At least two arguments are required");
process::exit(1);
}
let rename_file = &args[1];
let file = File::open(rename_file)?;
let mut renames = HashMap::new();
for line in BufReader::new(file).lines() {
let line = line?;
let split: Vec<&str> = line.split("\t\t").collect();
renames.insert(split[0].to_string(), split[1].to_string());
}
for r in &args[2..] {
try!(apply_renames(&r, &renames));
/*
match apply_renames(&r, &renames) {
Ok(()) => println!("Successfully processed renames for {}", r)
},
_ => println!("Error processing {}", r);
*/
}
Ok(())
}