1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![warn(clippy::print_stderr)]
3#![warn(clippy::print_stdout)]
4#![allow(clippy::self_named_module_files)] #[doc = include_str!("../README.md")]
7#[cfg(doctest)]
8pub struct ReadmeDoctests;
9
10use std::borrow::Cow;
11
12const TESTS_DIR: include_dir::Dir<'_> =
13 include_dir::include_dir!("$CARGO_MANIFEST_DIR/assets/toml-test/tests");
14
15pub fn version(ver: &str) -> impl Iterator<Item = &'static std::path::Path> {
16 TESTS_DIR
17 .get_file(format!("files-toml-{ver}"))
18 .and_then(|f| std::str::from_utf8(f.contents()).ok())
19 .into_iter()
20 .flat_map(|f| f.lines())
21 .map(std::path::Path::new)
22}
23
24pub fn versions() -> std::collections::HashMap<&'static str, Vec<&'static std::path::Path>> {
25 TESTS_DIR
26 .files()
27 .filter_map(|f| {
28 let name = f.path().file_name()?;
29 let version = name.to_str()?.strip_prefix("files-toml-")?;
30 let paths = std::str::from_utf8(f.contents())
31 .ok()?
32 .lines()
33 .map(std::path::Path::new)
34 .collect::<Vec<_>>();
35 Some((version, paths))
36 })
37 .collect()
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Valid<'a> {
42 pub name: Cow<'a, std::path::Path>,
43 pub fixture: Cow<'a, [u8]>,
44 pub expected: Cow<'a, [u8]>,
45}
46
47impl<'a> Valid<'a> {
48 pub fn borrow<'b: 'a>(&'b self) -> Valid<'b> {
49 Self {
50 name: Cow::Borrowed(self.name()),
51 fixture: Cow::Borrowed(self.fixture()),
52 expected: Cow::Borrowed(self.expected()),
53 }
54 }
55
56 pub fn name(&self) -> &std::path::Path {
57 self.name.as_ref()
58 }
59
60 pub fn fixture(&self) -> &[u8] {
61 self.fixture.as_ref()
62 }
63
64 pub fn expected(&self) -> &[u8] {
65 self.expected.as_ref()
66 }
67}
68
69pub fn valid() -> impl Iterator<Item = Valid<'static>> {
70 let valid_dir = TESTS_DIR.get_dir("valid").unwrap();
71 valid_files(valid_dir).chain(valid_dir.dirs().flat_map(|d| {
72 assert_eq!(d.dirs().count(), 0);
73 valid_files(d)
74 }))
75}
76
77fn valid_files<'d>(
78 dir: &'d include_dir::Dir<'static>,
79) -> impl Iterator<Item = Valid<'static>> + 'd {
80 dir.files()
81 .filter(|f| f.path().extension().unwrap_or_default() == "toml")
82 .map(move |f| {
83 let t = f;
84 let j = dir
85 .files()
86 .find(|f| {
87 f.path().parent() == t.path().parent()
88 && f.path().file_stem() == t.path().file_stem()
89 && f.path().extension().unwrap() == "json"
90 })
91 .unwrap();
92 let name = Cow::Borrowed(t.path());
93 let fixture = Cow::Borrowed(t.contents());
94 let expected = Cow::Borrowed(j.contents());
95 Valid {
96 name,
97 fixture,
98 expected,
99 }
100 })
101}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct Invalid<'a> {
105 pub name: Cow<'a, std::path::Path>,
106 pub fixture: Cow<'a, [u8]>,
107}
108
109impl<'a> Invalid<'a> {
110 pub fn borrow<'b: 'a>(&'b self) -> Invalid<'b> {
111 Self {
112 name: Cow::Borrowed(self.name()),
113 fixture: Cow::Borrowed(self.fixture()),
114 }
115 }
116
117 pub fn name(&self) -> &std::path::Path {
118 self.name.as_ref()
119 }
120
121 pub fn fixture(&self) -> &[u8] {
122 self.fixture.as_ref()
123 }
124}
125
126pub fn invalid() -> impl Iterator<Item = Invalid<'static>> {
127 let invalid_dir = TESTS_DIR.get_dir("invalid").unwrap();
128 assert_eq!(invalid_dir.files().count(), 0);
129 invalid_dir.dirs().flat_map(|d| {
130 assert_eq!(d.dirs().count(), 0);
131 d.files().map(|f| {
132 let t = f;
133 let name = Cow::Borrowed(f.path());
134 let fixture = Cow::Borrowed(t.contents());
135 Invalid { name, fixture }
136 })
137 })
138}