Skip to content

Instantly share code, notes, and snippets.

@marioidival
Forked from steveklabnik/main.rs
Created October 26, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioidival/930555ea24ea1e00ffcb07e1c35877ee to your computer and use it in GitHub Desktop.
Save marioidival/930555ea24ea1e00ffcb07e1c35877ee to your computer and use it in GitHub Desktop.
The Results of the Expressive C++17 Coding Challenge in Rust
use std::env;
use std::io;
use std::io::prelude::*;
use std::fs::File;
#[derive(Debug)]
enum Error {
Io(io::Error),
Program(&'static str),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Io(e)
}
}
impl From<&'static str> for Error {
fn from(e: &'static str) -> Error {
Error::Program(e)
}
}
fn main() {
let mut args = env::args();
// skip the program name
args.next().unwrap();
let filename = args.next().unwrap();
let column_name = args.next().unwrap();
let replacement = args.next().unwrap();
let output_filename = args.next().unwrap();
let csv_data = load_csv(&filename).unwrap();
let modified_data = replace_column(csv_data, &column_name, &replacement).unwrap();
write_csv(&modified_data, &output_filename).unwrap();
}
fn load_csv(filename: &str) -> Result<String, Error> {
let mut f = File::open(filename)?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
if buffer.is_empty() {
return Err("input file missing")?
}
Ok(buffer)
}
fn replace_column(data: String, column: &str, replacement: &str) -> Result<String, Error> {
let mut lines = data.lines();
let columns = lines.next().unwrap();
let columns: Vec<&str> = columns.split(',').collect();
let column_number = columns.iter().position(|&e| e == column);
let column_number = match column_number {
Some(column) => column,
None => Err("column name doesn’t exist in the input file")?
};
let mut result = String::with_capacity(data.capacity());
result.push_str(&columns.join(","));
result.push('\n');
for line in lines {
let mut records: Vec<&str> = line.split(',').collect();
records[column_number] = replacement;
result.push_str(&records.join(","));
result.push('\n');
}
Ok(result)
}
fn write_csv(data: &str, filename: &str) -> Result<(), Error> {
let mut buffer = File::create(filename)?;
buffer.write_all(data.as_bytes())?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment