Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>AStar.Dev.Functional.Extensions</PackageId>
<Version>0.4.0</Version>
<Version>0.4.2</Version>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>AStar Development, Jason Barden</Authors>
<Company>AStar Development</Company>
Expand All @@ -26,7 +26,7 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Title>AStar.Dev.Functional.Extensions</Title>
<Copyright>AStar Development 2025</Copyright>
<PackageReleaseNotes>Add an extension method to convert a failures exception message to an instance of the ErrorResponse class.</PackageReleaseNotes>
<PackageReleaseNotes>Add a pair of additional extension methods for MatchAsync to handle additional scenarios.</PackageReleaseNotes>
<PackageIcon>astar.png</PackageIcon>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions src/AStar.Dev.Functional.Extensions/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public async Task<TResult> MatchAsync<TResult>(
Error err => await onFailure(err.Reason),
_ => throw new InvalidOperationException($"Unrecognized result type: {GetType().Name}")
};

#pragma warning restore S3060

/// <summary>
Expand Down
40 changes: 39 additions & 1 deletion src/AStar.Dev.Functional.Extensions/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ namespace AStar.Dev.Functional.Extensions;
[SuppressMessage("ReSharper", "GrammarMistakeInComment")]
public static class ResultExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="TSuccess"></typeparam>
/// <typeparam name="TError"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="resultTask"></param>
/// <param name="onSuccess"></param>
/// <param name="onFailure"></param>
/// <returns></returns>
public static async Task<TResult> MatchAsync<TSuccess, TError, TResult>(
this Task<Result<TSuccess, TError>> resultTask,
Func<TSuccess, Task<TResult>> onSuccess,
Func<TError, Task<TResult>> onFailure)
{
var result = await resultTask;
return await result.Match(onSuccess, onFailure);
}

/// <summary>
///
/// </summary>
/// <typeparam name="TSuccess"></typeparam>
/// <typeparam name="TError"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="resultTask"></param>
/// <param name="onSuccess"></param>
/// <param name="onFailure"></param>
/// <returns></returns>
public static async Task<TResult> MatchAsync<TSuccess, TError, TResult>(
this Task<Result<TSuccess, TError>> resultTask,
Func<TSuccess, TResult> onSuccess,
Func<TError, TResult> onFailure)
{
var result = await resultTask;
return result.Match(onSuccess, onFailure);
}

/// <summary>
/// Transforms the success value of a <see cref="Result{TSuccess, TError}" /> using the specified mapping function.
/// </summary>
Expand All @@ -24,7 +62,7 @@ public static class ResultExtensions
/// </returns>
public static Result<TNew, TError> Map<TSuccess, TError, TNew>(
this Result<TSuccess, TError> result,
Func<TSuccess, TNew> map) =>
Func<TSuccess, TNew> map) =>
result.Match<Result<TNew, TError>>(
ok => new Result<TNew, TError>.Ok(map(ok)),
err => new Result<TNew, TError>.Error(err)
Expand Down
Loading