crdt_testdata/
lib.rs

1use libflate::gzip::Decoder;
2use serde::Deserialize;
3use std::fs::File;
4use std::io::{BufReader, Read};
5
6/// This file contains some simple helpers for loading test data. Its used by benchmarking and
7/// testing code.
8///
9/// (position, delete length, insert content).
10#[derive(Debug, Clone, Deserialize)]
11pub struct TestPatch(pub usize, pub usize, pub String);
12
13#[derive(Debug, Clone, Deserialize)]
14pub struct TestTxn {
15    // time: String, // ISO String. Unused.
16    pub patches: Vec<TestPatch>,
17}
18
19#[derive(Debug, Clone, Deserialize)]
20pub struct TestData {
21    #[serde(rename = "startContent")]
22    pub start_content: String,
23    #[serde(rename = "endContent")]
24    pub end_content: String,
25
26    pub txns: Vec<TestTxn>,
27}
28
29impl TestData {
30    pub fn len(&self) -> usize {
31        self.txns.iter().map(|txn| txn.patches.len()).sum::<usize>()
32    }
33
34    pub fn is_empty(&self) -> bool {
35        !self.txns.iter().any(|txn| !txn.patches.is_empty())
36    }
37}
38
39pub fn load_testing_data(filename: &str) -> TestData {
40    let file = File::open(filename).unwrap();
41    let input = BufReader::new(file);
42    let mut decoder = Decoder::new(input).unwrap();
43    let mut raw_json = vec![];
44    decoder.read_to_end(&mut raw_json).unwrap();
45    serde_json::from_reader(raw_json.as_slice()).unwrap()
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::load_testing_data;
51
52    #[test]
53    fn it_works() {
54        let data = load_testing_data("data/sveltecomponent.json.gz");
55        assert!(!data.txns.is_empty());
56    }
57}