Class ResultOneOfExtensions

Namespace
REslava.Result.AdvancedPatterns
Assembly
REslava.Result.dll

Essential conversion methods between Result<T> and OneOf<T1, T2>. Focused on the most common use cases with explicit, clear conversions.

public static class ResultOneOfExtensions
Inheritance
ResultOneOfExtensions
Inherited Members

Remarks

This extension provides the two most essential conversion patterns: 1. Result → OneOf for migrating from Result to OneOf patterns 2. OneOf → Result for integrating OneOf with existing Result code

All conversions require explicit error mapping functions to ensure type safety and clear intent. No "magic" default mapping is provided.

Methods

ToOneOf<TError, T>(Result<T>, Func<IReason, TError>)

Converts a Result<T> to OneOf<TError, T> using custom error mapping. This is the most common conversion pattern for migrating from Result to OneOf.

public static OneOf<TError, T> ToOneOf<TError, T>(this Result<T> result, Func<IReason, TError> errorMapper)

Parameters

result Result<T>

The Result to convert.

errorMapper Func<IReason, TError>

Function to map Result error to TError.

Returns

OneOf<TError, T>

A OneOf containing either the mapped error or the success value.

Type Parameters

TError

The error type for OneOf.

T

The success type.

Examples

// Migrate from Result to OneOf
Result<User> result = GetUser();
OneOf<ApiError, User> oneOf = result.ToOneOf(reason => new ApiError(reason.Message));

Exceptions

ArgumentNullException

Thrown when errorMapper is null.

ToResult<TError, T>(OneOf<TError, T>, Func<TError, IError>)

Converts a OneOf<TError, T> to Result<T> using custom error mapping. Essential for integrating OneOf with existing Result-based code.

public static Result<T> ToResult<TError, T>(this OneOf<TError, T> oneOf, Func<TError, IError> errorMapper)

Parameters

oneOf OneOf<TError, T>

The OneOf to convert.

errorMapper Func<TError, IError>

Function to map TError to IError.

Returns

Result<T>

A Result containing either the mapped error or the success value.

Type Parameters

TError

The error type in OneOf.

T

The success type.

Examples

// Integrate OneOf with existing Result infrastructure
OneOf<ApiError, User> oneOf = GetUser();
Result<User> result = oneOf.ToResult(error => Error.Create(error.Message, error.Code));

if (result.IsSuccess)
{
    await SaveToDatabase(result.Value);
}

Exceptions

ArgumentNullException

Thrown when errorMapper is null.