<commit_before>use std::io;

use crate::error::*;
use crate::types::*;
use std::fmt;

pub struct Writer<R> {
    buffer: R,
}

impl<R: io::Write> Writer<R> {
    pub fn new(inner: R) -> Self {
        Writer { buffer: inner }
    }

    pub fn write_snapshot<T: fmt::Display>(&mut self, snapshot: &Snapshot<T>) -> Result<()> {
        self.buffer.write(snapshot.size().to_string().as_bytes())?;
        self.buffer.write(b"\n")?;
        self.buffer.write(snapshot.comment.as_bytes())?;
        self.buffer.write(b"\n")?;
        for atom in &snapshot.atoms {
            self.buffer.write(format!("{}", atom).as_bytes())?;
            self.buffer.write(b"\n")?;
        }
        self.buffer.flush()?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_write() {
        let mut buffer: Vec<u8> = vec![];
        {
            let mut writer = Writer::new(&mut buffer);
            let result = writer.write_snapshot(&Snapshot {
                comment: "comment".to_string(),
                atoms: vec![
                    Atom::new("C", 10.2, 13.4, 8.9),<commit_msg>:recycle: Use writeln! format for Writer<commit_after>use std::io;

use crate::error::*;
use crate::types::*;
use std::fmt;

pub struct Writer<R> {
    buffer: R,
}

impl<R: io::Write> Writer<R> {
    pub fn new(inner: R) -> Self {
        Writer { buffer: inner }
    }

    pub fn write_snapshot<T>(&mut self, snapshot: &Snapshot<T>) -> Result<()>
    where
        T: fmt::Display,
    {
        writeln!(self.buffer, "{}", snapshot.size())?;
        writeln!(self.buffer, "{}", snapshot.comment)?;
        for atom in &snapshot.atoms {
            writeln!(self.buffer, "{}", atom)?;
        }
        self.buffer.flush()?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_write() {
        let mut buffer: Vec<u8> = vec![];
        {
            let mut writer = Writer::new(&mut buffer);
            let result = writer.write_snapshot(&Snapshot {
                comment: "comment".to_string(),
                atoms: vec![
                    Atom::new("C", 10.2, 13.4, 8.9),
{'fraction_non_alphanumeric': 0.18483855872718766, 'fraction_numerical': 0.008423022929340197, 'mean_word_length': 2.125730994152047, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '1819972', 'n_tokens_mistral': 714, 'n_tokens_neox': 719, 'n_words': 153}