@@ -139,3 +139,5 @@ extension Result: CustomStringConvertible {
|
|||||||
#externalMacro(module: "Macros", type: "RequireErrorResult")
|
#externalMacro(module: "Macros", type: "RequireErrorResult")
|
||||||
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
|
@freestanding(expression) public macro UseOkResult<T>(_: Result<T>) -> T =
|
||||||
#externalMacro(module: "Macros", type: "UseOkResult")
|
#externalMacro(module: "Macros", type: "UseOkResult")
|
||||||
|
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
|
||||||
|
#externalMacro(module: "Macros", type: "UseErrorResult")
|
||||||
|
|||||||
@@ -42,6 +42,29 @@ public struct UseOkResult: ExpressionMacro {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct UseErrorResult: ExpressionMacro {
|
||||||
|
public static func expansion(
|
||||||
|
of node: some FreestandingMacroExpansionSyntax,
|
||||||
|
in context: some MacroExpansionContext
|
||||||
|
) throws -> ExprSyntax {
|
||||||
|
|
||||||
|
guard let argument = node.argumentList.first?.expression else {
|
||||||
|
throw Require.Error.SyntaxError
|
||||||
|
}
|
||||||
|
|
||||||
|
return """
|
||||||
|
{
|
||||||
|
switch \(argument) {
|
||||||
|
case Result.Error(let __error): return __error
|
||||||
|
case Result.Ok(let __good):
|
||||||
|
print("Unexpected result: \\(__good)")
|
||||||
|
throw Require.Error.UnexpectedResult
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public struct Require {
|
public struct Require {
|
||||||
public enum Error: Swift.Error {
|
public enum Error: Swift.Error {
|
||||||
case UnexpectedResult
|
case UnexpectedResult
|
||||||
@@ -101,6 +124,6 @@ public struct RequireErrorResult: ExpressionMacro {
|
|||||||
@main
|
@main
|
||||||
struct P4Macros: CompilerPlugin {
|
struct P4Macros: CompilerPlugin {
|
||||||
var providingMacros: [Macro.Type] = [
|
var providingMacros: [Macro.Type] = [
|
||||||
RequireResult.self, RequireErrorResult.self, UseOkResult.self,
|
RequireResult.self, RequireErrorResult.self, UseOkResult.self, UseErrorResult.self
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user