Repository: shepmaster/snafu (Rust)

Issue:
Multiple SNAFU attributes are not supported in one attribute
It is intended that we can say `#[snafu(a, b, c)]`, but it doesn't appear to work:

```rust
#[derive(Debug, Snafu)]
pub(crate) enum Error {
    #[snafu(visibility(pub(crate)), display("Moo"))]
    Alpha,
}
```

```
error: unexpected token
 --> tests/multi.rs:8:39
  |
8 |         #[snafu(visibility(pub(crate)), display("Moo"))]
  |                                       ^
```


Discussion:
I think the problem is that this parenthesis handling needs to be moved out of `SnafuAttribute` and  up into `SnafuAttributeBody`

https://github.com/shepmaster/snafu/blob/31ac98aa3b4aff990fc8c5f416489f485cebf8cb/snafu-derive/src/lib.rs#L918-L919
Thanks for the pointer :)  Created PR #137.  I was hoping this would help with the parentheses issue in #133, which it didn't directly, but it did help me learn about syn parsing and gives me a couple more ideas to try for the other issue.
> I was hoping this would help with the parentheses issue

Yeah, I expected it too as well!



Patch:
diff --git a/snafu-derive/src/lib.rs b/snafu-derive/src/lib.rs
index 5dd3af45..090f6f2b 100644
--- a/snafu-derive/src/lib.rs
+++ b/snafu-derive/src/lib.rs
@@ -915,35 +915,32 @@ impl syn::parse::Parse for SnafuAttribute {
     fn parse(input: syn::parse::ParseStream) -> SynResult<Self> {
         use syn::{Expr, Ident, Visibility};
 
-        let inside;
-        parenthesized!(inside in input);
-        let name: Ident = inside.parse()?;
-
+        let name: Ident = input.parse()?;
         if name == "display" {
-            let m: MyMeta<List<Expr>> = inside.parse()?;
+            let m: MyMeta<List<Expr>> = input.parse()?;
             let v = m.into_option().ok_or_else(|| {
                 syn::Error::new(name.span(), "`snafu(display)` requires an argument")
             })?;
             let v = Box::new(v.0);
             Ok(SnafuAttribute::Display(v))
         } else if name == "visibility" {
-            let m: MyMeta<Visibility> = inside.parse()?;
+            let m: MyMeta<Visibility> = input.parse()?;
             let v = m
                 .into_option()
                 .map_or_else(private_visibility, |v| Box::new(v) as UserInput);
             Ok(SnafuAttribute::Visibility(v))
         } else if name == "source" {
-            if inside.is_empty() {
+            if input.is_empty() {
                 Ok(SnafuAttribute::Source(vec![Source::Flag(true)]))
             } else {
-                let v: MyParens<List<Source>> = inside.parse()?;
+                let v: MyParens<List<Source>> = input.parse()?;
                 Ok(SnafuAttribute::Source(v.0.into_vec()))
             }
         } else if name == "backtrace" {
-            if inside.is_empty() {
+            if input.is_empty() {
                 Ok(SnafuAttribute::Backtrace(Backtrace::Flag(true)))
             } else {
-                let v: MyParens<Backtrace> = inside.parse()?;
+                let v: MyParens<Backtrace> = input.parse()?;
                 Ok(SnafuAttribute::Backtrace(v.0))
             }
         } else {
@@ -962,8 +959,13 @@ impl syn::parse::Parse for SnafuAttributeBody {
         use syn::punctuated::Punctuated;
         use syn::token::Comma;
 
+        // Remove the parentheses from `#[snafu(...)]` so SnafuAttribute only has to deal with the
+        // tokens inside.
+        let inside;
+        parenthesized!(inside in input);
+
         let parse_comma_list = Punctuated::<SnafuAttribute, Comma>::parse_terminated;
-        let list = parse_comma_list(input)?;
+        let list = parse_comma_list(&inside)?;
 
         Ok(SnafuAttributeBody(
             list.into_pairs().map(|p| p.into_value()).collect(),


Test patch:
diff --git a/tests/multiple_attributes.rs b/tests/multiple_attributes.rs
new file mode 100644
index 00000000..000b2730
--- /dev/null
+++ b/tests/multiple_attributes.rs
@@ -0,0 +1,25 @@
+extern crate snafu;
+
+mod error {
+    use snafu::Snafu;
+
+    #[derive(Debug, Snafu)]
+    pub(super) enum Error {
+        // We'll test both of these attributes inside `#[snafu(...)]`
+        #[snafu(visibility(pub(super)), display("Moo"))]
+        Alpha,
+    }
+}
+
+// Confirm `pub(super)` is applied to the generated struct
+#[test]
+fn is_visible() {
+    let _ = error::Alpha;
+}
+
+// Confirm `display("Moo")` is applied to the variant
+#[test]
+fn has_display() {
+    let err = error::Error::Alpha;
+    assert_eq!(format!("{}", err), "Moo");
+}