Repository: dtolnay/automod (language: rust)

Issue:
Use path attribute for modules with hyphen in filename
My folder structure is the following

```
tests/
├── issues
│   ├── issue-00055.rs
│   └── mod.rs
├── master_playlist.rs
├── media_playlist.rs
├── mod.rs
├── rfc8216.rs
└── version-number.rs
```

`tests/issues/mod.rs` contains

```rust
automod::dir!("tests/issues");
```

and the `proc-macro` panics with

```
error: proc macro panicked
 --> tests/issues/mod.rs:3:1
  |
3 | automod::dir!("tests/issues");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: `"issue-00055"` is not a valid identifier
```

---

This issue can be solved by generating the following for the above example

```rust
#[path = "tests/issues/issue-00055"]
mod issue_00055;
```

https://stackoverflow.com/a/57535534/7766117


### Why would I want to do this?

As the stackoverflow answer mentions

> However this is not common, and I wouldn't recommand it. Just stick with snake_case for module names.

this should normally not be done, but I prefer to have my files separated by hyphens in this case, because it looks better and the rust repository does the same:
https://github.com/rust-lang/rust/tree/master/src/test/ui/issues



Discussion / hints:
I would accept a PR to fix this.

Fix patch:
diff --git a/examples/issue4/with-hyphen.rs b/examples/issue4/with-hyphen.rs
new file mode 100644
index 0000000..8e8c204
--- /dev/null
+++ b/examples/issue4/with-hyphen.rs
@@ -0,0 +1,4 @@
+#[test]
+fn test() {
+    assert_eq!(module_path!(), "tests::issue4::with_hyphen");
+}
diff --git a/src/lib.rs b/src/lib.rs
index f26d486..c873c91 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -109,9 +109,18 @@ pub fn dir(input: TokenStream) -> TokenStream {
 }
 
 fn mod_item(vis: &Visibility, name: String) -> TokenStream2 {
-    let ident = Ident::new(&name, Span::call_site());
-    quote! {
-        #vis mod #ident;
+    if name.contains('-') {
+        let path = format!("{}.rs", name);
+        let ident = Ident::new(&name.replace('-', "_"), Span::call_site());
+        quote! {
+            #[path = #path]
+            #vis mod #ident;
+        }
+    } else {
+        let ident = Ident::new(&name, Span::call_site());
+        quote! {
+            #vis mod #ident;
+        }
     }
 }
 


Test patch:
diff --git a/examples/tests.rs b/examples/tests.rs
index 381783f..6ea8108 100644
--- a/examples/tests.rs
+++ b/examples/tests.rs
@@ -12,4 +12,8 @@ mod regression {
     automod::dir!("examples/regression");
 }
 
+mod issue4 {
+    automod::dir!("examples/issue4");
+}
+
 fn main() {}