Skip to content

Instantly share code, notes, and snippets.

@tmcw tmcw/main.rs
Created Feb 26, 2017

Embed
What would you like to do?
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde_json::Value;
use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use std::fs;
use std::collections::HashMap;
use std::cell::RefCell;
#[derive(Serialize, Deserialize, Debug)]
struct Song {
artist: String,
title: String,
}
struct SongEntry {
song: Song,
key: String,
position: (String, u32),
}
#[derive(Serialize, Deserialize, Debug)]
struct SongTimeline {
song: Song,
positions: Vec<(String, u32)>,
}
fn main() {
let paths = fs::read_dir("./charts/").unwrap();
let songs = paths.flat_map(|path| {
let p = path.unwrap().path();
let file = File::open(&p).unwrap();
let v: Vec<Song> = serde_json::from_reader(file).unwrap();
v.into_iter().enumerate().map(move |(i, song)| {
let date = p.file_stem().unwrap().to_str().unwrap().to_string();
SongEntry {
key: format!("{}{}", song.artist, song.title),
song: song,
position: (date, i as u32),
}
})
});
let mut song_map: HashMap<String, SongTimeline> = HashMap::new();
println!("Indexing songs");
for (i, song_entry) in songs.enumerate() {
let key = song_entry.key.clone();
let pos = song_map.entry(key).or_insert(SongTimeline {
song: song_entry.song,
positions: vec![(song_entry.position.clone())],
});
pos.positions.push(song_entry.position);
}
println!("Extracting values");
let mut song_timelines = vec![];
for val in song_map.values() {
song_timelines.push(val);
}
println!("Writing output");
let reduced = serde_json::to_string(&song_timelines).unwrap();
let mut file = File::create("reduced.rust.json").unwrap();
file.write_all(reduced.as_bytes());
println!("Done!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.