Skip to content

Commit

Permalink
Better living through Result
Browse files Browse the repository at this point in the history
  • Loading branch information
griotspeak committed Dec 24, 2024
1 parent 65ed73b commit 6442269
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ extension DiscriminatedUnionMacro: MemberMacro {
let titleCasedName = "\(caseName.first!.uppercased())\(caseName.dropFirst())"
return """
public func tupleFrom\(raw: titleCasedName)() throws -> \(raw: tupleType) {
public func tupleFrom\(raw: titleCasedName)() -> Swift.Result<\(raw: tupleType), ExtractorError> {
if case .\(raw: caseName)(\(raw: pBindings)) = self {
return \(raw: returnValue)
.success(\(raw: returnValue))
} else {
throw ExtractorError.invalidExtraction(expected: .\(raw: caseName), actual: self.discriminant)
.failure(.invalidExtraction(expected: .\(raw: caseName), actual: self.discriminant))
}
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/DiscriminatedUnionTests/DiscriminatedUnionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ enum Pet {
case invalidExtraction(expected: Discriminant, actual: Discriminant)
}
public func catTupleValue() throws -> Bool {
public func tupleFromCat() -> Swift.Result<Bool, ExtractorError> {
if case .cat(let curious) = self {
return curious
.success(curious)
} else {
throw ExtractorError.invalidExtraction(expected: .cat, actual: self.discriminant)
.failure(.invalidExtraction(expected: .cat, actual: self.discriminant))
}
}
public func birdTupleValue() throws -> (name: String, Int) {
public func tupleFromBird() -> Swift.Result<(name: String, Int), ExtractorError> {
if case .bird(let name, let index1) = self {
return (name, index1)
.success((name, index1))
} else {
throw ExtractorError.invalidExtraction(expected: .bird, actual: self.discriminant)
.failure(.invalidExtraction(expected: .bird, actual: self.discriminant))
}
}
}
Expand Down

0 comments on commit 6442269

Please sign in to comment.