Repository: TedDriggs/darling (rust)

Issue:
Struct and newtype enums should appear in error paths from derived impls
When an enum with non-unit variants derives `FromMetaItem`, the ident of the non-unit variant will form part of the path in the input source. A user would expect to see that ident in an error path generated by darling.

Pull request description:
Fix #10 by automatically setting rename_rule to snake_case for enums
When deriving for an enum, assume the author wants snake_case unless explicitly set otherwise.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eecca05..5601e93 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,4 +4,5 @@
 
 - Added support for returning multiple errors from parsing [#5](https://github.com/TedDriggs/darling/pull/5)
 - Derived impls no longer return on first error [#5](https://github.com/TedDriggs/darling/pull/5)
-- Removed default types for `V` and `F` from `ast::Body`
\ No newline at end of file
+- Removed default types for `V` and `F` from `ast::Body`
+- Enum variants are automatically converted to snake_case [#12](https://github.com/TedDriggs/darling/pull/12)
\ No newline at end of file
diff --git a/core/src/options/core.rs b/core/src/options/core.rs
index 9b84897..c3ee864 100644
--- a/core/src/options/core.rs
+++ b/core/src/options/core.rs
@@ -43,7 +43,13 @@ impl Core {
             generics: di.generics.clone(),
             body: Body::empty_from(&di.body),
             default: Default::default(),
-            rename_rule: Default::default(),
+            // See https://github.com/TedDriggs/darling/issues/10: We default to snake_case
+            // for enums to help authors produce more idiomatic APIs.
+            rename_rule: if let syn::Body::Enum(_) = di.body {
+                RenameRule::SnakeCase
+            } else {
+                Default::default()
+            },
             map: Default::default(),
             bound: Default::default(),
         }
@@ -64,16 +70,26 @@ impl ParseAttribute for Core {
     fn parse_nested(&mut self, mi: &syn::MetaItem) -> Result<()> {
         match mi.name() {
             "default" => {
-                self.default = FromMetaItem::from_meta_item(mi)?;
-                Ok(())
+                if self.default.is_some() {
+                    Err(Error::duplicate_field("default"))
+                } else {
+                    self.default = FromMetaItem::from_meta_item(mi)?;
+                    Ok(())
+                }
             }
             "rename_all" => {
+                // WARNING: This may have been set based on body shape previously,
+                // so an overwrite may be permissible.
                 self.rename_rule = FromMetaItem::from_meta_item(mi)?;
                 Ok(())
             }
             "map" => {
-                self.map = FromMetaItem::from_meta_item(mi)?;
-                Ok(())
+                if self.map.is_some() {
+                    Err(Error::duplicate_field("map"))
+                } else {
+                    self.map = FromMetaItem::from_meta_item(mi)?;
+                    Ok(())
+                }
             }
             "bound" => {
                 self.bound = FromMetaItem::from_meta_item(mi)?;


Test patch:
diff --git a/tests/accrue_errors.rs b/tests/accrue_errors.rs
index 66bb08f..b9c178b 100644
--- a/tests/accrue_errors.rs
+++ b/tests/accrue_errors.rs
@@ -60,7 +60,6 @@ fn body_only_issues() {
 }
 
 #[derive(Debug, FromMetaItem)]
-#[darling(rename_all="snake_case")]
 enum Week {
     Monday,
     Tuesday {