Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2017 16:17
Show Gist options
  • Save tmcw/139a6219254b547616611dcb79dd0858 to your computer and use it in GitHub Desktop.
Save tmcw/139a6219254b547616611dcb79dd0858 to your computer and use it in GitHub Desktop.
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 {
positions: vec![(song_entry.position.clone())],
println!("Extracting values");
let mut song_timelines = vec![];
for val in song_map.values() {
println!("Writing output");
let reduced = serde_json::to_string(&song_timelines).unwrap();
let mut file = File::create("reduced.rust.json").unwrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment