-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathParserResult.cs
More file actions
218 lines (187 loc) · 7.88 KB
/
ParserResult.cs
File metadata and controls
218 lines (187 loc) · 7.88 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine
{
public sealed class TypeInfo
{
private readonly Type current;
private readonly IEnumerable<Type> choices;
private TypeInfo(Type current, IEnumerable<Type> choices)
{
this.current = current;
this.choices = choices;
}
public Type Current
{
get { return this.current; }
}
public IEnumerable<Type> Choices
{
get { return this.choices; }
}
internal static TypeInfo Create(Type current)
{
return new TypeInfo(current, Enumerable.Empty<Type>());
}
internal static TypeInfo Create(Type current, IEnumerable<Type> choices)
{
return new TypeInfo(current, choices);
}
}
/// <summary>
/// Discriminator enumeration of <see cref="CommandLine.ParserResultType"/> derivates.
/// </summary>
public enum ParserResultType
{
/// <summary>
/// Value of <see cref="CommandLine.Parsed{T}"/> type.
/// </summary>
Parsed,
/// <summary>
/// Value of <see cref="CommandLine.NotParsed{T}"/> type.
/// </summary>
NotParsed
}
/// <summary>
/// Models a parser result. When inherited by <see cref="CommandLine.Parsed{T}"/>, it contains an instance of type <typeparamref name="T"/>
/// with parsed values.
/// When inherited by <see cref="CommandLine.NotParsed{T}"/>, it contains a sequence of <see cref="CommandLine.Error"/>.
/// </summary>
/// <typeparam name="T">The type with attributes that define the syntax of parsing rules.</typeparam>
public abstract class ParserResult<T>
{
private readonly ParserResultType tag;
private readonly TypeInfo typeInfo;
internal ParserResult(IEnumerable<Error> errors, TypeInfo typeInfo)
{
this.tag = ParserResultType.NotParsed;
this.typeInfo = typeInfo ?? TypeInfo.Create(typeof(T));
Errors = errors ?? new Error[0];
Value = default;
}
internal ParserResult(T value, TypeInfo typeInfo)
{
Value = value ?? throw new ArgumentNullException(nameof(value));
this.tag = ParserResultType.Parsed;
this.typeInfo = typeInfo ?? TypeInfo.Create(value.GetType());
Errors = new Error[0];
}
/// <summary>
/// Parser result type discriminator, defined as <see cref="CommandLine.ParserResultType"/> enumeration.
/// </summary>
public ParserResultType Tag
{
get { return this.tag; }
}
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>
/// It contains an instance of type <typeparamref name="T"/> with parsed values.
/// </summary>
/// <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>>
{
internal Parsed(T value, TypeInfo typeInfo)
: base(value, typeInfo)
{
}
internal Parsed(T value)
: this(value, TypeInfo.Create(value.GetType()))
{
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <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)
{
if (obj is Parsed<T> other)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { Tag, Value }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.Parsed{T}"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.Parsed{T}"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.Parsed{T}"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(Parsed<T> other)
{
if (other == null)
{
return false;
}
return this.Tag.Equals(other.Tag)
&& Value.Equals(other.Value);
}
}
/// <summary>
/// It contains a sequence of <see cref="CommandLine.Error"/>.
/// </summary>
/// <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>>
{
internal NotParsed(TypeInfo typeInfo, IEnumerable<Error> errors)
: base(errors, typeInfo)
{
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <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)
{
if (obj is NotParsed<T> other)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { Tag, Errors }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.NotParsed{T}"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.NotParsed{T}"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.NotParsed{T}"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(NotParsed<T> other)
{
if (other == null)
{
return false;
}
return this.Tag.Equals(other.Tag)
&& Errors.SequenceEqual(other.Errors);
}
}
}