use std::collections::BTreeMap;
use std::fmt::{self, Debug};
use std::fs;
use std::path::{Path, PathBuf};
use ctl_input::de::Tagged;
use ctl_input::format::Reading;
use ctl_input::tree::{Node, NodeKind};
use ctl_input::{FieldPath, Format, Formats, Input, Limits};
use serde::Deserialize;
use serde::de::{DeserializeOwned, IgnoredAny};
#[derive(Debug, Deserialize, PartialEq)]
struct Config {
name: String,
labels: Vec<String>,
#[serde(default)]
outputs: Vec<Output>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Output {
path: PathBuf,
file: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct TomlConfig {
name: String,
labels: Vec<String>,
release: Release,
owner: Owner,
#[serde(default)]
outputs: Vec<TomlOutput>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Release {
tag: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Owner {
name: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct TomlOutput {
path: PathBuf,
#[serde(default)]
file: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Ledger {
schema_version: u32,
prefix: String,
#[serde(default)]
queue: Vec<BTreeMap<String, String>>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Port {
port: u16,
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
struct Strict {
name: String,
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
enum Mode {
Fast,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Moded {
mode: Mode,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Copied {
base: BTreeMap<String, String>,
copy: Port,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Nested {
base: BTreeMap<String, String>,
outer: BTreeMap<String, BTreeMap<String, String>>,
copy: Wrapped,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Wrapped {
nested: Port,
}
type Ports = BTreeMap<String, u16>;
type Names = BTreeMap<String, String>;
#[derive(Debug, Deserialize, PartialEq)]
struct Dated {
#[serde(default)]
when: String,
#[serde(default)]
date: String,
}
type Flags = BTreeMap<String, bool>;
#[derive(Debug, Deserialize, PartialEq)]
struct Globs {
files: BTreeMap<String, Entry>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Entry {
label: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Shipped {
assets: Assets,
#[serde(default)]
ci: Jobs,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Assets {
#[serde(flatten)]
targets: BTreeMap<String, Target>,
}
#[derive(Debug, Default, Deserialize, PartialEq)]
struct Jobs {
#[serde(flatten)]
jobs: BTreeMap<String, Target>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Target {
files: Vec<String>,
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(untagged)]
enum Setting {
List(Vec<String>),
Word(String),
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(untagged)]
enum Anything {
Text(String),
Number(f64),
}
type Anythings = BTreeMap<String, Anything>;
#[derive(Debug, Deserialize, PartialEq)]
struct Tags {
styles: Tagged<Setting>,
level: Tagged<Setting>,
}
#[derive(Deserialize, PartialEq)]
#[serde(transparent)]
struct Json(serde_json::Value);
impl Debug for Json {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
fn fixtures() -> Vec<PathBuf> {
let directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
let entries = fs::read_dir(directory).unwrap_or_else(|error| panic!("{error}"));
let mut files: Vec<PathBuf> = entries
.map(|entry| entry.unwrap_or_else(|error| panic!("{error}")).path())
.collect();
files.sort();
files
}
fn format_of(name: &str) -> &'static dyn Format {
Formats::builtin()
.of(Path::new(name))
.unwrap_or_else(|error| panic!("{error}"))
}
fn named(file: &Path) -> Option<(String, String)> {
let name = file.file_stem()?.to_string_lossy().into_owned();
let extension = Path::new(&name).extension()?.to_str()?;
matches!(extension, "yaml" | "toml").then_some(())?;
let text = fs::read_to_string(file).unwrap_or_else(|error| panic!("{error}"));
Some((name, text))
}
fn outcome<T: DeserializeOwned + Debug>(name: &str, text: &str) -> String {
match input(name, text).parse::<T>() {
Ok(value) => format!("value {value:?}"),
Err(error) => {
let problem = &error.problems[0];
let place = match (problem.line, problem.column) {
(Some(line), Some(column)) => format!("{line}:{column}"),
_ => "nowhere".to_owned(),
};
format!("refused {place}: {}", problem.message)
}
}
}
fn readings(name: &str, text: &str) -> Vec<String> {
macro_rules! read {
($($type:ty),* $(,)?) => {
vec![$(format!(
"{name} as {}: {}",
stringify!($type),
outcome::<$type>(name, text)
)),*]
};
}
read![
IgnoredAny, Json, Ledger, Port, Strict, Moded, Copied, Nested, Flags, Ports, Names,
Anythings, Globs, Shipped, Tags, Config, TomlConfig, Dated,
]
}
#[test]
fn the_fixtures_read_as_recorded() {
let mut lines = Vec::new();
let mut texts = 0;
for file in fixtures() {
let Some((name, text)) = named(&file) else {
continue;
};
texts += 1;
lines.extend(readings(&name, &text));
}
assert!(texts > 40, "{texts} fixtures");
insta::assert_snapshot!("readings", lines.join("\n"));
}
fn paths(node: &Node, path: &FieldPath, found: &mut Vec<FieldPath>) {
found.push(path.clone());
found.push(path.clone().key("no such key"));
if let NodeKind::Sequence(items) = node.kind() {
for (index, item) in items.iter().enumerate() {
paths(item, &path.clone().index(index), found);
}
}
if let NodeKind::Mapping(entries) = node.kind() {
for (key, value) in entries {
if let NodeKind::Scalar(scalar) = key.kind() {
paths(value, &path.clone().key(scalar.value()), found);
}
}
}
}
fn input(name: &str, text: &str) -> Input {
let format = format_of(name);
Input::new_with_format(name, text, format)
}
#[test]
fn the_fixtures_place_every_path_as_recorded() {
let mut lines = Vec::new();
for file in fixtures() {
let Some((name, text)) = named(&file) else {
continue;
};
let format = format_of(&name);
let Ok(tree) = format.read(&text, &Limits::NONE, Reading::Listing) else {
let refused = input(&name, &text).place(&FieldPath::root()).is_none();
lines.push(format!(
"{name}: refused by the format, placed nowhere: {refused}"
));
continue;
};
let input = input(&name, &text);
let mut found = Vec::new();
paths(&tree, &FieldPath::root(), &mut found);
let at = |place: Option<ctl_input::Place>| {
place.map_or_else(
|| "none".to_owned(),
|at| format!("{}:{}", at.line, at.column),
)
};
for path in found {
lines.push(format!(
"{name} {path:?}: value {}, key {}",
at(input.place(&path)),
at(input.place_key(&path))
));
}
}
assert!(lines.len() > 500, "{} paths", lines.len());
insta::assert_snapshot!("places", lines.join("\n"));
}