Skip to content

Commit

Permalink
add filterNoParams and filterWithParams to [Case]
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 15, 2024
1 parent fa7dac5 commit 3694ca8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ In addition to [`swift-mustache`'s own "functions"/"transforms"](https://docs.hu
* `withParens`: If the string is not empty, surrounds it in parenthesis.
* `Array`:
* `joined`: Equivalent to `.joined(seperator: ", ")`
* `[Parameter]`:
* `[Case]` (`cases`):
* `filterNoParams`: Filters-in the cases with no parameters.
* `filterWithParams`: Filters-in the cases with one or more parameters.
* `[Parameter]` (`parameters`):
* `names`: Returns a string-array of the names of the parameters.
* `names(parameters)` -> `[param1, param2, param3]`.
* `types`: Retunrs a string-array of the types of the parameters.
Expand Down
2 changes: 1 addition & 1 deletion Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension EnumeratorMacroType: MemberMacro {
let members = enumDecl.memberBlock.members
let caseDecls = members.compactMap { $0.decl.as(EnumCaseDeclSyntax.self) }
let elements = caseDecls.flatMap(\.elements)
let cases = try elements.map(EnumCase.init(from:))
let cases = try ECases(elements: elements)

guard let arguments = node.arguments else {
throw MacroError.macroDeclarationHasNoArguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftDiagnostics
import SwiftSyntax
import Mustache

struct EnumCase {
struct ECase {
let name: EString
let parameters: EParameters

Expand Down
47 changes: 47 additions & 0 deletions Sources/EnumeratorMacroImpl/Types/ECases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Mustache
import SwiftSyntax

struct ECases {
fileprivate let underlying: EArray<ECase>

init(elements: [EnumCaseElementSyntax]) throws {
self.underlying = .init(
underlying: try elements.map(ECase.init(from:))
)
}
}

extension ECases: CustomStringConvertible {
var description: String {
self.underlying.description
}
}

extension ECases: Sequence, MustacheSequence {
func makeIterator() -> Array<ECase>.Iterator {
self.underlying.makeIterator()
}
}

extension ECases: CustomReflectable {
var customMirror: Mirror {
Mirror(reflecting: self.underlying)
}
}

extension ECases: MustacheTransformable {
func transform(_ name: String) -> Any? {
if let defaultTransformed = self.underlying.transform(name) {
return convertToCustomTypesIfPossible(defaultTransformed)
} else {
switch name {
case "filterNoParams":
return self.filter(\.parameters.underlying.underlying.isEmpty)
case "filterWithParams":
return self.filter({ !$0.parameters.underlying.underlying.isEmpty })
default:
return nil
}
}
}
}
14 changes: 7 additions & 7 deletions Sources/EnumeratorMacroImpl/Types/EParameters.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Mustache

struct EParameters {
fileprivate let underlying: EArray<EParameter>
let underlying: EArray<EParameter>

init(underlying: [EParameter]) {
self.underlying = .init(underlying: underlying)
}
}

extension EParameters: CustomStringConvertible {
var description: String {
self.underlying.description
}
}

extension EParameters: Sequence, MustacheSequence {
func makeIterator() -> Array<EParameter>.Iterator {
self.underlying.makeIterator()
Expand Down Expand Up @@ -64,9 +70,3 @@ extension EParameters: MustacheTransformable {
}
}
}

extension EParameters: CustomStringConvertible {
var description: String {
self.underlying.description
}
}

0 comments on commit 3694ca8

Please sign in to comment.