<commit_before>use ffi;
use error::YamlError;
use event::{YamlVersionDirective, YamlTagDirective};

use std::ptr;
use std::mem;
use std::c_vec::CVec;
use std::c_str::CString;
use std::io::IoError;
use libc;

pub struct YamlBaseEmitter {
    emitter_mem: ffi::yaml_emitter_t
}

impl YamlBaseEmitter {
    unsafe fn new() -> YamlBaseEmitter {
        YamlBaseEmitter {
            emitter_mem: mem::uninitialized()
        }
    }
}

impl Drop for YamlBaseEmitter {
    fn drop(&mut self) {
        unsafe {
            ffi::yaml_emitter_delete(&mut self.emitter_mem);
        }
    }
}

pub struct YamlEmitter<'r> {
    base_emitter: YamlBaseEmitter,
    writer: &'r mut Writer+'r,
    io_error: Option<IoError>,
}

impl<'r> YamlEmitter<'r> {
    pub fn init<'r>(writer: &'r mut Writer) -> Box<YamlEmitter<'r>> {
        unsafe {
            let mut emitter = box YamlEmitter {
                base_emitter: YamlBaseEmitter::new(),
                writer: writer,
                io_error: None
            };

            if ffi::yaml_emitter_initialize(&mut emitter.base_emitter.emitter_mem) == 0 {
                panic!("failed to initialize yaml_emitter_t");
            }

            ffi::yaml_emitter_set_output(&mut emitter.base_emitter.emitter_mem, handle_writer_cb, mem::transmute(&mut *emitter));

            emitter
        }
    }

    fn get_error(&mut self) -> YamlError {
        let emitter_mem = &self.base_emitter.emitter_mem;
        unsafe {
            let mut error = YamlError {
                kind: emitter_mem.error,
                problem: CString::new(emitter_mem.problem, false).as_str().map(|s| s.to_string()),
                io_error: None,
                context: None
            };

            mem::swap(&mut self.io_error, &mut error.io_error);

            return error;
        }
    }

    pub fn emit_stream(&mut self,
                    encoding: ffi::YamlEncoding,
                    f: |&mut YamlEmitter| -> Result<(), YamlError>)
        -> Result<(), YamlError>
    {
        try!(self.emit_stream_start_event(encoding));
        try!(f(self));
        self.emit_stream_end_event()
    }

    fn emit_stream_start_event(&mut self, encoding: ffi::YamlEncoding) -> Result<(), YamlError> {
        unsafe {
            let mut event = mem::uninitialized();

            if ffi::yaml_stream_start_event_initialize(&mut event, encoding) == 0 {<commit_msg>Force flush at the end of emit_stream<commit_after>use ffi;
use error::YamlError;
use event::{YamlVersionDirective, YamlTagDirective};

use std::ptr;
use std::mem;
use std::c_vec::CVec;
use std::c_str::CString;
use std::io::IoError;
use libc;

pub struct YamlBaseEmitter {
    emitter_mem: ffi::yaml_emitter_t
}

impl YamlBaseEmitter {
    unsafe fn new() -> YamlBaseEmitter {
        YamlBaseEmitter {
            emitter_mem: mem::uninitialized()
        }
    }
}

impl Drop for YamlBaseEmitter {
    fn drop(&mut self) {
        unsafe {
            ffi::yaml_emitter_delete(&mut self.emitter_mem);
        }
    }
}

pub struct YamlEmitter<'r> {
    base_emitter: YamlBaseEmitter,
    writer: &'r mut Writer+'r,
    io_error: Option<IoError>,
}

impl<'r> YamlEmitter<'r> {
    pub fn init<'r>(writer: &'r mut Writer) -> Box<YamlEmitter<'r>> {
        unsafe {
            let mut emitter = box YamlEmitter {
                base_emitter: YamlBaseEmitter::new(),
                writer: writer,
                io_error: None
            };

            if ffi::yaml_emitter_initialize(&mut emitter.base_emitter.emitter_mem) == 0 {
                panic!("failed to initialize yaml_emitter_t");
            }

            ffi::yaml_emitter_set_output(&mut emitter.base_emitter.emitter_mem, handle_writer_cb, mem::transmute(&mut *emitter));

            emitter
        }
    }

    fn get_error(&mut self) -> YamlError {
        let emitter_mem = &self.base_emitter.emitter_mem;
        unsafe {
            let mut error = YamlError {
                kind: emitter_mem.error,
                problem: CString::new(emitter_mem.problem, false).as_str().map(|s| s.to_string()),
                io_error: None,
                context: None
            };

            mem::swap(&mut self.io_error, &mut error.io_error);

            return error;
        }
    }

    pub fn emit_stream(&mut self,
                    encoding: ffi::YamlEncoding,
                    f: |&mut YamlEmitter| -> Result<(), YamlError>)
        -> Result<(), YamlError>
    {
        try!(self.emit_stream_start_event(encoding));
        try!(f(self));
        try!(self.emit_stream_end_event());
        self.flush()
    }

    fn emit_stream_start_event(&mut self, encoding: ffi::YamlEncoding) -> Result<(), YamlError> {
        unsafe {
            let mut event = mem::uninitialized();

            if ffi::yaml_stream_start_event_initialize(&mut event, encoding) == 0 {
{'fraction_non_alphanumeric': 0.12046952224052718, 'fraction_numerical': 0.0008237232289950577, 'mean_word_length': 2.2186878727634194, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '4773778', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1510, 'n_words': 307}