mirror of
https://github.com/OMGeeky/confique.git
synced 2026-01-23 19:55:38 +01:00
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:
@@ -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,
|
||||
},
|
||||
|
||||
25
src/toml.rs
25
src/toml.rs
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user