Skip to main content

generate

Function generate 

Source
pub fn generate<F>(
    headers: &[&str],
    rows: usize,
    seed: u64,
    row_factory: F,
) -> String
where F: FnMut(&mut Rng) -> Vec<String>,
Expand description

Generate a CSV string with rows rows, one per Vec<String> produced by row_factory. The first line is the header.

Field values containing ,, ", \n, or \r are escaped per RFC 4180: the value is wrapped in double quotes and any internal " is doubled. Values without those characters pass through verbatim.

Header values are escaped the same way.

§Example

use dev_fixtures::mock::csv::generate;
let csv = generate(
    &["id", "name"],
    3,
    42,
    |rng| vec![rng.range(1000).to_string(), format!("name_{}", rng.range(100))],
);
assert!(csv.starts_with("id,name\n"));
assert_eq!(csv.lines().count(), 4); // 1 header + 3 rows

§Escaping example

use dev_fixtures::mock::csv::generate;
let csv = generate(&["a", "b"], 1, 0, |_rng| {
    vec![r#"contains, comma"#.into(), r#"has "quotes""#.into()]
});
// Both fields are wrapped; the quote inside is doubled.
assert!(csv.contains("\"contains, comma\""));
assert!(csv.contains("\"has \"\"quotes\"\"\""));