/Beefeater

Guard your methods' Ins and Outs.

Primary LanguageC#MIT LicenseMIT

Beefeater

Guard your methods' Ins and Outs.

License NuGet NuGet Source Browser Badges

Stories in Ready Stories in progress Issue Stats Issue Stats

AppVeyor Build status Travis Build Status

Coverage Status codecov.io Coverity Scan Build Status

This library contains helpers to add semantics to the optionality of your parameters and results from method calls.

This library is built on NetStandard1.0 for maximum compatability.

Nuget

You can install the nuget package using Install-Package Beefeater or by heading to the Nuget Package Page.

Examples

public Option<string> Modify(NotNull<string> first, Option<string> second)
{
    return second.Match(
        v => first + v,
        () => Option<string>.None);
}

var x = Modify("Hello", "World");
var x = Modify("Hello", null);

public enum ErrorResult
{
    UnknownError,
    FileNotFound,
    Unauthorized
}
public Result<bool, ErrorResult> Create(NotNull<string> filePath, Option<string> second)
{
    FileStream stream;
    try
    {
        stream = File.OpenWrite(filePath);
    }
    catch (UnauthorizedAccessException ex)
    {
        return ErrorResult.Unauthorized;
    }
    catch (FileNotFoundException ex)
    {
        return ErrorResult.FileNotFound;
    }
    catch (Exception ex)
    {
        return ErrorResult.UnknownError;
    }
    using (stream)
    {
        return second.Match(
            v =>
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine(v);
                }
                return true;
            },
            () => false);
    }
}

public Either<long, double> DivideByTwo(int aNumber)
{
    if (aNumber % 2 == 0)
        return aNumber / 2;
    return aNumber / (double)2;
}

var x = DivideByTwo(10);
var x = DivideByTwo(5);