-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,122 @@ | ||
# EnumeratorMacro | ||
A utility for working with Swift enums | ||
A utility for creating case-by-case code for your Swift enums using the Mustache templating engine. | ||
`EnumeratorMacro` uses [swift-mustache](https://github.com/hummingbird-project/swift-mustache/issues/35)'s flavor. | ||
|
||
# This is a Work-In-Progress | ||
|
||
## Examples | ||
|
||
### Derive Case Names | ||
```swift | ||
@Enumerator( | ||
""" | ||
var caseName: String { | ||
switch self { | ||
{{#cases}} | ||
case .{{name}}: "{{name}}" | ||
{{/cases}} | ||
} | ||
} | ||
""" | ||
) | ||
enum MyEnum { | ||
case a | ||
case b | ||
} | ||
``` | ||
Is expanded to: | ||
```swift | ||
enum MyEnum { | ||
case a | ||
case b | ||
|
||
var caseName: String { | ||
switch self { | ||
case .a: "a" | ||
case .b: "b" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Create a Subtype Enum | ||
|
||
```swift | ||
@Enumerator(""" | ||
{{#cases}} | ||
var is{{capitalized(name)}}: Bool { | ||
switch self { | ||
case .{{name}}: return true | ||
default: return false | ||
} | ||
} | ||
{{/cases}} | ||
""") | ||
enum TestEnum { | ||
case a(val1: String, val2: Int) | ||
case b | ||
case testCase(testValue: String) | ||
} | ||
``` | ||
Is expanded to: | ||
```swift | ||
enum TestEnum { | ||
case a(val1: String, val2: Int) | ||
case b | ||
case testCase(testValue: String) | ||
|
||
var isA: Bool { | ||
switch self { | ||
case .a: return true | ||
default: return false | ||
} | ||
} | ||
|
||
var isB: Bool { | ||
switch self { | ||
case .b: return true | ||
default: return false | ||
} | ||
} | ||
|
||
var isTestcase: Bool { | ||
switch self { | ||
case .testCase: return true | ||
default: return false | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Create a Copy Of The Enum | ||
|
||
Not very practical but I'll leave it here for showcase for now. | ||
|
||
```swift | ||
@Enumerator(""" | ||
enum Copy { | ||
{{#cases}} | ||
case {{name}}{{withParens(joined(namesWithTypes(parameters)))}} | ||
{{/cases}} | ||
} | ||
""") | ||
enum TestEnum { | ||
case a(val1: String, val2: Int) | ||
case b | ||
case testCase(testValue: String) | ||
} | ||
``` | ||
Is expanded to: | ||
```swift | ||
enum TestEnum { | ||
case a(val1: String, val2: Int) | ||
case b | ||
case testCase(testValue: String) | ||
|
||
enum Copy { | ||
case a(val1: String, val2: Int) | ||
case b | ||
case testCase(testValue: String) | ||
} | ||
} | ||
``` |