Do major refactor of format code

This gets rid of a lot of duplicated logic that was previously
copy&pasted. This commit alos:
- Makes it easier to implement new formats
- Gets rid of a few bugs
- Makes the system more flexible (more options)
- Adds a lot of additional tests
This commit is contained in:
Lukas Kalbertodt
2022-10-19 17:12:32 +02:00
parent 44f59f415a
commit 6a9ccd4e22
12 changed files with 558 additions and 247 deletions

View File

@@ -5,45 +5,26 @@ use std::fmt::{self, Write};
use crate::{
Config,
format::{DefaultValueComment, add_empty_line, assert_single_trailing_newline},
meta::{Expr, FieldKind, LeafKind, Meta},
format::{self, Formatter},
meta::Expr,
};
/// Options for generating a TOML template.
pub struct FormatOptions {
// TODO: think about forward/backwards compatibility.
/// Indentation for nested tables. Default: 0.
pub indent: u8,
/// Whether to include doc comments (with your own text and information
/// about whether a value is required and/or has a default). Default:
/// true.
pub comments: bool,
/// If `comments` and this field are `true`, leaf fields with `env = "FOO"`
/// attribute will have a line like this added:
///
/// ```text
/// # Can also be specified via environment variable `FOO`.
/// ```
///
/// Default: `true`.
pub env_keys: bool,
// Potential future options:
// - Comment out default values (`#foo = 3` vs `foo = 3`)
// - Which docs to include from nested objects
/// Non-TOML specific options.
general: format::Options,
}
impl Default for FormatOptions {
fn default() -> Self {
Self {
indent: 0,
comments: true,
env_keys: true,
general: Default::default(),
}
}
}
@@ -91,7 +72,6 @@ impl Default for FormatOptions {
/// ## Required! This value must be specified.
/// ##color =
///
///
/// [log]
/// ## If set to `true`, the app will log to stdout.
/// ##
@@ -111,105 +91,69 @@ impl Default for FormatOptions {
/// }
/// ```
pub fn format<C: Config>(options: FormatOptions) -> String {
let mut out = String::new();
let meta = &C::META;
// Print root docs.
if options.comments {
meta.doc.iter().for_each(|doc| writeln!(out, "#{doc}").unwrap());
if !meta.doc.is_empty() {
add_empty_line(&mut out);
}
}
// Recursively format all nested objects and fields
format_impl(&mut out, meta, vec![], &options);
assert_single_trailing_newline(&mut out);
out
let mut out = TomlFormatter::new(&options);
format::format::<C>(&mut out, options.general);
out.finish()
}
fn format_impl(
s: &mut String,
meta: &Meta,
path: Vec<&str>,
options: &FormatOptions,
) {
/// Like `println!` but into `s` and with indentation.
macro_rules! emit {
($fmt:literal $(, $args:expr)* $(,)?) => {{
// Writing to a string never fails, we can unwrap.
let indent = path.len().saturating_sub(1) * options.indent as usize;
write!(s, "{: <1$}", "", indent).unwrap();
writeln!(s, $fmt $(, $args)*).unwrap();
}};
}
struct TomlFormatter {
indent: u8,
buffer: String,
stack: Vec<&'static str>,
}
// Output all leaf fields first
let leaf_fields = meta.fields.iter().filter_map(|f| match &f.kind {
FieldKind::Leaf { kind, env } => Some((f, kind, env)),
_ => None,
});
for (field, kind, env) in leaf_fields {
let mut emitted_something = false;
macro_rules! empty_sep_doc_line {
() => {
if emitted_something {
emit!("#");
}
};
}
if options.comments {
field.doc.iter().for_each(|doc| emit!("#{doc}"));
emitted_something = !field.doc.is_empty();
if let Some(env) = env {
empty_sep_doc_line!();
emit!("# Can also be specified via environment variable `{env}`.")
}
}
if let LeafKind::Required { default } = kind {
// Emit comment about default value or the value being required
if options.comments {
empty_sep_doc_line!();
emit!("# {}", DefaultValueComment(default.as_ref().map(PrintExpr)));
}
// Emit the actual line with the name and optional value
match default {
Some(v) => emit!("#{} = {}", field.name, PrintExpr(v)),
None => emit!("#{} =", field.name),
}
} else {
emit!("#{} =", field.name);
}
if options.comments {
add_empty_line(s);
impl TomlFormatter {
fn new(options: &FormatOptions) -> Self {
Self {
indent: options.indent,
buffer: String::new(),
stack: Vec::new(),
}
}
// Then all nested fields recursively
let nested_fields = meta.fields.iter().filter_map(|f| match &f.kind {
FieldKind::Nested { meta } => Some((f, meta)),
_ => None,
});
for (field, meta) in nested_fields {
emit!("");
// add_empty_line(s);
if options.comments {
field.doc.iter().for_each(|doc| emit!("#{doc}"));
}
fn emit_indentation(&mut self) {
let num_spaces = self.stack.len() * self.indent as usize;
write!(self.buffer, "{: <1$}", "", num_spaces).unwrap();
}
}
let child_path = path.iter().copied().chain([field.name]).collect::<Vec<_>>();
emit!("[{}]", child_path.join("."));
format_impl(s, meta, child_path, options);
impl Formatter for TomlFormatter {
type ExprPrinter = PrintExpr;
if options.comments {
add_empty_line(s);
}
fn buffer(&mut self) -> &mut String {
&mut self.buffer
}
fn comment(&mut self, comment: impl fmt::Display) {
self.emit_indentation();
writeln!(self.buffer, "#{comment}").unwrap();
}
fn disabled_field(&mut self, name: &str, value: Option<&'static Expr>) {
match value.map(PrintExpr) {
None => self.comment(format_args!("{name} =")),
Some(v) => self.comment(format_args!("{name} = {v}")),
};
}
fn start_nested(&mut self, name: &'static str, doc: &[&'static str]) {
self.stack.push(name);
doc.iter().for_each(|doc| self.comment(doc));
self.emit_indentation();
writeln!(self.buffer, "[{}]", self.stack.join(".")).unwrap();
}
fn end_nested(&mut self) {
self.stack.pop().expect("formatter bug: stack empty");
}
fn start_main(&mut self) {
self.make_gap(1);
}
fn finish(self) -> String {
assert!(self.stack.is_empty(), "formatter bug: stack not empty");
self.buffer
}
}
@@ -244,10 +188,31 @@ mod tests {
#[test]
fn no_comments() {
let out = format::<test_utils::example1::Conf>(FormatOptions {
comments: false,
.. FormatOptions::default()
});
let mut options = FormatOptions::default();
options.general.comments = false;
let out = format::<test_utils::example1::Conf>(options);
assert_str_eq!(&out, include_format_output!("1-no-comments.toml"));
}
#[test]
fn indent_2() {
let mut options = FormatOptions::default();
options.indent = 2;
let out = format::<test_utils::example1::Conf>(options);
assert_str_eq!(&out, include_format_output!("1-indent-2.toml"));
}
#[test]
fn nested_gap_2() {
let mut options = FormatOptions::default();
options.general.nested_field_gap = 2;
let out = format::<test_utils::example1::Conf>(options);
assert_str_eq!(&out, include_format_output!("1-nested-gap-2.toml"));
}
#[test]
fn immediately_nested() {
let out = format::<test_utils::example2::Conf>(Default::default());
assert_str_eq!(&out, include_format_output!("2-default.toml"));
}
}