/
Result.StringResult.cs
74 lines (64 loc) · 2.39 KB
/
Result.StringResult.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="string"/> to <see cref="StringResult"/>.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>The string result.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator Result(string str)
=> new StringResult(str);
/// <summary>
/// Gets the string.
/// </summary>
/// <param name="stringValue">The string value.</param>
/// <returns><c>true</c> if the current instance contains <see cref="string"/>, otherwise <c>false</c>.</returns>
public bool TryGetString([NotNullWhen(true)] out string? stringValue)
{
if (this is StringResult stringResult)
{
stringValue = stringResult.String;
return true;
}
stringValue = null;
return false;
}
/// <summary>
/// Gets the string value.
/// </summary>
/// <exception cref="InvalidCastException">Thrown when the current instance is not <see cref="StringResult"/>.</exception>
[ExcludeFromCodeCoverage]
public virtual string String
=> ((StringResult)this).String;
/// <summary>
/// Represents the string result.
/// </summary>
public sealed class StringResult : Result
{
/// <summary>
/// Initializes a new instance of the <see cref="Result.StringResult"/> class.
/// </summary>
/// <param name="str">The string value.</param>
public StringResult(string str)
=> String = str ?? throw new ArgumentNullException(nameof(str));
/// <summary>
/// Converts <see cref="Result.StringResult"/> to <see cref="string"/>.
/// </summary>
/// <param name="stringResult">The string result.</param>
/// <returns>The string.</returns>
[ExcludeFromCodeCoverage]
public static implicit operator string(StringResult stringResult)
=> stringResult.String;
/// <inheritdoc />
public override string ToString()
=> String;
/// <summary>
/// Gets the string value.
/// </summary>
public override string String { get; }
}
}