Disallow Option<_> type for fields with #[nested] or #[default]

Regarding nested fields: I cannot imagine a situation where that
distinction is useful. Also, specifying an empty nested object looks
stupid in TOML and YAML anyway.

Regarding default fields: If there is a default value, then the field
should not be declared as optional to begin with.
This commit is contained in:
Lukas Kalbertodt
2021-07-25 10:46:06 +02:00
parent 9c5990d803
commit ebd8aba2fa
6 changed files with 146 additions and 118 deletions

View File

@@ -22,15 +22,15 @@ pub struct Meta {
pub struct Field {
pub name: &'static str,
pub doc: &'static [&'static str],
pub optional: bool,
pub kind: FieldKind,
}
#[derive(Clone, Copy, Debug)]
pub enum FieldKind {
Leaf {
RequiredLeaf {
default: Option<Expr>,
},
OptionalLeaf,
Nested {
meta: &'static Meta,
},

View File

@@ -141,24 +141,19 @@ fn format_impl(
}
match &field.kind {
FieldKind::Leaf { default } => {
FieldKind::RequiredLeaf { default } => {
// Emit comment about default value or the value being required
if options.comments {
match default {
Some(v) => {
if !field.doc.is_empty() {
emit!("#");
}
emit!("# Default value: {}", PrintExpr(v));
if let Some(v) = default {
if !field.doc.is_empty() {
emit!("#");
}
None => {
if !field.optional {
if !field.doc.is_empty() {
emit!("#");
}
emit!("# Required! This value must be specified.");
}
emit!("# Default value: {}", PrintExpr(v));
} else {
if !field.doc.is_empty() {
emit!("#");
}
emit!("# Required! This value must be specified.");
}
}
@@ -169,6 +164,8 @@ fn format_impl(
}
}
FieldKind::OptionalLeaf => emit!("#{} =", field.name),
FieldKind::Nested { meta } => {
let child_path = path.iter().copied().chain([field.name]).collect();
format_impl(s, meta, child_path, options);