This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Guard.Messages.cs
315 lines (216 loc) · 16.3 KB
/
Guard.Messages.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if !NETSTANDARD1_0
using System.Net.Mail;
#endif
namespace Dawn
{
/// <content>Provides error messages for the common preconditions.</content>
public static partial class Guard
{
private static class Messages
{
public static string State(string? caller)
=> caller != null
? $"{caller} call is not valid due to the current state of the object."
: "Operation is not valid due to the current state of the object.";
public static string Support(string? caller)
=> caller != null ? $"{caller} is not supported" : "Specified method is not supported.";
public static string Disposal()
=> "Cannot access a disposed object.";
public static string Null<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be null.";
public static string NotNull<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be null.";
public static string NotAllNull(string name1, string name2)
=> $"{name1} and {name2} cannot both be null.";
public static string NotAllNull(string name1, string name2, string name3)
=> $"{name1}, {name2} and {name3} cannot all be null.";
public static string Default<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be {default(T)!}.";
public static string NotDefault<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be {default(T)!}.";
public static string Equal<T>(in ArgumentInfo<T> argument, in T other)
=> argument.Secure ? Require(argument) : $"{argument.Name} must be {ToString(other)}.";
public static string NotEqual<T>(in ArgumentInfo<T> argument, in T other)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot be {ToString(other)}.";
public static string Equal<T>(in ArgumentInfo<T> argument, in T other, T delta)
=> argument.Secure ? Require(argument) : $"{argument.Name} must be within {delta} accuracy of {other}.";
public static string NotEqual<T>(in ArgumentInfo<T> argument, in T other, T delta)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot be within {delta} accuracy of {other}.";
public static string Same<T>(in ArgumentInfo<T> argument, object other)
=> argument.Secure ? Require(argument) : $"{argument.Name} must have the same reference as {ToString(other)}.";
public static string NotSame<T>(in ArgumentInfo<T> argument, object other)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot have the same reference as {ToString(other)}.";
public static string Require<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} is invalid.";
public static string Type(in ArgumentInfo<object> argument, Type type)
=> $"{argument.Name} must be an instance of type {type}.";
public static string NotType(in ArgumentInfo<object> argument, Type type)
=> $"{argument.Name} cannot be an instance of type {type}.";
public static string Compatible<TArgument, TTarget>(in ArgumentInfo<TArgument> argument)
=> $"{argument.Name} must be assignable to type {typeof(TTarget)}.";
public static string NotCompatible<TArgument, TTarget>(in ArgumentInfo<TArgument> argument)
=> $"{argument.Name} cannot be assignable to type {typeof(TTarget)}.";
public static string Min<T>(in ArgumentInfo<T> argument, in T minValue)
=> $"{argument.Name} cannot be less than {minValue}.";
public static string GreaterThan<T>(in ArgumentInfo<T> argument, in T other)
=> $"{argument.Name} must be greater than {other}.";
public static string Max<T>(in ArgumentInfo<T> argument, in T maxValue)
=> $"{argument.Name} cannot be greater than {maxValue}.";
public static string LessThan<T>(in ArgumentInfo<T> argument, in T other)
=> $"{argument.Name} must be less than {other}.";
public static string Zero<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be zero.";
public static string NotZero<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be zero.";
public static string Positive<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be greater than zero.";
public static string NotPositive<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be zero or less.";
public static string Negative<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be less than zero.";
public static string NotNegative<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be zero or greater.";
public static string InRange<T>(in ArgumentInfo<T> argument, in T minValue, in T maxValue)
=> $"{argument.Name} must be between {minValue} and {maxValue}";
public static string NaN<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be not a number (NaN).";
public static string NotNaN<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be not a number (NaN).";
public static string Infinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be positive or negative infinity.";
public static string NotInfinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be positive or negative infinity.";
public static string PositiveInfinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be positive infinity (∞).";
public static string NotPositiveInfinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be positive infinity (∞).";
public static string NegativeInfinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be negative infinity -(∞).";
public static string NotNegativeInfinity<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be negative infinity (-∞).";
public static string StringEmpty(in ArgumentInfo<string> argument)
=> $"{argument.Name} must be empty.";
public static string StringNotEmpty(in ArgumentInfo<string> argument)
=> $"{argument.Name} cannot be empty.";
public static string StringWhiteSpace(in ArgumentInfo<string> argument)
=> $"{argument.Name} must be empty or consist only of white-space characters.";
public static string StringNotWhiteSpace(in ArgumentInfo<string> argument)
=> $"{argument.Name} cannot be empty or consist only of white-space characters.";
public static string StringLength(in ArgumentInfo<string> argument, int length)
=> $"{argument.Name} must consist of {length} characters.";
public static string StringNotLength(in ArgumentInfo<string> argument, int length)
=> $"{argument.Name} cannot consist of {length} characters.";
public static string StringMinLength(in ArgumentInfo<string> argument, int minLength)
=> $"{argument.Name} cannot be shorter than {minLength} characters.";
public static string StringMaxLength(in ArgumentInfo<string> argument, int maxLength)
=> $"{argument.Name} cannot be longer than {maxLength} characters.";
public static string StringLengthInRange(in ArgumentInfo<string> argument, int minLength, int maxLength)
=> $"{argument.Name} must contain {minLength} to {maxLength} characters.";
public static string StringStartsWith(in ArgumentInfo<string> argument, string value)
=> argument.Secure ? Require(argument) : $"{argument.Name} must start with '{value}'.";
public static string StringDoesNotStartWith(in ArgumentInfo<string> argument, string value)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot start with '{value}'.";
public static string StringEndsWith(in ArgumentInfo<string> argument, string value)
=> argument.Secure ? Require(argument) : $"{argument.Name} must end with '{value}'.";
public static string StringDoesNotEndWith(in ArgumentInfo<string> argument, string value)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot end with '{value}'.";
public static string StringMatches(in ArgumentInfo<string> argument, string pattern)
=> argument.Secure ? Require(argument) : $"No match in {argument.Name} could be found by the regular expression '{pattern}'.";
public static string StringMatchesTimeout(in ArgumentInfo<string> argument, string pattern, TimeSpan matchTimeout)
=> argument.Secure ? Require(argument) : $"No match in {argument.Name} could be found by the regular expression '{pattern}' in {matchTimeout}";
public static string StringDoesNotMatch(in ArgumentInfo<string> argument, string pattern)
=> argument.Secure ? Require(argument) : $"A match in {argument.Name} is found by the regular expression '{pattern}'.";
public static string StringDoesNotMatchTimeout(in ArgumentInfo<string> argument, string pattern, TimeSpan matchTimeout)
=> argument.Secure ? Require(argument) : $"{argument.Name} could not entirely be searched by the regular expression '{pattern}' due to time-out {matchTimeout}";
public static string True<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be true.";
public static string False<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be false.";
public static string Enum<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} is not an enum value.";
public static string EnumDefined<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} is not a defined {typeof(T)} member.";
public static string EnumNone<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot have any of its bits set.";
public static string EnumNotNone<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must have at least one of its bits set.";
public static string EnumHasFlag<T>(in ArgumentInfo<T> argument, T flag)
=> argument.Secure ? Require(argument) : $"{argument.Name} does not has the {flag} flag.";
public static string EnumDoesNotHaveFlag<T>(in ArgumentInfo<T> argument, T flag)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot have the {flag} flag.";
public static string CollectionEmpty<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must be empty.";
public static string CollectionNotEmpty<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot be empty.";
public static string CollectionCount<T>(in ArgumentInfo<T> argument, int count)
=> $"{argument.Name} must consist of {count} items.";
public static string CollectionNotCount<T>(in ArgumentInfo<T> argument, int count)
=> $"{argument.Name} cannot consist of {count} items.";
public static string CollectionMinCount<T>(in ArgumentInfo<T> argument, int minCount)
=> $"{argument.Name} must contain at least {minCount} items.";
public static string CollectionMaxCount<T>(in ArgumentInfo<T> argument, int maxCount)
=> $"{argument.Name} cannot contain more than {maxCount} items.";
public static string CollectionCountInRange<T>(in ArgumentInfo<T> argument, int minCount, int maxCount)
=> $"{argument.Name} must contain {minCount} to {maxCount} items.";
public static string CollectionContains<TCollection, TItem>(ArgumentInfo<TCollection> argument, TItem item)
=> argument.Secure ? Require(argument) : $"{argument.Name} must contain {ToString(item)}.";
public static string CollectionDoesNotContain<TCollection, TItem>(ArgumentInfo<TCollection> argument, TItem item)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot contain {ToString(item)}.";
public static string InCollection<T>(ArgumentInfo<T> argument, IEnumerable collection)
=> argument.Secure ? Require(argument) : $"{argument.Name} must be one of the following: {Join(collection)}";
public static string NotInCollection<T>(ArgumentInfo<T> argument, IEnumerable collection)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot be one of the following: {Join(collection)}";
public static string UriAbsolute(in ArgumentInfo<Uri> argument)
=> $"{argument.Name} must be an absolute URI.";
public static string UriRelative(in ArgumentInfo<Uri> argument)
=> $"{argument.Name} must be a relative URI.";
public static string UriScheme(in ArgumentInfo<Uri> argument, string scheme)
=> $"{argument.Name} must be an absolute URI with the {scheme} scheme.";
public static string UriNotScheme(in ArgumentInfo<Uri> argument, string scheme)
=> $"{argument.Name} cannot have the {scheme} scheme.";
public static string UriHttp(in ArgumentInfo<Uri> argument)
=> $"{argument.Name} must be an absolute URI with the HTTP scheme.";
public static string UriHttps(in ArgumentInfo<Uri> argument)
=> $"{argument.Name} must be an absolute URI with the HTTPS scheme.";
#if !NETSTANDARD1_0
public static string EmailHasHost(in ArgumentInfo<MailAddress> argument, string host)
=> argument.Secure ? Require(argument) : $"{argument.Name} must have the host '{host}'.";
public static string EmailDoesNotHaveHost(in ArgumentInfo<MailAddress> argument, string host)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot have the host '{host}'.";
public static string EmailHostIn(in ArgumentInfo<MailAddress> argument, IEnumerable<string> hosts)
=> argument.Secure ? Require(argument) : $"{argument.Name} must have one of the following hosts: {Join(hosts)}.";
public static string EmailHostNotIn(in ArgumentInfo<MailAddress> argument, IEnumerable<string> hosts)
=> argument.Secure ? Require(argument) : $"{argument.Name} cannot have one of the following hosts: {Join(hosts)}.";
public static string EmailHasDisplayName(in ArgumentInfo<MailAddress> argument)
=> $"{argument.Name} must have a display name specified.";
public static string EmailDoesNotHaveDisplayName(in ArgumentInfo<MailAddress> argument)
=> $"{argument.Name} cannot have a display name specified.";
#endif
public static string KindSpecified<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must have its kind specified.";
public static string KindUnspecified<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot have its kind specified.";
private static string ToString(object? obj) => obj?.ToString() ?? "null";
private static string Join(IEnumerable collection)
{
const int max = 5;
var objects = collection is IEnumerable<string> e
? e.Select(i => $"\"{i}\"") as IEnumerable<object>
: collection.Cast<object>();
var list = objects.Take(max + 1).ToList();
var ellipsis = list.Count > max;
if (ellipsis)
list.RemoveAt(max);
var result = string.Join(", ", list);
if (ellipsis)
result += "...";
return result;
}
}
}
}