using System; namespace GameCult.Thing.Contract; /// A value was outside its domain. Names the domain and its bound, never the value. public sealed class ThingDomainException : Exception { public ThingDomainException(string domain, string bound) : base($"{domain} must be {bound}") { Domain = domain; Bound = bound; } /// The domain that refused. public string Domain { get; } /// The bound that was not met. public string Bound { get; } } /// /// The value domains of the contract's numbers and local names. Every constructor whose field carries a domain calls the /// matching check, and every deserializer reaches a value through that constructor, so no door skips a check. /// public static class ThingDomain { /// A result's position in its invocation's answers, counting from 1. public static int Seq(int value) => value >= 1 ? value : throw new ThingDomainException("Seq", "at least 1"); /// A document or source version, never negative. public static long Version(long value) => value >= 0 ? value : throw new ThingDomainException("Version", "at least 0"); public static long? Version(long? value) => value.HasValue ? Version(value.Value) : null; /// A count, an extent or an index, never negative. public static int Count(int value) => value >= 0 ? value : throw new ThingDomainException("Count", "at least 0"); public static int? Count(int? value) => value.HasValue ? Count(value.Value) : null; /// A name the contract joins into a key: non-empty, no '.'. public static string Segment(string value) => string.IsNullOrEmpty(value) ? throw new ThingDomainException("Segment", "non-empty") : value.IndexOf('.') >= 0 ? throw new ThingDomainException("Segment", "free of '.'") : value; }