ResultᐸValue, Errorᐳ - TheRealKoeDev/Railway-Results GitHub Wiki

NameSpace: KoeLib.Patterns.Railway.Results

Example

public class Error
{
    public string Message { get; }
    public DateTimeOffset CreatedAt { get; }
    public Exception Exception { get; }

    public Error(string message, Exception ex = null)
    {
        Message = message;
        CreatedAt = DateTimeOffset.Now;
        Exception = ex;
    }

    public override string ToString()
    {
        return  $"Error: {Message} \n" +
                $"Time: {CreatedAt} \n" +                       
                (Exception == null ? string.Empty : "Exception: " + Exception.Message);
    }
}

public static Result<string, Error> GetPageContent(string address)
{
    try
    {
        using (WebClient client = new WebClient())
        {
            return client.DownloadString(address);
        }               
    }
    catch(Exception e)
    {
        return new Error("Download failed.", e);
    }
}

public static Result<string, Error> StoreContent(string content, string filename)
{
    try
    {
        DirectoryInfo directory = Directory.CreateDirectory(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "example-downloads")));
        File.WriteAllText(directory.FullName + "\\" + Path.GetFileName(filename), content);
        return "Message: Content was sucessfully stored.";
    }
    catch(Exception e)
    {
        return new Error("Storage failed.", e);
    }
}

public static void Main()
{
    string address = "https://www.google.com";

    string message= GetPageContent(address)
    .Bind(content => StoreContent(content, address + ".html"))
    .Match(msg => msg , error => error.ToString());

    Console.WriteLine($"{address} \n{message}");
    Console.ReadLine();
}

Implicit Operators

public static implicit operator Result<TValue, TError>(TValue value);
public static implicit operator Result<TValue, TError>(TError error);

public static implicit operator Result(Result<TValue, TError> result);
public static implicit operator Result<TValue>(Result<TValue, TError> result);
public static implicit operator ResultOrError<TError>(Result<TValue, TError> result);

Static Methods

Success

public static Result<TValue, TError> Success(TValue value);

Error

public static Result<TValue, TError> Error(TError error);

Instance Methods

OnSuccess

public Result<TValue, TError> OnSuccess(Action<TValue> onSuccess);
public Result<TNewValue, TError> OnSuccess<TNewValue>(Func<TValue, TNewValue> onSuccess);

OnError

public Result<TValue, TError> OnError(Action onError);
public Result<TValue, TNewError> OnError<TNewError>(Func<TError, TNewError> onError);

FixOnError

public Result<TValue, TError> OnFixError(Func<TError, TValue> onError);

Either

public Result<TValue, TError> Either(Action<TValue> onSuccess, Action<TError> onError);
public Result<TNewValue, TError> Either<TNewValue>(Func<TValue, TNewValue> onSuccess, Action<TError> onError);
public Result<TValue, TNewError> Either<TNewError>(Action<TValue> onSuccess, Func<TError, TNewError> onError);
public Result<TNewValue,TNewError> Either<TNewValue,TNewError>(Func<TValue, TNewValue> onSuccess, Func<TError, TNewError> onError);

Bind

public ResultWithError<TError> Bind(Func<TValue, ResultWithError<TError>> onSuccess);
public Result<TNewValue, TError> Bind<TNewValue>(Func<TValue, Result<TNewValue, TError>> onSuccess);
public TResult Bind<TResult>(Func<TValue, TResult> onSuccess, Func<TError, TResult> onError) where TResult : IResult;

BindOnError

public Result<TValue> BindOnError(Func<TError, Result<TValue>> onError);
public Result<TValue, TNewError> BindOnError<TNewError>(Func<TError, Result<TValue, TNewError>> onError)

Ensure

public Result<TValue, TError> Ensure(Func<TValue, bool> condition, Func<TError> error);

Match

public T Match<T>(Func<TValue, T> onSuccess, Func<TError, T> onError);

KeepOnSuccess

public Result<TValue, TError> KeepOnSuccess<T>(Func<TValue,T> onSuccess, out T kept);

KeepOnError

public Result<TValue, TError> KeepOnError<T>(Func<TError, T> onError, out T kept);

KeepEither

public Result<TValue, TError> KeepEither<T>(Func<TValue,T> onSuccess, Func<TError, T> onError, out T keptValue); 
public Result<TValue, TError> KeepEither<T1, T2>(Func<TValue,T1> onSuccess, Func<TError, T2> onError, out T1 keptOnSuccess, out T2 keptOnError);

AsPlainResult

public Result AsPlainResult();

AsResultWithValue

public Result<TValue> AsResultWithValue();

AsResultWithError

public ResultOrError<TError> AsResultWithError();
⚠️ **GitHub.com Fallback** ⚠️