<commit_before>use std::io::IoResult;

use collections::HashMap;
use serialize::{json, Encodable, Encoder};

use backend::Backend;

pub struct JsonBackend {
    pretty: bool,

    entries: Vec<Entry>,
}

#[deriving(Encodable)]
struct Entry {
    ty: ~str,
    value: ~str,
}

impl JsonBackend {
    pub fn new() -> JsonBackend {
        JsonBackend {
            pretty: false,

            entries: Vec::new(),
        }
    }
}

impl Backend for JsonBackend {
    fn configure(&mut self, vars: &HashMap<~str, ~str>) -> Result<(), ~str> {
        match vars.find(&~"pretty") {
            Some(ref value) => {
                self.pretty = match from_str(value.as_slice()) {
                    Some(b) => b,
                    None => {
                        return Err(format!("Bad value for pretty: {}", value));
                    }
                }
            }
            None => {}
        }

        Ok(())
    }

    fn header(&mut self, _w: &mut Writer) -> IoResult<()> {
        Ok(())
    }

    fn code_start(&mut self, _w: &mut Writer) -> IoResult<()> {
        Ok(())
    }

    fn code_end(&mut self, w: &mut Writer) -> IoResult<()> {
        if self.pretty {
            let mut encoder = json::PrettyEncoder::new(w);
            self.entries.encode(&mut encoder);
        } else {
            let mut encoder = json::Encoder::new(w);
            self.entries.encode(&mut encoder);
        }

        Ok(())
    }

    fn start(&mut self, _w: &mut Writer, ty: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"start",
            value: ty.to_owned(),
        });

        Ok(())
    }

    fn end(&mut self, _w: &mut Writer, ty: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"end",
            value: ty.to_owned(),
        });

        Ok(())
    }

    fn text(&mut self, _w: &mut Writer, text: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"text",
            value: text.to_owned(),
        });

        Ok(())
    }
}
<commit_msg>Add JSON header: The output is the map associating colors name and value.<commit_after>use std::io::IoResult;

use collections::HashMap;
use serialize::{json, Encodable, Encoder};

use backend::Backend;
use colors;

pub struct JsonBackend {
    pretty: bool,

    entries: Vec<Entry>,
}

#[deriving(Encodable)]
struct Entry {
    ty: ~str,
    value: ~str,
}

impl JsonBackend {
    pub fn new() -> JsonBackend {
        JsonBackend {
            pretty: false,

            entries: Vec::new(),
        }
    }
}

impl Backend for JsonBackend {
    fn configure(&mut self, vars: &HashMap<~str, ~str>) -> Result<(), ~str> {
        match vars.find(&~"pretty") {
            Some(ref value) => {
                self.pretty = match from_str(value.as_slice()) {
                    Some(b) => b,
                    None => {
                        return Err(format!("Bad value for pretty: {}", value));
                    }
                }
            }
            None => {}
        }

        Ok(())
    }

    fn header(&mut self, w: &mut Writer) -> IoResult<()> {
        let colors = colors::get_colors();

        if self.pretty {
            let mut encoder = json::PrettyEncoder::new(w);
            colors.encode(&mut encoder);
        } else {
            let mut encoder = json::Encoder::new(w);
            colors.encode(&mut encoder);
        }

        Ok(())
    }

    fn code_start(&mut self, _w: &mut Writer) -> IoResult<()> {
        Ok(())
    }

    fn code_end(&mut self, w: &mut Writer) -> IoResult<()> {
        if self.pretty {
            let mut encoder = json::PrettyEncoder::new(w);
            self.entries.encode(&mut encoder);
        } else {
            let mut encoder = json::Encoder::new(w);
            self.entries.encode(&mut encoder);
        }

        Ok(())
    }

    fn start(&mut self, _w: &mut Writer, ty: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"start",
            value: ty.to_owned(),
        });

        Ok(())
    }

    fn end(&mut self, _w: &mut Writer, ty: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"end",
            value: ty.to_owned(),
        });

        Ok(())
    }

    fn text(&mut self, _w: &mut Writer, text: &str) -> IoResult<()> {
        self.entries.push(Entry {
            ty: ~"text",
            value: text.to_owned(),
        });

        Ok(())
    }
}

{'fraction_non_alphanumeric': 0.16037735849056603, 'fraction_numerical': 0.0, 'mean_word_length': 1.6101992966002345, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, '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': '7032920', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1372, 'n_words': 330}