Skip to content

Commit 4b68928

Browse files
committed
feat: add MustBeEmailAddress overloads for Span<T> and Memory<T>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
1 parent 08adfbc commit 4b68928

File tree

2 files changed

+341
-0
lines changed

2 files changed

+341
-0
lines changed

Code/Light.GuardClauses/Check.MustBeEmailAddress.cs

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,337 @@ public static string MustBeEmailAddress(
107107

108108
return parameter;
109109
}
110+
111+
#if NET8_0
112+
/// <summary>
113+
/// Ensures that the span represents a valid email address using the default email regular expression
114+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws an <see cref="InvalidEmailAddressException" />.
115+
/// </summary>
116+
/// <param name="parameter">The span that will be validated as an email address.</param>
117+
/// <param name="parameterName">The name of the parameter (optional).</param>
118+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
119+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
120+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121+
public static ReadOnlySpan<char> MustBeEmailAddress(
122+
this ReadOnlySpan<char> parameter,
123+
[CallerArgumentExpression("parameter")] string? parameterName = null,
124+
string? message = null
125+
)
126+
{
127+
if (!parameter.IsEmailAddress())
128+
{
129+
Throw.InvalidEmailAddress(parameter, parameterName, message);
130+
}
131+
132+
return parameter;
133+
}
134+
135+
/// <summary>
136+
/// Ensures that the span represents a valid email address using the default email regular expression
137+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws your custom exception.
138+
/// </summary>
139+
/// <param name="parameter">The span that will be validated as an email address.</param>
140+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> is passed to this delegate.</param>
141+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address.</exception>
142+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
143+
public static ReadOnlySpan<char> MustBeEmailAddress(
144+
this ReadOnlySpan<char> parameter,
145+
ReadOnlySpanExceptionFactory<char> exceptionFactory
146+
)
147+
{
148+
if (!parameter.IsEmailAddress())
149+
{
150+
Throw.CustomSpanException(exceptionFactory, parameter);
151+
}
152+
153+
return parameter;
154+
}
155+
156+
/// <summary>
157+
/// Ensures that the span represents a valid email address using the provided regular expression,
158+
/// or otherwise throws an <see cref="InvalidEmailAddressException" />.
159+
/// </summary>
160+
/// <param name="parameter">The span that will be validated as an email address.</param>
161+
/// <param name="emailAddressPattern">The regular expression that determines if the span represents a valid email.</param>
162+
/// <param name="parameterName">The name of the parameter (optional).</param>
163+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
164+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
165+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="emailAddressPattern" /> is null.</exception>
166+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
167+
[ContractAnnotation("emailAddressPattern:null => halt")]
168+
public static ReadOnlySpan<char> MustBeEmailAddress(
169+
this ReadOnlySpan<char> parameter,
170+
Regex emailAddressPattern,
171+
[CallerArgumentExpression("parameter")] string? parameterName = null,
172+
string? message = null
173+
)
174+
{
175+
emailAddressPattern.MustNotBeNull(nameof(emailAddressPattern));
176+
177+
if (!parameter.IsEmailAddress(emailAddressPattern))
178+
{
179+
Throw.InvalidEmailAddress(parameter, parameterName, message);
180+
}
181+
182+
return parameter;
183+
}
184+
185+
/// <summary>
186+
/// Ensures that the span represents a valid email address using the provided regular expression,
187+
/// or otherwise throws your custom exception.
188+
/// </summary>
189+
/// <param name="parameter">The span that will be validated as an email address.</param>
190+
/// <param name="emailAddressPattern">The regular expression that determines if the span represents a valid email.</param>
191+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> and <paramref name="emailAddressPattern" /> are passed to this delegate.</param>
192+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address or when <paramref name="emailAddressPattern" /> is null.</exception>
193+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
194+
public static ReadOnlySpan<char> MustBeEmailAddress(
195+
this ReadOnlySpan<char> parameter,
196+
Regex emailAddressPattern,
197+
ReadOnlySpanExceptionFactory<char, Regex> exceptionFactory
198+
)
199+
{
200+
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract -- caller might have NRTs turned off
201+
if (emailAddressPattern is null || !parameter.IsEmailAddress(emailAddressPattern))
202+
{
203+
Throw.CustomSpanException(exceptionFactory, parameter, emailAddressPattern!);
204+
}
205+
206+
return parameter;
207+
}
208+
209+
/// <summary>
210+
/// Ensures that the span represents a valid email address using the default email regular expression
211+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws an <see cref="InvalidEmailAddressException" />.
212+
/// </summary>
213+
/// <param name="parameter">The span that will be validated as an email address.</param>
214+
/// <param name="parameterName">The name of the parameter (optional).</param>
215+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
216+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
217+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
218+
public static Span<char> MustBeEmailAddress(
219+
this Span<char> parameter,
220+
[CallerArgumentExpression("parameter")] string? parameterName = null,
221+
string? message = null
222+
)
223+
{
224+
((ReadOnlySpan<char>) parameter).MustBeEmailAddress(parameterName, message);
225+
return parameter;
226+
}
227+
228+
/// <summary>
229+
/// Ensures that the span represents a valid email address using the default email regular expression
230+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws your custom exception.
231+
/// </summary>
232+
/// <param name="parameter">The span that will be validated as an email address.</param>
233+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> is passed to this delegate.</param>
234+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address.</exception>
235+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
236+
public static Span<char> MustBeEmailAddress(
237+
this Span<char> parameter,
238+
ReadOnlySpanExceptionFactory<char> exceptionFactory
239+
)
240+
{
241+
((ReadOnlySpan<char>) parameter).MustBeEmailAddress(exceptionFactory);
242+
return parameter;
243+
}
244+
245+
/// <summary>
246+
/// Ensures that the span represents a valid email address using the provided regular expression,
247+
/// or otherwise throws an <see cref="InvalidEmailAddressException" />.
248+
/// </summary>
249+
/// <param name="parameter">The span that will be validated as an email address.</param>
250+
/// <param name="emailAddressPattern">The regular expression that determines if the span represents a valid email.</param>
251+
/// <param name="parameterName">The name of the parameter (optional).</param>
252+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
253+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
254+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="emailAddressPattern" /> is null.</exception>
255+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
256+
[ContractAnnotation("emailAddressPattern:null => halt")]
257+
public static Span<char> MustBeEmailAddress(
258+
this Span<char> parameter,
259+
Regex emailAddressPattern,
260+
[CallerArgumentExpression("parameter")] string? parameterName = null,
261+
string? message = null
262+
)
263+
{
264+
((ReadOnlySpan<char>) parameter).MustBeEmailAddress(emailAddressPattern, parameterName, message);
265+
return parameter;
266+
}
267+
268+
/// <summary>
269+
/// Ensures that the span represents a valid email address using the provided regular expression,
270+
/// or otherwise throws your custom exception.
271+
/// </summary>
272+
/// <param name="parameter">The span that will be validated as an email address.</param>
273+
/// <param name="emailAddressPattern">The regular expression that determines if the span represents a valid email.</param>
274+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> and <paramref name="emailAddressPattern" /> are passed to this delegate.</param>
275+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address or when <paramref name="emailAddressPattern" /> is null.</exception>
276+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
277+
public static Span<char> MustBeEmailAddress(
278+
this Span<char> parameter,
279+
Regex emailAddressPattern,
280+
ReadOnlySpanExceptionFactory<char, Regex> exceptionFactory
281+
)
282+
{
283+
((ReadOnlySpan<char>) parameter).MustBeEmailAddress(emailAddressPattern, exceptionFactory);
284+
return parameter;
285+
}
286+
287+
/// <summary>
288+
/// Ensures that the memory represents a valid email address using the default email regular expression
289+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws an <see cref="InvalidEmailAddressException" />.
290+
/// </summary>
291+
/// <param name="parameter">The memory that will be validated as an email address.</param>
292+
/// <param name="parameterName">The name of the parameter (optional).</param>
293+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
294+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
295+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
296+
public static Memory<char> MustBeEmailAddress(
297+
this Memory<char> parameter,
298+
[CallerArgumentExpression("parameter")] string? parameterName = null,
299+
string? message = null
300+
)
301+
{
302+
((ReadOnlySpan<char>) parameter.Span).MustBeEmailAddress(parameterName, message);
303+
return parameter;
304+
}
305+
306+
/// <summary>
307+
/// Ensures that the memory represents a valid email address using the default email regular expression
308+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws your custom exception.
309+
/// </summary>
310+
/// <param name="parameter">The memory that will be validated as an email address.</param>
311+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> is passed to this delegate.</param>
312+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address.</exception>
313+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
314+
public static Memory<char> MustBeEmailAddress(
315+
this Memory<char> parameter,
316+
ReadOnlySpanExceptionFactory<char> exceptionFactory
317+
)
318+
{
319+
((ReadOnlySpan<char>) parameter.Span).MustBeEmailAddress(exceptionFactory);
320+
return parameter;
321+
}
322+
323+
/// <summary>
324+
/// Ensures that the memory represents a valid email address using the provided regular expression,
325+
/// or otherwise throws an <see cref="InvalidEmailAddressException" />.
326+
/// </summary>
327+
/// <param name="parameter">The memory that will be validated as an email address.</param>
328+
/// <param name="emailAddressPattern">The regular expression that determines if the memory represents a valid email.</param>
329+
/// <param name="parameterName">The name of the parameter (optional).</param>
330+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
331+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
332+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="emailAddressPattern" /> is null.</exception>
333+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
334+
[ContractAnnotation("emailAddressPattern:null => halt")]
335+
public static Memory<char> MustBeEmailAddress(
336+
this Memory<char> parameter,
337+
Regex emailAddressPattern,
338+
[CallerArgumentExpression("parameter")] string? parameterName = null,
339+
string? message = null
340+
)
341+
{
342+
((ReadOnlySpan<char>) parameter.Span).MustBeEmailAddress(emailAddressPattern, parameterName, message);
343+
return parameter;
344+
}
345+
346+
/// <summary>
347+
/// Ensures that the memory represents a valid email address using the provided regular expression,
348+
/// or otherwise throws your custom exception.
349+
/// </summary>
350+
/// <param name="parameter">The memory that will be validated as an email address.</param>
351+
/// <param name="emailAddressPattern">The regular expression that determines if the memory represents a valid email.</param>
352+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> and <paramref name="emailAddressPattern" /> are passed to this delegate.</param>
353+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address or when <paramref name="emailAddressPattern" /> is null.</exception>
354+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
355+
public static Memory<char> MustBeEmailAddress(
356+
this Memory<char> parameter,
357+
Regex emailAddressPattern,
358+
ReadOnlySpanExceptionFactory<char, Regex> exceptionFactory
359+
)
360+
{
361+
((ReadOnlySpan<char>) parameter.Span).MustBeEmailAddress(emailAddressPattern, exceptionFactory);
362+
return parameter;
363+
}
364+
365+
/// <summary>
366+
/// Ensures that the memory represents a valid email address using the default email regular expression
367+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws an <see cref="InvalidEmailAddressException" />.
368+
/// </summary>
369+
/// <param name="parameter">The memory that will be validated as an email address.</param>
370+
/// <param name="parameterName">The name of the parameter (optional).</param>
371+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
372+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
373+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
374+
public static ReadOnlyMemory<char> MustBeEmailAddress(
375+
this ReadOnlyMemory<char> parameter,
376+
[CallerArgumentExpression("parameter")] string? parameterName = null,
377+
string? message = null
378+
)
379+
{
380+
parameter.Span.MustBeEmailAddress(parameterName, message);
381+
return parameter;
382+
}
383+
384+
/// <summary>
385+
/// Ensures that the memory represents a valid email address using the default email regular expression
386+
/// defined in <see cref="RegularExpressions.EmailRegex" />, or otherwise throws your custom exception.
387+
/// </summary>
388+
/// <param name="parameter">The memory that will be validated as an email address.</param>
389+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> is passed to this delegate.</param>
390+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address.</exception>
391+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
392+
public static ReadOnlyMemory<char> MustBeEmailAddress(
393+
this ReadOnlyMemory<char> parameter,
394+
ReadOnlySpanExceptionFactory<char> exceptionFactory
395+
)
396+
{
397+
parameter.Span.MustBeEmailAddress(exceptionFactory);
398+
return parameter;
399+
}
400+
401+
/// <summary>
402+
/// Ensures that the memory represents a valid email address using the provided regular expression,
403+
/// or otherwise throws an <see cref="InvalidEmailAddressException" />.
404+
/// </summary>
405+
/// <param name="parameter">The memory that will be validated as an email address.</param>
406+
/// <param name="emailAddressPattern">The regular expression that determines if the memory represents a valid email.</param>
407+
/// <param name="parameterName">The name of the parameter (optional).</param>
408+
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
409+
/// <exception cref="InvalidEmailAddressException">Thrown when <paramref name="parameter" /> is no valid email address.</exception>
410+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="emailAddressPattern" /> is null.</exception>
411+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
412+
[ContractAnnotation("emailAddressPattern:null => halt")]
413+
public static ReadOnlyMemory<char> MustBeEmailAddress(
414+
this ReadOnlyMemory<char> parameter,
415+
Regex emailAddressPattern,
416+
[CallerArgumentExpression("parameter")] string? parameterName = null,
417+
string? message = null
418+
)
419+
{
420+
parameter.Span.MustBeEmailAddress(emailAddressPattern, parameterName, message);
421+
return parameter;
422+
}
423+
424+
/// <summary>
425+
/// Ensures that the memory represents a valid email address using the provided regular expression,
426+
/// or otherwise throws your custom exception.
427+
/// </summary>
428+
/// <param name="parameter">The memory that will be validated as an email address.</param>
429+
/// <param name="emailAddressPattern">The regular expression that determines if the memory represents a valid email.</param>
430+
/// <param name="exceptionFactory">The delegate that creates your custom exception. A string created from <paramref name="parameter" /> and <paramref name="emailAddressPattern" /> are passed to this delegate.</param>
431+
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid email address or when <paramref name="emailAddressPattern" /> is null.</exception>
432+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
433+
public static ReadOnlyMemory<char> MustBeEmailAddress(
434+
this ReadOnlyMemory<char> parameter,
435+
Regex emailAddressPattern,
436+
ReadOnlySpanExceptionFactory<char, Regex> exceptionFactory
437+
)
438+
{
439+
parameter.Span.MustBeEmailAddress(emailAddressPattern, exceptionFactory);
440+
return parameter;
441+
}
442+
#endif
110443
}

0 commit comments

Comments
 (0)