<commit_before>use std::error::Error;
use std::fs;
use std::io::prelude::*;

extern crate hyper;
use self::hyper::client::Client;

extern crate yaml_rust;
use self::yaml_rust::{Yaml, YamlLoader};

type Schema = Yaml;
type SchemaResult = Result<Schema, Box<Error>>;

fn local_copy_location(schema_name: &str) -> String {
    fs::create_dir_all("tapioca-schemata");
    format!("tapioca-schemata/{}.yml", schema_name)
}

pub(super) fn parse_schema<'a>(schema_name: &'a str) -> SchemaResult {
    let mut file = fs::File::open(local_copy_location(schema_name))?;
    let mut schema_str = String::new();
    file.read_to_string(&mut schema_str)?;

    let docs = YamlLoader::load_from_str(schema_str.as_ref())?;
    Ok(docs[0].clone())
}

pub(super) fn fetch_schema<'a>(schema_name: &'a str, schema_url: &'a str) -> SchemaResult {
    let client = Client::new();
    let mut file = fs::File::create(local_copy_location(schema_name))?;

    let mut buf = String::new();
    client.get(schema_url).send()?
        .read_to_string(&mut buf);

    file.write_all(buf.as_ref())?;
    let docs = YamlLoader::load_from_str(buf.as_ref())?;
    Ok(docs[0].clone())
}<commit_msg>Refactor YAML parsing<commit_after>use std::error::Error;
use std::fs;
use std::io::prelude::*;

extern crate hyper;
use self::hyper::client::Client;

extern crate yaml_rust;
use self::yaml_rust::{Yaml, YamlLoader};

type Schema = Yaml;
type SchemaResult = Result<Schema, Box<Error + Send + Sync>>;

fn local_copy_location(schema_name: &str) -> String {
    fs::create_dir_all("tapioca-schemata");
    format!("tapioca-schemata/{}.yml", schema_name)
}

fn parse_first_doc(name: &str, buf: &str) -> SchemaResult {
    let docs = YamlLoader::load_from_str(buf.as_ref())?;
    if docs.len() == 0 {
        Err(From::from(format!("Could not parse YAML for {}", name)))
    } else {
        Ok(docs[0].clone())
    }
}

pub(super) fn parse_schema(schema_name: &str) -> SchemaResult {
    let mut file = fs::File::open(local_copy_location(schema_name))?;
    let mut buf = String::new();

    file.read_to_string(&mut buf)?;
    parse_first_doc(&schema_name, &buf)
}

pub(super) fn fetch_schema(schema_name: &str, schema_url: &str) -> SchemaResult {
    let mut file = fs::File::create(local_copy_location(schema_name))?;
    let mut buf = String::new();
    client.get(schema_url).send()?
        .read_to_string(&mut buf);

    file.write_all(buf.as_ref())?;
        parse_first_doc(&schema_name, &buf)
}
{'fraction_non_alphanumeric': 0.17119675456389452, 'fraction_numerical': 0.0016227180527383367, 'mean_word_length': 4.871428571428571, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4910069', 'n_tokens_mistral': 894, 'n_tokens_neox': 882, 'n_words': 172}