Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Reading Templates From Files #4

Open
MahdiBM opened this issue Jul 12, 2024 · 0 comments
Open

Support Reading Templates From Files #4

MahdiBM opened this issue Jul 12, 2024 · 0 comments

Comments

@MahdiBM
Copy link
Owner

MahdiBM commented Jul 12, 2024

In a future where the macros sandbox allows for reading from files, users could simplify this code:

import EnumeratorMacro

@Enumerator("""
enum Subtype: String {
    {{#cases}}
    case {{name}}
    {{/cases}}
}
""",
"""
var subtype: Subtype {
    switch self {
    {{#cases}}
    case .{{name}}:
        .{{name}}
    {{/cases}}
    }
}
""")
enum TestEnum {
    case a(val1: String, val2: Int)
    case b
    case testCase(testValue: String)
}

to:

import EnumeratorMacro
import EnumeratorMacroImpl

@attached(member, names: arbitrary)
macro CreateSubtype() = #externalMacro(
    module: "EnumeratorMacroImpl",
    type: "EnumeratorMacroType"
)

@CreateSubtype
enum TestEnum {
    case a(val1: String, val2: Int)
    case b
    case testCase(testValue: String)
}

And for all the next enums, you only need to use @CreateSubtype instead of writing the whole template.

How this'll work is that it'll try to read a template file which will be expected to be located at a pre-defined place like /PROJECT_ROOT/macro-templates/CreateSubtype.mustache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant