1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

const TESTS_DIR: include_dir::Dir =
    include_dir::include_dir!("$CARGO_MANIFEST_DIR/assets/toml-test/tests");

pub fn version(ver: &str) -> impl Iterator<Item = &'static std::path::Path> {
    TESTS_DIR
        .get_file(&format!("files-toml-{ver}"))
        .and_then(|f| std::str::from_utf8(f.contents()).ok())
        .into_iter()
        .flat_map(|f| f.lines())
        .map(std::path::Path::new)
}

pub fn versions() -> std::collections::HashMap<&'static str, Vec<&'static std::path::Path>> {
    TESTS_DIR
        .files()
        .filter_map(|f| {
            let name = f.path().file_name()?;
            let version = name.to_str()?.strip_prefix("files-toml-")?;
            let paths = std::str::from_utf8(f.contents())
                .ok()?
                .lines()
                .map(std::path::Path::new)
                .collect::<Vec<_>>();
            Some((version, paths))
        })
        .collect()
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Valid<'a> {
    pub name: &'a std::path::Path,
    pub fixture: &'a [u8],
    pub expected: &'a [u8],
}

pub fn valid() -> impl Iterator<Item = Valid<'static>> {
    let valid_dir = TESTS_DIR.get_dir("valid").unwrap();
    valid_files(valid_dir).chain(valid_dir.dirs().flat_map(|d| {
        assert_eq!(d.dirs().count(), 0);
        valid_files(d)
    }))
}

fn valid_files<'d>(
    dir: &'d include_dir::Dir<'static>,
) -> impl Iterator<Item = Valid<'static>> + 'd {
    dir.files()
        .filter(|f| f.path().extension().unwrap_or_default() == "toml")
        .map(move |f| {
            let t = f;
            let j = dir
                .files()
                .find(|f| {
                    f.path().parent() == t.path().parent()
                        && f.path().file_stem() == t.path().file_stem()
                        && f.path().extension().unwrap() == "json"
                })
                .unwrap();
            let name = t.path();
            let fixture = t.contents();
            let expected = j.contents();
            Valid {
                name,
                fixture,
                expected,
            }
        })
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Invalid<'a> {
    pub name: &'a std::path::Path,
    pub fixture: &'a [u8],
}

pub fn invalid() -> impl Iterator<Item = Invalid<'static>> {
    let invalid_dir = TESTS_DIR.get_dir("invalid").unwrap();
    assert_eq!(invalid_dir.files().count(), 0);
    invalid_dir.dirs().flat_map(|d| {
        assert_eq!(d.dirs().count(), 0);
        d.files().map(|f| {
            let t = f;
            let name = f.path();
            let fixture = t.contents();
            Invalid { name, fixture }
        })
    })
}