<commit_before>use data::{Compression, NBT, NBTFile};
use errors::Result;

use std::io::Write;

use byteorder::{BigEndian, WriteBytesExt};

use flate2;
use flate2::write::{GzEncoder, ZlibEncoder};

macro_rules! compression_level {
    () => { flate2::Compression::default() };
}

/// Given an NBT file, write it as a binary NBT file to the writer
pub fn write_file<W: Write>(w: &mut W, file: &NBTFile) -> Result<()> {
    let map = match file.root {
        NBT::Compound(ref x) => x,
        _ => unreachable!(),
    };

    match file.compression {
        Compression::None => write_compound(w, &map, false)?,
        Compression::Gzip => {
            let mut w = GzEncoder::new(w, compression_level!());
            write_compound(&mut w, map, false)?;
            let _: &mut W = w.finish()?;
        },
        Compression::Zlib => {
            let mut w = ZlibEncoder::new(w, compression_level!());
            write_compound(&mut w, map, false)?;
            let _: &mut W = w.finish()?;
        },
    }

    Ok(())
}

fn write_tag<W: Write>(w: &mut W, tag: &NBT) -> Result<()> {
    match tag {
        &NBT::End => panic!("Unable to write End tag"),
        &NBT::Byte(x) => write_byte(w, x),
        &NBT::Short(x) => write_short(w, x),
        &NBT::Int(x) => write_int(w, x),
        &NBT::Long(x) => write_long(w, x),
        &NBT::Float(x) => write_float(w, x),
        &NBT::Double(x) => write_double(w, x),
        &NBT::ByteArray(ref x) => write_byte_array(w, x),<commit_msg>Change an accidental panic into a bail<commit_after>use data::{Compression, NBT, NBTFile};
use errors::Result;

use std::io::Write;

use byteorder::{BigEndian, WriteBytesExt};

use flate2;
use flate2::write::{GzEncoder, ZlibEncoder};

macro_rules! compression_level {
    () => { flate2::Compression::default() };
}

/// Given an NBT file, write it as a binary NBT file to the writer
pub fn write_file<W: Write>(w: &mut W, file: &NBTFile) -> Result<()> {
    let map = match file.root {
        NBT::Compound(ref x) => x,
        _ => unreachable!(),
    };

    match file.compression {
        Compression::None => write_compound(w, &map, false)?,
        Compression::Gzip => {
            let mut w = GzEncoder::new(w, compression_level!());
            write_compound(&mut w, map, false)?;
            let _: &mut W = w.finish()?;
        },
        Compression::Zlib => {
            let mut w = ZlibEncoder::new(w, compression_level!());
            write_compound(&mut w, map, false)?;
            let _: &mut W = w.finish()?;
        },
    }

    Ok(())
}

fn write_tag<W: Write>(w: &mut W, tag: &NBT) -> Result<()> {
    match tag {
        &NBT::End => bail!("Unable to write End tag"),
        &NBT::Byte(x) => write_byte(w, x),
        &NBT::Short(x) => write_short(w, x),
        &NBT::Int(x) => write_int(w, x),
        &NBT::Long(x) => write_long(w, x),
        &NBT::Float(x) => write_float(w, x),
        &NBT::Double(x) => write_double(w, x),
        &NBT::ByteArray(ref x) => write_byte_array(w, x),
{'fraction_non_alphanumeric': 0.1836870026525199, 'fraction_numerical': 0.001989389920424403, 'mean_word_length': 2.7292954264524103, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '597846', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1080, 'n_words': 251}