1 using Mercury.Scanning;
3 using System.Collections.Generic;
6 namespace Mercury.Formats
9 internal static class FmtChk
11 public static void AssertCount<T>(
int count, ICollection<T> collection,
string message = null)
13 if (count > collection.Count)
14 throw new FormatException(message ??
"Not enough elements in collection");
17 public static void AssertNotEmpty<T>(ICollection<T> collection,
string message = null)
19 AssertCount(1, collection, message ??
"Collection is empty");
22 public static void AssertNotNull(
object obj,
string message)
25 throw new ArgumentNullException(
"message");
28 public static void AssertToken(Token token,
string value,
string message = null)
30 if (token.Value != value)
31 throw new FormatException(message ??
string.Format(
"Expected '{0}', got '{1}'", value, token.Value));
34 public static void AssertPattern<T>(ICollection<T> pattern, ICollection<T> source,
string message = null)
36 if (pattern.Count > source.Count)
37 throw new FormatException(message ??
"Invalid sequence");
39 Func<T, T, bool> f = (l, r) =>
41 if ((l != null) && (!l.Equals(r)))
42 throw new FormatException(message ??
string.Format(
"Expected '{0}', got '{1}'", l, r));
47 pattern.Zip(source, f);
50 public static void AssertPattern<T,U>(ICollection<T> pattern, ICollection<U> source, Func<U,T> convert,
string message = null)
52 if (pattern.Count > source.Count)
53 throw new FormatException(message ??
"Invalid sequence");
55 AssertPattern(pattern, source.Take(pattern.Count).Select(convert).ToArray(), message);
58 public static void AssertTrue(
bool condition,
string message)
60 if (!condition)
throw new FormatException(message);
63 public static void AssertFalse(
bool condition,
string message)
65 if (condition)
throw new FormatException(message);