pub fn build_sample(
    file: impl Write,
    line_length: LineLength,
    sample_length: SampleLength,
    character_generator: impl Fn() -> char,
) {
    // Let's buffer our writer, since we'll make lots of small writes.
    let mut file = BufWriter::new(file);

    match sample_length {
        SampleLength::Lines(n) => {
            for _ in 0..n {
                let line = build_line(&line_length, &character_generator);
                file.write_all(line.string.as_bytes()).unwrap();
            }
        }
        SampleLength::Characters(limit) => {
            let mut chars_written = 0;

            // Calculate the upper bound on the length of a line so that we can handle the last
            // line specially and ensure that we get the overall file length just right.
            let max_line_length = match line_length {
                LineLength::Fixed(n) => n + 1,     // +1 for newline.
                LineLength::Range(ref r) => r.end, // Range is half open, so no need for +1.
            };

            // Be careful not to subtract from limit here or you'll get subtract with overflow.
            while chars_written + max_line_length < limit {
                let line = build_line(&line_length, &character_generator);
                chars_written += line.length;
                file.write_all(line.string.as_bytes()).unwrap();
            }

            // Write the last line.
            if chars_written < limit {
                let line = build_line(
                    &LineLength::Fixed(limit - chars_written - 1),
                    &character_generator,
                );
                file.write_all(line.string.as_bytes()).unwrap();
            }
        }
    }

    // For safety.
    file.flush().unwrap();
}
/// Builds a sample based on the provided parameters and writes it to `file`.
Builds a sample based on the provided parameters and writes it to `file`.
Builds a sample based on the provided parameters and writes it to ` file ` .
pub fn build_sample(
    file: impl Write,
    line_length: LineLength,
    sample_length: SampleLength,
    character_generator: impl Fn() -> char,
) {
    // Let's buffer our writer, since we'll make lots of small writes.
    let mut file = BufWriter::new(file);
    match sample_length {
        SampleLength::Lines(n) => {
            for _ in 0..n {
                let line = build_line(&line_length, &character_generator);
                file.write_all(line.string.as_bytes()).unwrap();
            }
        }
        SampleLength::Characters(limit) => {
            let mut chars_written = 0;
            // Calculate the upper bound on the length of a line so that we can handle the last
            // line specially and ensure that we get the overall file length just right.
            let max_line_length = match line_length {
                LineLength::Fixed(n) => n + 1,     // +1 for newline.
                LineLength::Range(ref r) => r.end, // Range is half open, so no need for +1.
            };
            // Be careful not to subtract from limit here or you'll get subtract with overflow.
            while chars_written + max_line_length < limit {
                let line = build_line(&line_length, &character_generator);
                chars_written += line.length;
                file.write_all(line.string.as_bytes()).unwrap();
            }
            // Write the last line.
            if chars_written < limit {
                let line = build_line(
                    &LineLength::Fixed(limit - chars_written - 1),
                    &character_generator,
                );
                file.write_all(line.string.as_bytes()).unwrap();
            }
        }
    }
    // For safety.
    file.flush().unwrap();
}
pub fn build_sample ( file : impl Write , line_length : LineLength , sample_length : SampleLength , character_generator : impl Fn ( ) -> char , ) { // Let's buffer our writer, since we'll make lots of small writes. let mut file = BufWriter :: new ( file ) ; match sample_length { SampleLength :: Lines ( n ) => { for _ in 0 .. n { let line = build_line ( & line_length , & character_generator ) ; file . write_all ( line . string . as_bytes ( ) ) . unwrap ( ) ; } } SampleLength :: Characters ( limit ) => { let mut chars_written = 0 ; // Calculate the upper bound on the length of a line so that we can handle the last // line specially and ensure that we get the overall file length just right. let max_line_length = match line_length { LineLength :: Fixed ( n ) => n + 1 , // +1 for newline. LineLength :: Range ( ref r ) => r . end , // Range is half open, so no need for +1. } ; // Be careful not to subtract from limit here or you'll get subtract with overflow. while chars_written + max_line_length < limit { let line = build_line ( & line_length , & character_generator ) ; chars_written += line . length ; file . write_all ( line . string . as_bytes ( ) ) . unwrap ( ) ; } // Write the last line. if chars_written < limit { let line = build_line ( & LineLength :: Fixed ( limit - chars_written - 1 ) , & character_generator , ) ; file . write_all ( line . string . as_bytes ( ) ) . unwrap ( ) ; } } } // For safety. file . flush ( ) . unwrap ( ) ; }
Builds a sample based on the provided parameters and writes it to `file`.
Builds a sample based on the provided parameters and writes it to ` file ` .
file
impl Write
line_length
LineLength
sample_length
SampleLength
character_generator
impl Fn() -> char
file
impl Write
line_length
LineLength
sample_length
SampleLength
character_generator
impl Fn() -> char