Skip to content

Commit

Permalink
move Errors and Value up to abstract class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjaylward committed May 22, 2020
1 parent 24e2be2 commit ad50269
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/CommandLine/ParserResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CommandLine
public sealed class TypeInfo
{
private readonly Type current;
private readonly IEnumerable<Type> choices;
private readonly IEnumerable<Type> choices;

private TypeInfo(Type current, IEnumerable<Type> choices)
{
Expand Down Expand Up @@ -64,10 +64,20 @@ public abstract class ParserResult<T>
private readonly ParserResultType tag;
private readonly TypeInfo typeInfo;

internal ParserResult(ParserResultType tag, TypeInfo typeInfo)
internal ParserResult(IEnumerable<Error> errors, TypeInfo typeInfo)
{
this.tag = tag;
this.tag = ParserResultType.NotParsed;
this.typeInfo = typeInfo;
Errors = errors;
Value = default;
}

internal ParserResult(T value, TypeInfo typeInfo)
{
this.tag = ParserResultType.Parsed;
this.typeInfo = typeInfo;
Errors = new Error[0];
Value = value;
}

/// <summary>
Expand All @@ -82,6 +92,16 @@ public TypeInfo TypeInfo
{
get { return typeInfo; }
}

/// <summary>
/// Gets the instance with parsed values. If one or more errors occures, <see langword="default"/> is returned.
/// </summary>
public T Value { get; }

/// <summary>
/// Gets the sequence of parsing errors. If there are no errors, then an empty IEnumerable is returned.
/// </summary>
public IEnumerable<Error> Errors { get; }
}

/// <summary>
Expand All @@ -90,26 +110,16 @@ public TypeInfo TypeInfo
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public sealed class Parsed<T> : ParserResult<T>, IEquatable<Parsed<T>>
{
private readonly T value;

internal Parsed(T value, TypeInfo typeInfo)
: base(ParserResultType.Parsed, typeInfo)
: base(value, typeInfo)
{
this.value = value;
}

internal Parsed(T value)
: this(value, TypeInfo.Create(value.GetType()))
{
}

/// <summary>
/// Gets the instance with parsed values.
/// </summary>
public T Value
{
get { return value; }
}

/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
Expand All @@ -118,8 +128,7 @@ public T Value
/// <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>
public override bool Equals(object obj)
{
var other = obj as Parsed<T>;
if (other != null)
if (obj is Parsed<T> other)
{
return Equals(other);
}
Expand Down Expand Up @@ -159,21 +168,12 @@ public bool Equals(Parsed<T> other)
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public sealed class NotParsed<T> : ParserResult<T>, IEquatable<NotParsed<T>>
{
private readonly IEnumerable<Error> errors;

internal NotParsed(TypeInfo typeInfo, IEnumerable<Error> errors)
: base(ParserResultType.NotParsed, typeInfo)
: base(errors, typeInfo)
{
this.errors = errors;
}

/// <summary>
/// Gets the sequence of parsing errors.
/// </summary>
public IEnumerable<Error> Errors
{
get { return errors; }
}

/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
Expand All @@ -182,8 +182,7 @@ public IEnumerable<Error> Errors
/// <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>
public override bool Equals(object obj)
{
var other = obj as NotParsed<T>;
if (other != null)
if (obj is NotParsed<T> other)
{
return Equals(other);
}
Expand Down

0 comments on commit ad50269

Please sign in to comment.