Skip to content

Commit ad50269

Browse files
committed
move Errors and Value up to abstract class definition
1 parent 24e2be2 commit ad50269

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/CommandLine/ParserResult.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CommandLine
99
public sealed class TypeInfo
1010
{
1111
private readonly Type current;
12-
private readonly IEnumerable<Type> choices;
12+
private readonly IEnumerable<Type> choices;
1313

1414
private TypeInfo(Type current, IEnumerable<Type> choices)
1515
{
@@ -64,10 +64,20 @@ public abstract class ParserResult<T>
6464
private readonly ParserResultType tag;
6565
private readonly TypeInfo typeInfo;
6666

67-
internal ParserResult(ParserResultType tag, TypeInfo typeInfo)
67+
internal ParserResult(IEnumerable<Error> errors, TypeInfo typeInfo)
6868
{
69-
this.tag = tag;
69+
this.tag = ParserResultType.NotParsed;
7070
this.typeInfo = typeInfo;
71+
Errors = errors;
72+
Value = default;
73+
}
74+
75+
internal ParserResult(T value, TypeInfo typeInfo)
76+
{
77+
this.tag = ParserResultType.Parsed;
78+
this.typeInfo = typeInfo;
79+
Errors = new Error[0];
80+
Value = value;
7181
}
7282

7383
/// <summary>
@@ -82,6 +92,16 @@ public TypeInfo TypeInfo
8292
{
8393
get { return typeInfo; }
8494
}
95+
96+
/// <summary>
97+
/// Gets the instance with parsed values. If one or more errors occures, <see langword="default"/> is returned.
98+
/// </summary>
99+
public T Value { get; }
100+
101+
/// <summary>
102+
/// Gets the sequence of parsing errors. If there are no errors, then an empty IEnumerable is returned.
103+
/// </summary>
104+
public IEnumerable<Error> Errors { get; }
85105
}
86106

87107
/// <summary>
@@ -90,26 +110,16 @@ public TypeInfo TypeInfo
90110
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
91111
public sealed class Parsed<T> : ParserResult<T>, IEquatable<Parsed<T>>
92112
{
93-
private readonly T value;
94-
95113
internal Parsed(T value, TypeInfo typeInfo)
96-
: base(ParserResultType.Parsed, typeInfo)
114+
: base(value, typeInfo)
97115
{
98-
this.value = value;
99116
}
100117

101118
internal Parsed(T value)
102119
: this(value, TypeInfo.Create(value.GetType()))
103120
{
104121
}
105122

106-
/// <summary>
107-
/// Gets the instance with parsed values.
108-
/// </summary>
109-
public T Value
110-
{
111-
get { return value; }
112-
}
113123

114124
/// <summary>
115125
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
@@ -118,8 +128,7 @@ public T Value
118128
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
119129
public override bool Equals(object obj)
120130
{
121-
var other = obj as Parsed<T>;
122-
if (other != null)
131+
if (obj is Parsed<T> other)
123132
{
124133
return Equals(other);
125134
}
@@ -159,21 +168,12 @@ public bool Equals(Parsed<T> other)
159168
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
160169
public sealed class NotParsed<T> : ParserResult<T>, IEquatable<NotParsed<T>>
161170
{
162-
private readonly IEnumerable<Error> errors;
163171

164172
internal NotParsed(TypeInfo typeInfo, IEnumerable<Error> errors)
165-
: base(ParserResultType.NotParsed, typeInfo)
173+
: base(errors, typeInfo)
166174
{
167-
this.errors = errors;
168175
}
169176

170-
/// <summary>
171-
/// Gets the sequence of parsing errors.
172-
/// </summary>
173-
public IEnumerable<Error> Errors
174-
{
175-
get { return errors; }
176-
}
177177

178178
/// <summary>
179179
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
@@ -182,8 +182,7 @@ public IEnumerable<Error> Errors
182182
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
183183
public override bool Equals(object obj)
184184
{
185-
var other = obj as NotParsed<T>;
186-
if (other != null)
185+
if (obj is NotParsed<T> other)
187186
{
188187
return Equals(other);
189188
}

0 commit comments

Comments
 (0)