rust-mv3u/src/main.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2018-10-28 11:24:37 +01:00
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::collections::HashMap;
2018-11-10 22:18:28 +01:00
use std::env;
2018-10-28 11:24:37 +01:00
use std::fs;
2018-11-10 22:18:28 +01:00
use std::process;
2018-10-28 11:24:37 +01:00
fn main() -> Result<()> {
2018-11-10 22:18:28 +01:00
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 playlist_file = &args[2];
println!("{:?}", rename_file);
let file = File::open(rename_file)?;
2018-10-28 11:24:37 +01:00
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());
}
let mut outputs = Vec::new();
2018-10-28 11:24:37 +01:00
2018-11-10 22:18:28 +01:00
let pls_file = File::open(playlist_file)?;
2018-10-28 11:24:37 +01:00
for entry in BufReader::new(pls_file).lines() {
let entry = entry?;
let new = renames.get(&entry).unwrap_or(&entry).to_string();
/*
if new != &entry {
println!("renamed “{}” to “{}", entry, new);
} else {
println!("{}” remains unchanged", entry);
}
*/
outputs.push(new);
2018-10-28 11:24:37 +01:00
}
let data = outputs.join("\n");
fs::write("playlist2.m3u", data).expect("unable to write");
Ok(())
}