<commit_before>use mio::{Buf, MutBuf};
use std::mem;
use std::io::Write;
use std::cmp;
use std::str;
use std::fmt;
use tendril;
use queue_backend::Message;

pub type ByteTendril = tendril::Tendril<tendril::fmt::Bytes, tendril::Atomic>;
pub type StrTendril = tendril::Tendril<tendril::fmt::UTF8, tendril::Atomic>;

#[derive(Eq, PartialEq, Debug)]
pub enum ProtocolError {
    Incomplete,
    Invalid(&'static str),
}

impl From<&'static str> for ProtocolError {
    fn from(from: &'static str) -> ProtocolError {
        ProtocolError::Invalid(from)
    }
}

pub type ProtocolResult<T> = Result<T, ProtocolError>;

#[derive(Clone)]
pub enum Value {
    Nil,
    Int(i64),
    Data(ByteTendril),
    Array(Vec<Value>),
    Status(StrTendril),
    Error(StrTendril),
    Message((u64, Message)),
}

impl Value {
    fn serialize_to(self, f: &mut Vec<u8>) {
        match self {
            Value::Nil =>
                write!(f, "$-1\r\n"),
            Value::Int(v) =>
                write!(f, ":{}\r\n", v),
            Value::Data(v) => {
                write!(f, "${}\r\n", v.len32()).unwrap();
                f.write_all(v.as_ref()).unwrap();
                write!(f, "\r\n")
            }
            Value::Array(a) => {
                write!(f, "*{}\r\n", a.len()).unwrap();
                for v in a {
                    v.serialize_to(f);
                }
                Ok(())
            },
            Value::Status(v) =>
                write!(f, "+{}\r\n", v.as_ref()),
            Value::Error(v) =>
                write!(f, "-{}\r\n", v.as_ref()),
            Value::Message((ticket, message)) => {
                write!(f, "*2\r\n").unwrap();
                write!(f, ":{}\r\n${}\r\n", ticket, message.body().len()).unwrap();
                f.write(message.body()).unwrap();
                write!(f, "\r\n")
            }
        }.unwrap()
    }
}

impl fmt::Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Value::Nil =>
                write!(f, "Nil"),
            Value::Int(v) =>
                write!(f, "Int({:?})", v),
            Value::Data(ref v) =>
                match str::from_utf8(v) {
                    Ok(s) => write!(f, "Data({:?})", s),
                    Err(_) => write!(f, "Data({:?})", v.as_ref())
                },
            Value::Array(ref b) => {
                try!(write!(f, "Array("));
                try!(f.debug_list().entries(b).finish());
                write!(f, ")")<commit_msg>use write_all to write to buffer<commit_after>use mio::{Buf, MutBuf};
use std::mem;
use std::io::Write;
use std::cmp;
use std::str;
use std::fmt;
use tendril;
use queue_backend::Message;

pub type ByteTendril = tendril::Tendril<tendril::fmt::Bytes, tendril::Atomic>;
pub type StrTendril = tendril::Tendril<tendril::fmt::UTF8, tendril::Atomic>;

#[derive(Eq, PartialEq, Debug)]
pub enum ProtocolError {
    Incomplete,
    Invalid(&'static str),
}

impl From<&'static str> for ProtocolError {
    fn from(from: &'static str) -> ProtocolError {
        ProtocolError::Invalid(from)
    }
}

pub type ProtocolResult<T> = Result<T, ProtocolError>;

#[derive(Clone)]
pub enum Value {
    Nil,
    Int(i64),
    Data(ByteTendril),
    Array(Vec<Value>),
    Status(StrTendril),
    Error(StrTendril),
    Message((u64, Message)),
}

impl Value {
    fn serialize_to(self, f: &mut Vec<u8>) {
        match self {
            Value::Nil =>
                write!(f, "$-1\r\n"),
            Value::Int(v) =>
                write!(f, ":{}\r\n", v),
            Value::Data(v) => {
                write!(f, "${}\r\n", v.len32()).unwrap();
                f.write_all(v.as_ref()).unwrap();
                write!(f, "\r\n")
            }
            Value::Array(a) => {
                write!(f, "*{}\r\n", a.len()).unwrap();
                for v in a {
                    v.serialize_to(f);
                }
                Ok(())
            },
            Value::Status(v) =>
                write!(f, "+{}\r\n", v.as_ref()),
            Value::Error(v) =>
                write!(f, "-{}\r\n", v.as_ref()),
            Value::Message((ticket, message)) => {
                write!(f, "*2\r\n:{}\r\n:${}\r\n", ticket, message.body().len()).unwrap();
                f.write_all(message.body()).unwrap();
                write!(f, "\r\n")
            }
        }.unwrap()
    }
}

impl fmt::Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Value::Nil =>
                write!(f, "Nil"),
            Value::Int(v) =>
                write!(f, "Int({:?})", v),
            Value::Data(ref v) =>
                match str::from_utf8(v) {
                    Ok(s) => write!(f, "Data({:?})", s),
                    Err(_) => write!(f, "Data({:?})", v.as_ref())
                },
            Value::Array(ref b) => {
                try!(write!(f, "Array("));
                try!(f.debug_list().entries(b).finish());
                write!(f, ")")
{'fraction_non_alphanumeric': 0.2036082474226804, 'fraction_numerical': 0.0043616177636796195, 'mean_word_length': 1.7523186033824332, 'pattern_counts': {'":': 3, '<': 19, '<?xml version=': 0, '>': 49, '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': '3463006', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1653, 'n_words': 313}