예제 #1
0
        public static UnitResult <E2> Compensate <T, E, E2>(this Result <T, E> result, Func <E, UnitResult <E2> > func)
        {
            if (result.IsSuccess)
            {
                return(UnitResult.Success <E2>());
            }

            return(func(result.Error));
        }
        /// <summary>
        ///     Selects result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static Task <UnitResult <E> > Bind <T, E>(this Result <T, E> result, Func <T, Task <UnitResult <E> > > func)
        {
            if (result.IsFailure)
            {
                return(UnitResult.Failure(result.Error).AsCompletedTask());
            }

            return(func(result.Value));
        }
예제 #3
0
        public static UnitResult <E> BindIf <E>(this UnitResult <E> result, bool condition, Func <UnitResult <E> > func)
        {
            if (!condition)
            {
                return(result);
            }

            return(result.Bind(func));
        }
        public static Task <UnitResult <E2> > Compensate <T, E, E2>(this Result <T, E> result, Func <E, Task <UnitResult <E2> > > func)
        {
            if (result.IsSuccess)
            {
                return(UnitResult.Success <E2>().AsCompletedTask());
            }

            return(func(result.Error));
        }
        public static Task <Result> Compensate <E>(this UnitResult <E> result, Func <E, Task <Result> > func)
        {
            if (result.IsSuccess)
            {
                return(Result.Success().AsCompletedTask());
            }

            return(func(result.Error));
        }
        public static Task <UnitResult <E> > Compensate <E>(this Result result, Func <string, Task <UnitResult <E> > > func)
        {
            if (result.IsSuccess)
            {
                return(UnitResult.Success <E>().AsCompletedTask());
            }

            return(func(result.Error));
        }
예제 #7
0
        public static UnitResult <E> BindIf <E>(this UnitResult <E> result, Func <bool> predicate, Func <UnitResult <E> > func)
        {
            if (!result.IsSuccess || !predicate())
            {
                return(result);
            }

            return(result.Bind(func));
        }
        /// <summary>
        ///     Selects result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static Task <Result <T, E> > Bind <T, E>(this UnitResult <E> result, Func <Task <Result <T, E> > > func)
        {
            if (result.IsFailure)
            {
                return(Result.Failure <T, E>(result.Error).AsCompletedTask());
            }

            return(func());
        }
        /// <summary>
        ///     Executes the given action if the calling result is a failure. Returns the calling result.
        /// </summary>
        public static async Task <UnitResult <E> > OnFailure <E>(this UnitResult <E> result, Func <E, Task> func)
        {
            if (result.IsFailure)
            {
                await func(result.Error).DefaultAwait();
            }

            return(result);
        }
        /// <summary>
        ///     If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
        /// </summary>
        public static UnitResult <E> MapError <E>(this Result result, Func <string, E> errorFactory)
        {
            if (result.IsFailure)
            {
                return(UnitResult.Failure(errorFactory(result.Error)));
            }

            return(UnitResult.Success <E>());
        }
        /// <summary>
        ///     If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
        /// </summary>
        public static Result MapError <E>(this UnitResult <E> result, Func <E, string> errorFactory)
        {
            if (result.IsFailure)
            {
                return(Result.Failure(errorFactory(result.Error)));
            }

            return(Result.Success());
        }
예제 #12
0
        /// <summary>
        ///     Executes the given action if the calling result is a success. Returns the calling result.
        /// </summary>
        public static UnitResult <E> Tap <E>(this UnitResult <E> result, Action action)
        {
            if (result.IsSuccess)
            {
                action();
            }

            return(result);
        }
예제 #13
0
        public static Task <UnitResult <E> > BindIf <E>(this UnitResult <E> result, Func <bool> predicate, Func <Task <UnitResult <E> > > func)
        {
            if (!result.IsSuccess || !predicate())
            {
                return(result.AsCompletedTask());
            }

            return(result.Bind(func));
        }
예제 #14
0
        public static Result Compensate <E>(this UnitResult <E> result, Func <E, Result> func)
        {
            if (result.IsSuccess)
            {
                return(Result.Success());
            }

            return(func(result.Error));
        }
예제 #15
0
        /// <summary>
        ///     If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
        /// </summary>
        public static UnitResult <E2> MapError <E, E2>(this UnitResult <E> result, Func <E, E2> errorFactory)
        {
            if (result.IsFailure)
            {
                return(UnitResult.Failure(errorFactory(result.Error)));
            }

            return(UnitResult.Success <E2>());
        }
        /// <summary>
        ///     Executes the given action if the calling result is a failure. Returns the calling result.
        /// </summary>
        public static UnitResult <E> OnFailure <E>(this UnitResult <E> result, Action <E> action)
        {
            if (result.IsFailure)
            {
                action(result.Error);
            }

            return(result);
        }
예제 #17
0
        public static UnitResult <E> Compensate <E>(this Result result, Func <string, UnitResult <E> > func)
        {
            if (result.IsSuccess)
            {
                return(UnitResult.Success <E>());
            }

            return(func(result.Error));
        }
예제 #18
0
        /// <summary>
        ///     Executes the given action if the calling result is a success. Returns the calling result.
        /// </summary>
        public static async Task <UnitResult <E> > Tap <E>(this UnitResult <E> result, Func <Task> func)
        {
            if (result.IsSuccess)
            {
                await func().DefaultAwait();
            }

            return(result);
        }
예제 #19
0
        /// <summary>
        ///     Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static Result <K, E> Map <K, E>(this UnitResult <E> result, Func <K> func)
        {
            if (result.IsFailure)
            {
                return(Result.Failure <K, E>(result.Error));
            }

            return(Result.Success <K, E>(func()));
        }
예제 #20
0
        public static Task <UnitResult <E> > BindIf <E>(this UnitResult <E> result, bool condition, Func <Task <UnitResult <E> > > func)
        {
            if (!condition)
            {
                return(result.AsCompletedTask());
            }

            return(result.Bind(func));
        }
예제 #21
0
        /// <summary>
        ///     Selects result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static Result <T, E> Bind <T, E>(this UnitResult <E> result, Func <Result <T, E> > func)
        {
            if (result.IsFailure)
            {
                return(result.Error);
            }

            return(func());
        }
        /// <summary>
        ///     If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function.
        /// </summary>
        public static async Task <UnitResult <E2> > MapError <E, E2>(this UnitResult <E> result, Func <E, Task <E2> > errorFactory)
        {
            if (result.IsSuccess)
            {
                return(UnitResult.Success <E2>());
            }

            var error = await errorFactory(result.Error).DefaultAwait();

            return(UnitResult.Failure(error));
        }
        /// <summary>
        ///     Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static async Task <Result <K, E> > Map <K, E>(this UnitResult <E> result, Func <Task <K> > func)
        {
            if (result.IsFailure)
            {
                return(Result.Failure <K, E>(result.Error));
            }

            K value = await func().DefaultAwait();

            return(Result.Success <K, E>(value));
        }
예제 #24
0
        /// <summary>
        ///     Executes the given action if the calling result is a failure. Returns the calling result.
        /// </summary>
        public static async Task <UnitResult <E> > OnFailure <E>(this Task <UnitResult <E> > resultTask, Func <Task> func)
        {
            UnitResult <E> result = await resultTask.DefaultAwait();

            if (result.IsFailure)
            {
                await func().DefaultAwait();
            }

            return(result);
        }
예제 #25
0
        /// <summary>
        ///     Combines several results (and any errors) into a single result.
        ///     The returned result will be a failure if any of the input <paramref name="results"/> are failures.
        /// </summary>
        /// <param name="results">
        ///     The Results to be combined.</param>
        /// <param name="composerError">
        ///     A function that combines any errors.</param>
        /// <returns>
        ///     A Result that is a success when all the input <paramref name="results"/> are also successes.</returns>
        public static UnitResult <E> Combine <E>(IEnumerable <UnitResult <E> > results, Func <IEnumerable <E>, E> composerError)
        {
            List <UnitResult <E> > failedResults = results.Where(x => x.IsFailure).ToList();

            if (failedResults.Count == 0)
            {
                return(UnitResult.Success <E>());
            }

            E error = composerError(failedResults.Select(x => x.Error));

            return(UnitResult.Failure(error));
        }
        /// <summary>
        ///     Passes the result to the given function (regardless of success/failure state) to yield a final output value.
        /// </summary>
        public static async Task <K> Finally <K, E>(this Task <UnitResult <E> > resultTask, Func <UnitResult <E>, Task <K> > func)
        {
            UnitResult <E> result = await resultTask.DefaultAwait();

            return(await func(result).DefaultAwait());
        }
        /// <summary>
        ///     Executes the given action if the calling result is a success. Returns the calling result.
        /// </summary>
        public static async Task <UnitResult <E> > Tap <E>(this Task <UnitResult <E> > resultTask, Action action)
        {
            UnitResult <E> result = await resultTask.DefaultAwait();

            return(result.Tap(action));
        }
        /// <summary>
        ///     Selects result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead.
        /// </summary>
        public static async Task <Result <T, E> > Bind <T, E>(this Task <UnitResult <E> > resultTask, Func <Result <T, E> > func)
        {
            UnitResult <E> result = await resultTask.DefaultAwait();

            return(result.Bind(func));
        }
예제 #29
0
        /// <summary>
        ///     Executes the given action if the calling result is a failure. Returns the calling result.
        /// </summary>
        public static async Task <UnitResult <E> > OnFailure <E>(this Task <UnitResult <E> > resultTask, Action <E> action)
        {
            UnitResult <E> result = await resultTask.DefaultAwait();

            return(result.OnFailure(action));
        }
예제 #30
0
 /// <summary>
 ///     Passes the result to the given function (regardless of success/failure state) to yield a final output value.
 /// </summary>
 public static K Finally <K, E>(this UnitResult <E> result, Func <UnitResult <E>, K> func)
 => func(result);