Last active
December 30, 2016 19:36
-
-
Save ttdonovan/270e13fa4ee2ccfc39ceafd4a3b8c697 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate csv; | |
use std::collections::HashMap; | |
use std::sync::Arc; | |
mod rows { | |
pub type Category = (String, String); | |
pub type Shirt = (String, String, String); | |
} | |
#[derive(Debug)] | |
struct Category { | |
uid: String, | |
name: String, | |
} | |
#[derive(Debug)] | |
struct Shirt { | |
uid: String, | |
name: String, | |
category: Arc<Category>, | |
} | |
#[derive(Debug)] | |
struct Catalog { | |
categories: Vec<Arc<Category>>, | |
shirts: HashMap<String, Shirt>, | |
} | |
impl Catalog { | |
pub fn new() -> Catalog { | |
let mut this = Catalog { | |
categories: vec![], | |
shirts: HashMap::new(), | |
}; | |
this.load_categories(); | |
this.load_shirts(); | |
this | |
} | |
fn load_categories(&mut self) { | |
let category_data = " | |
01,Graphic Tees | |
02,Dress Shirts | |
03,Casual Shirts"; | |
let mut category_rdr = csv::Reader::from_string(category_data).has_headers(false); | |
let categories = category_rdr.decode().collect::<csv::Result<Vec<rows::Category>>>().unwrap(); | |
for category in categories { | |
self.categories.push(Arc::new(Category { | |
uid: String::from(category.0), | |
name: String::from(category.1), | |
})); | |
} | |
} | |
fn load_shirts(&mut self) { | |
let shirt_data = " | |
0A,03,Flannel Checkered Shirt | |
0B,03,Classic Plaid Flannel Shirt | |
0C,02,Wrinkle-Resistant Standard Fit Shirt | |
0D,01,Bay Area Record Graphic Tee | |
0E,01,NYC Brick Crewneck Tee"; | |
let mut shirt_rdr = csv::Reader::from_string(shirt_data).has_headers(false); | |
let shirts = shirt_rdr.decode().collect::<csv::Result<Vec<rows::Shirt>>>().unwrap(); | |
for shirt in shirts { | |
self.shirts.insert(String::from(shirt.0.clone()), Shirt { | |
uid: String::from(shirt.0), | |
name: String::from(shirt.1), | |
category: self.categories.get(0).expect("whoops").clone(), | |
}); | |
} | |
} | |
} | |
fn main() { | |
println!("Shirts! 👔"); | |
let catalog = Catalog::new(); | |
println!("{:?}", catalog); | |
println!("\n'A0': {:?}", catalog.shirts.get("0A").expect("no shirt")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment