< Summary

Class:Itinero.Result`1
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Result.cs
Covered lines:19
Uncovered lines:31
Coverable lines:50
Total lines:140
Line coverage:38% (19 of 50)
Covered branches:2
Total branches:12
Branch coverage:16.6% (2 of 12)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
.ctor(...)0%20%
.ctor(...)100%10%
get_Value()50%471.42%
op_Implicit(...)100%10%
op_Implicit(...)100%1100%
op_Implicit(...)100%1100%
get_IsError()100%1100%
get_ErrorMessage()100%1100%
ConvertError()0%20%
Create(...)100%10%
ToString()0%40%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Result.cs

#LineLine coverage
 1using System;
 2
 3namespace Itinero;
 4
 5/// <summary>
 6/// Represents a result of some calculation and associated status information.
 7/// </summary>
 8/// <typeparam name="T"></typeparam>
 9public class Result<T>
 10{
 11    private readonly T _value;
 12    private readonly Func<string, Exception>? _createException;
 13
 14    /// <summary>
 15    /// Creates a new result.
 16    /// </summary>
 11717    public Result(T result)
 11718    {
 11719        _value = result;
 11720        this.ErrorMessage = string.Empty;
 11721        this.IsError = false;
 11722    }
 23
 24    /// <summary>
 25    /// Creates a new result.
 26    /// </summary>
 27    public Result(string errorMessage)
 028        : this(errorMessage, (m) => new Exception(m)) { }
 29
 30    /// <summary>
 31    /// Creates a new result.
 32    /// </summary>
 033    public Result(string errorMessage, Func<string, Exception>? createException)
 034    {
 035        _value = default!;
 036        _createException = createException;
 037        this.ErrorMessage = errorMessage;
 038        this.IsError = true;
 039    }
 40
 41    /// <summary>
 42    /// Gets the result.
 43    /// </summary>
 44    public T Value
 45    {
 46        get
 13147        {
 13148            if (this.IsError &&
 13149                _createException != null)
 050            {
 051                throw _createException(this.ErrorMessage);
 52            }
 53
 13154            return _value;
 13155        }
 56    }
 57
 58    /// <summary>
 59    /// Implicit conversion to a boolean indication success or fail.
 60    /// </summary>
 61    /// <param name="result">The result object.</param>
 62    /// <returns>The success or fail boolean.</returns>
 63    public static implicit operator bool(Result<T> result)
 064    {
 065        return !result.IsError;
 066    }
 67
 68    /// <summary>
 69    /// Implicit conversion to the result object type.
 70    /// </summary>
 71    /// <param name="result">The result object.</param>
 72    /// <returns>The result object type.</returns>
 73    public static implicit operator T(Result<T> result)
 5574    {
 5575        return result.Value;
 5576    }
 77
 78    /// <summary>
 79    /// Implicit conversion from the result object type.
 80    /// </summary>
 81    /// <param name="result">The result.</param>
 82    /// <returns>The result object.</returns>
 83    public static implicit operator Result<T>(T result)
 6984    {
 6985        return new(result);
 6986    }
 87
 88    /// <summary>
 89    /// Gets the status.
 90    /// </summary>
 30391    public bool IsError { get; private set; }
 92
 93    /// <summary>
 94    /// Gets the error message.
 95    /// </summary>
 11796    public string ErrorMessage { get; private set; }
 97
 98    /// <summary>
 99    /// Converts this result, when an error to an result of another type.
 100    /// </summary>
 101    /// <returns></returns>
 102    public Result<TNew> ConvertError<TNew>()
 0103    {
 0104        if (!this.IsError)
 0105        {
 0106            throw new Exception("Cannot convert a result that represents more than an error.");
 107        }
 108
 0109        return new Result<TNew>(this.ErrorMessage, _createException);
 0110    }
 111
 112    /// <summary>
 113    /// Converts this result.
 114    /// </summary>
 115    /// <typeparam name="TOther">A derived type.</typeparam>
 116    /// <returns></returns>
 117    public static Result<T> Create<TOther>(Result<TOther> result) where TOther : T
 0118    {
 0119        return new Result<T>(result);
 0120    }
 121
 122    /// <summary>
 123    /// Returns a description.
 124    /// </summary>
 125    /// <returns></returns>
 126    public override string ToString()
 0127    {
 0128        if (this.IsError)
 0129        {
 0130            return $"Result<{nameof(T)}>: {this.ErrorMessage}";
 131        }
 132
 0133        if (this.Value == null)
 0134        {
 0135            return $"Result<{nameof(T)}>: null";
 136        }
 137
 0138        return $"Result<{nameof(T)}>: {this.Value.ToString()}";
 0139    }
 140}