/
Result.PowerResult.cs
74 lines (64 loc) · 2.38 KB
/
Result.PowerResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics.CodeAnalysis;
namespace xFunc.Maths.Results;
public abstract partial class Result
{
/// <summary>
/// Converts <see cref="PowerValue"/> to <see cref="PowerResult"/>.
/// </summary>
/// <param name="power">The power value.</param>
/// <returns>The power result.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator Result(PowerValue power)
=> new PowerResult(power);
/// <summary>
/// Gets the power value.
/// </summary>
/// <param name="powerValue">The power value.</param>
/// <returns><c>true</c> if the current instance contains <see cref="PowerValue"/>, otherwise <c>false</c>.</returns>
public bool TryGetPower([NotNullWhen(true)] out PowerValue? powerValue)
{
if (this is PowerResult powerResult)
{
powerValue = powerResult.Power;
return true;
}
powerValue = null;
return false;
}
/// <summary>
/// Gets the power value.
/// </summary>
/// <exception cref="InvalidCastException">Thrown when the current instance is not <see cref="PowerResult"/>.</exception>
[ExcludeFromCodeCoverage]
public virtual PowerValue Power
=> ((PowerResult)this).Power;
/// <summary>
/// Represents the power result.
/// </summary>
public sealed class PowerResult : Result
{
/// <summary>
/// Initializes a new instance of the <see cref="Result.PowerResult"/> class.
/// </summary>
/// <param name="power">The power value.</param>
public PowerResult(PowerValue power)
=> Power = power;
/// <summary>
/// Converts <see cref="Result.PowerResult"/> to <see cref="PowerValue"/>.
/// </summary>
/// <param name="powerResult">The power result.</param>
/// <returns>The power value.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator PowerValue(PowerResult powerResult)
=> powerResult.Power;
/// <inheritdoc />
public override string ToString()
=> Power.ToString();
/// <summary>
/// Gets the power value.
/// </summary>
public override PowerValue Power { get; }
}
}