Skip to content

Instantly share code, notes, and snippets.

@sergiorykov
Created December 13, 2016 09:07
Show Gist options
  • Save sergiorykov/4dafbac32743f97d89b087d0c16ae3e3 to your computer and use it in GitHub Desktop.
Save sergiorykov/4dafbac32743f97d89b087d0c16ae3e3 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Mojito.Core.Extensions
{
public static class ResultExtensions
{
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnFailureAsync<TValue>(this Result<TValue> result,
Func<ErrorReason, Task<Result<TValue>>> func)
{
if (result.Failure)
{
return await func(result.Error);
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnFailureAsync<TValue>(this Task<Result<TValue>> task,
Func<ErrorReason, Task<Result<TValue>>> func)
{
var result = await task;
if (result.Failure)
{
return await func(result.Error);
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnFailureAsync<TValue>(this Task<Result<TValue>> task,
Func<ErrorReason, Task> func)
{
var result = await task;
if (result.Failure)
{
await func(result.Error);
return result.Error;
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnFailureAsync<TValue>(this Task<Result<TValue>> task, Func<ErrorReason, ErrorReason> func)
{
var result = await task;
if (result.Failure)
{
return func(result.Error);
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Result<TValue> result,
Func<TValue, Task<Result<TResult>>> func)
{
if (result.Failure)
{
return result.Error;
}
return await func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnSuccessAsync<TValue>(this Result<TValue> result,
Func<TValue, Task> func)
{
if (result.Failure)
{
return result.Error;
}
await func(result.Value);
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TResult>(this Result result, Func<Task<Result<TResult>>> func)
{
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result> OnSuccessAsync(this Result result, Func<Task<Result>> func)
{
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnSuccessAsync<TValue>(this Result result, Func<Task<TValue>> func)
{
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result> OnSuccessAsync(this Result result, Func<Task> func)
{
if (result.Failure)
{
return result.Error;
}
await func();
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnSuccessAsync<TValue>(this Result<TValue> result,
Func<TValue, Task<Result>> func)
{
if (result.Failure)
{
return result.Error;
}
var executed = await func(result.Value);
if (executed.Failure)
{
return executed.Error;
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Result<TValue> result,
Func<TValue, Task<TResult>> func)
{
if (result.Failure)
{
return result.Error;
}
return await func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Result<TValue> result,
Func<TValue, Task<Option<TResult>>> func, Func<ErrorReason> onError)
{
if (result.Failure)
{
return result.Error;
}
return await func(result.Value).ToResultAsync(onError);
}
[DebuggerStepThrough]
public static async Task<Result> OnSuccessAsync(this Task<Result> task, Func<Task<Result>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TResult>(this Task<Result> task, Func<TResult> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return func();
}
[DebuggerStepThrough]
public static async Task<Result> OnSuccessAsync(this Task<Result> task, Func<Task> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
await func();
return Result.Ok();
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TResult>(this Task<Result> task, Func<Result<TResult>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return func();
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TResult>(this Task<Result> task, Func<Task<TResult>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TResult>(this Task<Result> task,
Func<Task<Result<TResult>>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return await func();
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Task<Result<TValue>> task,
Func<TValue, Result<TResult>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Task<Result<TValue>> task,
Func<TValue, TResult> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnSuccessAsync<TValue>(this Task<Result<TValue>> task, Func<TValue, Task> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
await func(result.Value);
return result;
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Task<Result<TValue>> task,
Func<TValue, Task<TResult>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return await func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TResult>> OnSuccessAsync<TValue, TResult>(this Task<Result<TValue>> task,
Func<TValue, Task<Result<TResult>>> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return await func(result.Value);
}
[DebuggerStepThrough]
public static async Task<Result<TValue>> OnSuccessAsync<TValue>(this Task<Result<TValue>> value,
Func<TValue, Task<Result>> func)
{
var result = await value;
if (result.Failure)
{
return result.Error;
}
var condition = await func(result.Value);
if (condition.Failure)
{
return condition.Error;
}
return result;
}
[DebuggerStepThrough]
public static async Task<Result> OnSuccessAsync<TValue>(this Task<Result<TValue>> task,
Func<TValue, Result> func)
{
var result = await task;
if (result.Failure)
{
return result.Error;
}
return func(result.Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment