IDataType.cs (191070B)
1 using Serde.Bin.De; 2 using Serde.Bin.Ser; 3 using Serde.De; 4 using Serde.Ser; 5 using Std; 6 using Std.Clone; 7 using Std.Cmp; 8 using Std.Convert; 9 using Std.Error; 10 using Std.Hashing; 11 using Std.Maybe; 12 using Std.Num; 13 using Std.Ops; 14 using Std.Result; 15 using Std.Wrappers; 16 using System; 17 using System.Data.SqlTypes; 18 using System.Diagnostics; 19 using System.Runtime.InteropServices; 20 #region Namespaces 21 #pragma warning disable CA1062, CA1066, CA1716, CA1720, CA1724, CA1815, CA2231, CS8603, IDE1006, CS8981 22 namespace SQLServer { 23 #region Types 24 public interface IDataType: IInto<string>, IBinSerializable { 25 26 #region Type-level Constructors 27 #endregion 28 29 #region Instance Constructors 30 #endregion 31 32 #region Type-level Fields 33 #endregion 34 35 #region Instance Fields 36 #endregion 37 38 #region Type-level Properties 39 #endregion 40 41 #region Instance Properties 42 public abstract object Val { get; } 43 public abstract bool IsNULL { get; } 44 #endregion 45 46 #region Type-level Functions 47 #endregion 48 49 #region Instance Functions 50 internal abstract bool TruncationWillOccur(in Column col, bool numericRoundAbort); 51 public abstract any IntoAny(); 52 #endregion 53 54 #region Operators 55 #endregion 56 57 #region Types 58 #endregion 59 } 60 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 40)] 61 public readonly struct any: ISum<Unit, long, byte[], bool, string, System.DateTime, SqlDateTime, System.DateTime, System.DateTimeOffset, SqlDecimal, double, byte[], int, SqlMoney, string, string, string, float, SqlDateTime, short, SqlMoney, sql_variant, string, System.TimeSpan, byte, System.Guid, byte[], string, string>, IInto<string>, IInto<any>, IBinDeserializable<any> { 62 63 #region Type-level Constructors 64 #endregion 65 66 #region Instance Constructors 67 public any() => (_bigint, _binary, _bit, _char, _date, _datetime, _datetime2, _datetimeoffset, _decimal, _float, _image, _int, _money, _nchar, _ntext, _nvarchar, _real, _smalldatetime, _smallint, _smallmoney, _sql_variant, _text, _time, _tinyint, _uniqueidentifier, _varbinary, _varchar, _xml, Var) = (SQLServer.bigint.NULL.Value, SQLServer.binary.NULL.Value!, SQLServer.bit.NULL.Value, SQLServer.@char.NULL.Value!, SQLServer.date.NULL.Value, SQLServer.datetime.NULL.Value, SQLServer.datetime2.NULL.Value, SQLServer.datetimeoffset.NULL.Value, SQLServer.@decimal.NULL.Value, SQLServer.@float.NULL.Value, SQLServer.image.NULL.Value!, SQLServer.@int.NULL.Value, SQLServer.money.NULL.Value, SQLServer.nchar.NULL.Value!, SQLServer.ntext.NULL.Value!, SQLServer.nvarchar.NULL.Value!, SQLServer.real.NULL.Value, SQLServer.smalldatetime.NULL.Value, SQLServer.smallint.NULL.Value, SQLServer.smallmoney.NULL.Value, SQLServer.sql_variant.NULL, SQLServer.text.NULL.Value!, SQLServer.time.NULL.Value, SQLServer.tinyint.NULL.Value, SQLServer.uniqueidentifier.NULL.Value, SQLServer.varbinary.NULL.Value!, SQLServer.varchar.NULL.Value!, SQLServer.xml.NULL.Value!, Tag.NULL); 68 any(Tag kind, byte[] val) : this() => (Var, _binary) = (kind, val); 69 any(Tag kind, string val) : this() => (Var, _char) = (kind, val); 70 any(Tag kind, System.DateTime val) : this() => (Var, _date) = (kind, val); 71 any(Tag kind, SqlDateTime val) : this() => (Var, _datetime) = (kind, val); 72 any(Tag kind, SqlMoney val) : this() => (Var, _money) = (kind, val); 73 any(long val) : this() => (Var, _bigint) = (Tag.bigint, val); 74 any(bool val) : this() => (Var, _bit) = (Tag.bit, val); 75 any(System.DateTimeOffset val) : this() => (Var, _datetimeoffset) = (Tag.datetimeoffset, val); 76 any(SqlDecimal val) : this() => (Var, _decimal) = (Tag.@decimal, val); 77 any(double val) : this() => (Var, _float) = (Tag.@float, val); 78 any(int val) : this() => (Var, _int) = (Tag.@int, val); 79 any(float val) : this() => (Var, _real) = (Tag.real, val); 80 any(short val) : this() => (Var, _smallint) = (Tag.smallint, val); 81 any(sql_variant val) : this() => (Var, _sql_variant) = (Tag.sql_variant, val); 82 any(System.TimeSpan val) : this() => (Var, _time) = (Tag.time, val); 83 any(byte val) : this() => (Var, _tinyint) = (Tag.tinyint, val); 84 any(System.Guid val) : this() => (Var, _uniqueidentifier) = (Tag.uniqueidentifier, val); 85 #endregion 86 87 #region Type-level Fields 88 public static readonly any NULL = new(); 89 static readonly Fn<bigint, any> fn_bigint = (x) => new(x.Value); 90 static readonly Fn<binary, any> fn_binary = (x) => new(Tag.binary, x.Value!); 91 static readonly Fn<bit, any> fn_bit = (x) => new(x.Value); 92 static readonly Fn<@char, any> fn_char = (x) => new(Tag.@char, x.Value!); 93 static readonly Fn<date, any> fn_date = (x) => new(x.Value); 94 static readonly Fn<datetime, any> fn_datetime = (x) => new(Tag.datetime, x.Value); 95 static readonly Fn<datetime2, any> fn_datetime2 = (x) => new(x.Value); 96 static readonly Fn<datetimeoffset, any> fn_datetimeoffset = (x) => new(x.Value); 97 static readonly Fn<@decimal, any> fn_decimal = (x) => new(x.Value); 98 static readonly Fn<@float, any> fn_float = (x) => new(x.Value); 99 static readonly Fn<image, any> fn_image = (x) => new(Tag.image, x.Value!); 100 static readonly Fn<@int, any> fn_int = (x) => new(x.Value); 101 static readonly Fn<money, any> fn_money = (x) => new(Tag.money, x.Value); 102 static readonly Fn<nchar, any> fn_nchar = (x) => new(Tag.nchar, x.Value!); 103 static readonly Fn<ntext, any> fn_ntext = (x) => new(Tag.ntext, x.Value!); 104 static readonly Fn<nvarchar, any> fn_nvarchar = (x) => new(Tag.nvarchar, x.Value!); 105 static readonly Fn<real, any> fn_real = (x) => new(x.Value); 106 static readonly Fn<smalldatetime, any> fn_smalldatetime = (x) => new(Tag.smalldatetime, x.Value); 107 static readonly Fn<smallint, any> fn_smallint = (x) => new(x.Value); 108 static readonly Fn<smallmoney, any> fn_smallmoney = (x) => new(Tag.smallmoney, x.Value); 109 static readonly Fn<sql_variant, any> fn_sql_variant = (x) => new(x); 110 static readonly Fn<text, any> fn_text = (x) => new(Tag.text, x.Value!); 111 static readonly Fn<time, any> fn_time = (x) => new(x.Value); 112 static readonly Fn<tinyint, any> fn_tinyint = (x) => new(x.Value); 113 static readonly Fn<uniqueidentifier, any> fn_uniqueidentifier = (x) => new(x.Value); 114 static readonly Fn<varbinary, any> fn_varbinary = (x) => new(Tag.varbinary, x.Value!); 115 static readonly Fn<varchar, any> fn_varchar = (x) => new(Tag.varchar, x.Value!); 116 static readonly Fn<xml, any> fn_xml = (x) => new(Tag.xml, x.Value!); 117 internal static readonly string[] variants = Enum.GetNames<Tag>(); 118 #endregion 119 120 #region Instance Fields 121 [FieldOffset(0)] readonly long _bigint; 122 [FieldOffset(0)] readonly bool _bit; 123 [FieldOffset(0)] readonly System.DateTime _date; 124 [FieldOffset(0)] readonly SqlDateTime _datetime; 125 [FieldOffset(0)] readonly System.DateTime _datetime2; 126 [FieldOffset(0)] readonly System.DateTimeOffset _datetimeoffset; 127 [FieldOffset(0)] readonly SqlDecimal _decimal; 128 [FieldOffset(0)] readonly double _float; 129 [FieldOffset(0)] readonly int _int; 130 [FieldOffset(0)] readonly SqlMoney _money; 131 [FieldOffset(0)] readonly float _real; 132 [FieldOffset(0)] readonly SqlDateTime _smalldatetime; 133 [FieldOffset(0)] readonly short _smallint; 134 [FieldOffset(0)] readonly SqlMoney _smallmoney; 135 [FieldOffset(0)] readonly sql_variant _sql_variant; 136 [FieldOffset(0)] readonly System.TimeSpan _time; 137 [FieldOffset(0)] readonly byte _tinyint; 138 [FieldOffset(0)] readonly System.Guid _uniqueidentifier; 139 [FieldOffset(24)] readonly byte[] _binary; 140 [FieldOffset(24)] readonly string _char; 141 [FieldOffset(24)] readonly byte[] _image; 142 [FieldOffset(24)] readonly string _nchar; 143 [FieldOffset(24)] readonly string _ntext; 144 [FieldOffset(24)] readonly string _nvarchar; 145 [FieldOffset(24)] readonly string _text; 146 [FieldOffset(24)] readonly byte[] _varbinary; 147 [FieldOffset(24)] readonly string _varchar; 148 [FieldOffset(24)] readonly string _xml; 149 [FieldOffset(32)] public readonly Tag Var; 150 #endregion 151 152 #region Type-level Properties 153 #endregion 154 155 #region Instance Properties 156 public readonly Var29 Variant => (Var29)Var; 157 public readonly Unit Variant0 => IntoNULL(); 158 public readonly long Variant1 => IntoBigint(); 159 public readonly byte[] Variant2 => IntoBinary(); 160 public readonly bool Variant3 => IntoBit(); 161 public readonly string Variant4 => IntoChar(); 162 public readonly System.DateTime Variant5 => IntoDate(); 163 public readonly SqlDateTime Variant6 => IntoDatetime(); 164 public readonly System.DateTime Variant7 => IntoDatetime2(); 165 public readonly System.DateTimeOffset Variant8 => IntoDatetimeoffset(); 166 public readonly SqlDecimal Variant9 => IntoDecimal(); 167 public readonly double Variant10 => IntoFloat(); 168 public readonly byte[] Variant11 => IntoImage(); 169 public readonly int Variant12 => IntoInt(); 170 public readonly SqlMoney Variant13 => IntoMoney(); 171 public readonly string Variant14 => IntoNchar(); 172 public readonly string Variant15 => IntoNtext(); 173 public readonly string Variant16 => IntoNvarchar(); 174 public readonly float Variant17 => IntoReal(); 175 public readonly SqlDateTime Variant18 => IntoSmalldatetime(); 176 public readonly short Variant19 => IntoSmallint(); 177 public readonly SqlMoney Variant20 => IntoSmallmoney(); 178 public readonly sql_variant Variant21 => IntoSql_variant(); 179 public readonly string Variant22 => IntoText(); 180 public readonly System.TimeSpan Variant23 => IntoTime(); 181 public readonly byte Variant24 => IntoTinyint(); 182 public readonly System.Guid Variant25 => IntoUniqueidentifier(); 183 public readonly byte[] Variant26 => IntoVarbinary(); 184 public readonly string Variant27 => IntoVarchar(); 185 public readonly string Variant28 => IntoXml(); 186 #endregion 187 188 #region Type-level Functions 189 public static Result<any[], Error> DeserializeRefProd(ref Deserializer des) { 190 191 var anyIter = DeserializeAsanyIter(des, out var err); 192 193 if (err.IsSome) { 194 return new(err.Unwrap()); 195 } else { 196 var row = new any[anyIter.Arity.IntoUshort()]; 197 Maybe<Result<any, Error>> val; 198 Result<any, Error> res; 199 var i = 0; 200 201 while (i < row.Length && (val = anyIter.Next()).IsSome) { 202 res = val.Unwrap(); 203 204 if (res.IsOK) { 205 row[i++] = res.Unwrap(); 206 } else { 207 return new(res.UnwrapErr()); 208 } 209 } 210 211 if (i == row.Length) { 212 des = anyIter.Deserializer; 213 return new(row); 214 } else { 215 return new(Error.InvalidType(Unexpected.Other($"{i.ToString()}-arity SQLServer.RefProd."), (@string)$"{row.Length.ToString()}-arity SQLServer.RefProd.")); 216 } 217 } 218 } 219 220 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1021:Avoid out parameters", Justification = "anyIter cannot be used as a type argument (e.g., Std.Maybe.Maybe<anyIter>), so we use err as a way to designate an error.")] 221 public static anyIter DeserializeAsanyIter(Deserializer des, out Maybe<Error> err) => anyIter.New(des, out err); 222 // If one does not want to deserialize into any, then one can call this function to obtain the variant 223 // which in turn can inform code which IDataType's static Deserialize function to call to obtain the underlying 224 // value (e.g., OK(Tag.bigint) -> bigint.Deserialize(ref des) -> Result<long, Error>. 225 public static Result<Tag, Error> DeserializeVariant(ref Deserializer des) { 226 227 var des2 = des; 228 var resTag = des2.DeserializeByte(); 229 230 if (resTag.IsOK) { 231 var tag = (Tag)resTag.Unwrap(); 232 233 if (Enum.IsDefined(tag)) { 234 des = des2; 235 return new(tag); 236 } else { 237 return new(Error.UnknownVariant(tag.ToString(), variants)); 238 } 239 } else { 240 return new(resTag.UnwrapErr()); 241 } 242 } 243 public static Result<any, Error> De(ref Deserializer des) { 244 245 var des2 = des; 246 var resByte = des2.DeserializeByte(); 247 248 if (resByte.IsOK) { 249 var tag = (Tag)resByte.Unwrap(); 250 var resAny = tag switch { 251 Tag.NULL => new(NULL), 252 Tag.bigint => SQLServer.bigint.De(ref des2).Map(fn_bigint), 253 Tag.binary => SQLServer.binary.De(ref des2).Map(fn_binary), 254 Tag.bit => SQLServer.bit.De(ref des2).Map(fn_bit), 255 Tag.@char => SQLServer.@char.De(ref des2).Map(fn_char), 256 Tag.date => SQLServer.date.De(ref des2).Map(fn_date), 257 Tag.datetime => SQLServer.datetime.De(ref des2).Map(fn_datetime), 258 Tag.datetime2 => SQLServer.datetime2.De(ref des2).Map(fn_datetime2), 259 Tag.datetimeoffset => SQLServer.datetimeoffset.De(ref des2).Map(fn_datetimeoffset), 260 Tag.@decimal => SQLServer.@decimal.De(ref des2).Map(fn_decimal), 261 Tag.@float => SQLServer.@float.De(ref des2).Map(fn_float), 262 Tag.image => SQLServer.image.De(ref des2).Map(fn_image), 263 Tag.@int => SQLServer.@int.De(ref des2).Map(fn_int), 264 Tag.money => SQLServer.money.De(ref des2).Map(fn_money), 265 Tag.nchar => SQLServer.nchar.De(ref des2).Map(fn_nchar), 266 Tag.ntext => SQLServer.ntext.De(ref des2).Map(fn_ntext), 267 Tag.nvarchar => SQLServer.nvarchar.De(ref des2).Map(fn_nvarchar), 268 Tag.real => SQLServer.real.De(ref des2).Map(fn_real), 269 Tag.smalldatetime => SQLServer.smalldatetime.De(ref des2).Map(fn_smalldatetime), 270 Tag.smallint => SQLServer.smallint.De(ref des2).Map(fn_smallint), 271 Tag.smallmoney => SQLServer.smallmoney.De(ref des2).Map(fn_smallmoney), 272 Tag.sql_variant => SQLServer.sql_variant.De(ref des2).Map(fn_sql_variant), 273 Tag.text => SQLServer.text.De(ref des2).Map(fn_text), 274 Tag.time => SQLServer.time.De(ref des2).Map(fn_time), 275 Tag.tinyint => SQLServer.tinyint.De(ref des2).Map(fn_tinyint), 276 Tag.uniqueidentifier => SQLServer.uniqueidentifier.De(ref des2).Map(fn_uniqueidentifier), 277 Tag.varbinary => SQLServer.varbinary.De(ref des2).Map(fn_varbinary), 278 Tag.varchar => SQLServer.varchar.De(ref des2).Map(fn_varchar), 279 Tag.xml => SQLServer.xml.De(ref des2).Map(fn_xml), 280 _ => new(Error.UnknownVariant(tag.ToString(), variants)), 281 }; 282 if (resAny.IsOK) { des = des2; } 283 return resAny; 284 } else { 285 return new(resByte.UnwrapErr()); 286 } 287 } 288 public static Result<any, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 289 290 var des2 = des; 291 var resByte = des2.DeserializeByte(); 292 293 if (resByte.IsOK) { 294 var tag = (Tag)resByte.Unwrap(); 295 var resAny = tag switch { 296 Tag.NULL => new(NULL), 297 Tag.bigint => SQLServer.bigint.Deserialize(ref des2).Map(fn_bigint), 298 Tag.binary => SQLServer.binary.Deserialize(ref des2).Map(fn_binary), 299 Tag.bit => SQLServer.bit.Deserialize(ref des2).Map(fn_bit), 300 Tag.@char => SQLServer.@char.Deserialize(ref des2).Map(fn_char), 301 Tag.date => SQLServer.date.Deserialize(ref des2).Map(fn_date), 302 Tag.datetime => SQLServer.datetime.Deserialize(ref des2).Map(fn_datetime), 303 Tag.datetime2 => SQLServer.datetime2.Deserialize(ref des2).Map(fn_datetime2), 304 Tag.datetimeoffset => SQLServer.datetimeoffset.Deserialize(ref des2).Map(fn_datetimeoffset), 305 Tag.@decimal => SQLServer.@decimal.Deserialize(ref des2).Map(fn_decimal), 306 Tag.@float => SQLServer.@float.Deserialize(ref des2).Map(fn_float), 307 Tag.image => SQLServer.image.Deserialize(ref des2).Map(fn_image), 308 Tag.@int => SQLServer.@int.Deserialize(ref des2).Map(fn_int), 309 Tag.money => SQLServer.money.Deserialize(ref des2).Map(fn_money), 310 Tag.nchar => SQLServer.nchar.Deserialize(ref des2).Map(fn_nchar), 311 Tag.ntext => SQLServer.ntext.Deserialize(ref des2).Map(fn_ntext), 312 Tag.nvarchar => SQLServer.nvarchar.Deserialize(ref des2).Map(fn_nvarchar), 313 Tag.real => SQLServer.real.Deserialize(ref des2).Map(fn_real), 314 Tag.smalldatetime => SQLServer.smalldatetime.Deserialize(ref des2).Map(fn_smalldatetime), 315 Tag.smallint => SQLServer.smallint.Deserialize(ref des2).Map(fn_smallint), 316 Tag.smallmoney => SQLServer.smallmoney.Deserialize(ref des2).Map(fn_smallmoney), 317 Tag.sql_variant => SQLServer.sql_variant.Deserialize(ref des2).Map(fn_sql_variant), 318 Tag.text => SQLServer.text.Deserialize(ref des2).Map(fn_text), 319 Tag.time => SQLServer.time.Deserialize(ref des2).Map(fn_time), 320 Tag.tinyint => SQLServer.tinyint.Deserialize(ref des2).Map(fn_tinyint), 321 Tag.uniqueidentifier => SQLServer.uniqueidentifier.Deserialize(ref des2).Map(fn_uniqueidentifier), 322 Tag.varbinary => SQLServer.varbinary.Deserialize(ref des2).Map(fn_varbinary), 323 Tag.varchar => SQLServer.varchar.Deserialize(ref des2).Map(fn_varchar), 324 Tag.xml => SQLServer.xml.Deserialize(ref des2).Map(fn_xml), 325 _ => new(Error.UnknownVariant(tag.ToString(), variants)), 326 }; 327 if (resAny.IsOK) { des = des2; } 328 return resAny; 329 } else { 330 return new(resByte.UnwrapErr()); 331 } 332 } 333 public static any bigint(bigint val) => val.IsNULL ? NULL : new(val.Value); 334 public static any binary(binary val) => val.IsNULL ? NULL : new(Tag.binary, val.Value!); 335 public static any bit(bit val) => val.IsNULL ? NULL : new(val.Value); 336 public static any @char(@char val) => val.IsNULL ? NULL : new(Tag.@char, val.Value!); 337 public static any date(date val) => val.IsNULL ? NULL : new(Tag.date, val.Value!); 338 public static any datetime(datetime val) => val.IsNULL ? NULL : new(Tag.datetime, val.Value); 339 public static any datetime2(datetime2 val) => val.IsNULL ? NULL : new(Tag.datetime2, val.Value!); 340 public static any datetimeoffset(datetimeoffset val) => val.IsNULL ? NULL : new(val.Value); 341 public static any @decimal(@decimal val) => val.IsNULL ? NULL : new(val.Value); 342 public static any @float(@float val) => val.IsNULL ? NULL : new(val.Value); 343 public static any image(image val) => val.IsNULL ? NULL : new(Tag.image, val.Value!); 344 public static any @int(@int val) => val.IsNULL ? NULL : new(val.Value); 345 public static any money(money val) => val.IsNULL ? NULL : new(Tag.money, val.Value); 346 public static any nchar(nchar val) => val.IsNULL ? NULL : new(Tag.nchar, val.Value!); 347 public static any ntext(ntext val) => val.IsNULL ? NULL : new(Tag.ntext, val.Value!); 348 public static any nvarchar(nvarchar val) => val.IsNULL ? NULL : new(Tag.nvarchar, val.Value!); 349 public static any real(real val) => val.IsNULL ? NULL : new(val.Value); 350 public static any smalldatetime(smalldatetime val) => val.IsNULL ? NULL : new(Tag.smalldatetime, val.Value); 351 public static any smallint(smallint val) => val.IsNULL ? NULL : new(val.Value); 352 public static any smallmoney(smallmoney val) => val.IsNULL ? NULL : new(Tag.smallmoney, val.Value); 353 public static any sql_variant(sql_variant val) => val.IsNULL ? NULL : new(val); 354 public static any text(text val) => val.IsNULL ? NULL : new(Tag.text, val.Value!); 355 public static any time(time val) => val.IsNULL ? NULL : new(val.Value); 356 public static any tinyint(tinyint val) => val.IsNULL ? NULL : new(val.Value); 357 public static any uniqueidentifier(uniqueidentifier val) => val.IsNULL ? NULL : new(val.Value); 358 public static any varbinary(varbinary val) => val.IsNULL ? NULL : new(Tag.varbinary, val.Value!); 359 public static any varchar(varchar val) => val.IsNULL ? NULL : new(Tag.varchar, val.Value!); 360 public static any xml(xml val) => val.IsNULL ? NULL : new(Tag.xml, val.Value!); 361 #endregion 362 363 #region Instance Functions 364 public override readonly bool Equals(object? _) => false; 365 public override readonly int GetHashCode() => 0; 366 public readonly any Into() => this; 367 public readonly string IntoString() => ToString(); 368 readonly string IInto<string>.Into() => IntoString(); 369 public readonly Unit IntoNULL() => Var == Tag.NULL ? new Unit() : throw new InvalidOperationException($"The any variant {Var.ToString()} is not NULL!"); 370 public readonly long IntoBigint() => Var == Tag.bigint ? _bigint : throw new InvalidOperationException($"The any variant {Var.ToString()} is not bigint!"); 371 public readonly byte[] IntoBinary() => Var == Tag.binary ? _binary : throw new InvalidOperationException($"The any variant {Var.ToString()} is not binary!"); 372 public readonly bool IntoBit() => Var == Tag.bit ? _bit : throw new InvalidOperationException($"The any variant {Var.ToString()} is not bit!"); 373 public readonly string IntoChar() => Var == Tag.@char ? _char : throw new InvalidOperationException($"The any variant {Var.ToString()} is not @char!"); 374 public readonly System.DateTime IntoDate() => Var == Tag.date ? _date : throw new InvalidOperationException($"The any variant {Var.ToString()} is not date!"); 375 public readonly SqlDateTime IntoDatetime() => Var == Tag.datetime ? _datetime : throw new InvalidOperationException($"The any variant {Var.ToString()} is not datetime!"); 376 public readonly System.DateTime IntoDatetime2() => Var == Tag.datetime2 ? _datetime2 : throw new InvalidOperationException($"The any variant {Var.ToString()} is not datetime2!"); 377 public readonly System.DateTimeOffset IntoDatetimeoffset() => Var == Tag.datetimeoffset ? _datetimeoffset : throw new InvalidOperationException($"The any variant {Var.ToString()} is not datetimeoffset!"); 378 public readonly SqlDecimal IntoDecimal() => Var == Tag.@decimal ? _decimal : throw new InvalidOperationException($"The any variant {Var.ToString()} is not @decimal!"); 379 public readonly double IntoFloat() => Var == Tag.@float ? _float : throw new InvalidOperationException($"The any variant {Var.ToString()} is not @float!"); 380 public readonly byte[] IntoImage() => Var == Tag.image ? _image : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varchar!"); 381 public readonly int IntoInt() => Var == Tag.@int ? _int : throw new InvalidOperationException($"The any variant {Var.ToString()} is not an @int!"); 382 public readonly SqlMoney IntoMoney() => Var == Tag.money ? _money : throw new InvalidOperationException($"The any variant {Var.ToString()} is not money!"); 383 public readonly string IntoNchar() => Var == Tag.nchar ? _nchar : throw new InvalidOperationException($"The any variant {Var.ToString()} is not an nchar!"); 384 public readonly string IntoNtext() => Var == Tag.ntext ? _ntext : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varchar!"); 385 public readonly string IntoNvarchar() => Var == Tag.nvarchar ? _nvarchar : throw new InvalidOperationException($"The any variant {Var.ToString()} is not an nvarchar!"); 386 public readonly float IntoReal() => Var == Tag.real ? _real : throw new InvalidOperationException($"The any variant {Var.ToString()} is not real!"); 387 public readonly SqlDateTime IntoSmalldatetime() => Var == Tag.smalldatetime ? _smalldatetime : throw new InvalidOperationException($"The any variant {Var.ToString()} is not smalldatetime!"); 388 public readonly short IntoSmallint() => Var == Tag.smallint ? _smallint : throw new InvalidOperationException($"The any variant {Var.ToString()} is not smallint!"); 389 public readonly SqlMoney IntoSmallmoney() => Var == Tag.smallmoney ? _smallmoney : throw new InvalidOperationException($"The any variant {Var.ToString()} is not smallmoney!"); 390 public readonly sql_variant IntoSql_variant() => Var == Tag.sql_variant ? _sql_variant : throw new InvalidOperationException($"The any variant {Var.ToString()} is not smallmoney!"); 391 public readonly string IntoText() => Var == Tag.text ? _text : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varchar!"); 392 public readonly System.TimeSpan IntoTime() => Var == Tag.time ? _time : throw new InvalidOperationException($"The any variant {Var.ToString()} is not time!"); 393 public readonly byte IntoTinyint() => Var == Tag.tinyint ? _tinyint : throw new InvalidOperationException($"The any variant {Var.ToString()} is not tinyint!"); 394 public readonly System.Guid IntoUniqueidentifier() => Var == Tag.uniqueidentifier ? _uniqueidentifier : throw new InvalidOperationException($"The any variant {Var.ToString()} is not uniqueidentifier!"); 395 public readonly byte[] IntoVarbinary() => Var == Tag.varbinary ? _varbinary : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varbinary!"); 396 public readonly string IntoVarchar() => Var == Tag.varchar ? _varchar : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varchar!"); 397 public readonly string IntoXml() => Var == Tag.xml ? _xml : throw new InvalidOperationException($"The any variant {Var.ToString()} is not varchar!"); 398 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1065:Do not raise exceptions in unexpected locations", Justification = "Need to do this in the event an unhandled variant exists.")] 399 public override readonly string ToString() => $@"{Var.ToString()}({Var switch { 400 Tag.NULL => "NULL", 401 Tag.bigint => _bigint.ToString(), 402 Tag.binary => _binary.AsSpan().UpperHex(), 403 Tag.bit => _bit.ToString(), 404 Tag.@char => _char, 405 Tag.date => _date.ToString("yyyy-MM-dd"), 406 Tag.datetime => _datetime.Value.ToString("yyyy-MM-ddTHH:mm:ss.fff"), 407 Tag.datetime2 => _datetime2.ToString("O"), 408 Tag.datetimeoffset => _datetimeoffset.ToString("O"), 409 Tag.@decimal => _decimal.ToString(), 410 Tag.@float => _float.ToString("G17"), 411 Tag.image => _image.AsSpan().UpperHex(), 412 Tag.@int => _int.ToString(), 413 Tag.money => _money.Value.ToString("N4"), 414 Tag.nchar => _nchar, 415 Tag.ntext => _ntext, 416 Tag.nvarchar => _nvarchar, 417 Tag.real => _real.ToString("G9"), 418 Tag.smalldatetime => _smalldatetime.Value.ToString("yyyy-MM-ddTHH:mm"), 419 Tag.smallint => _smallint.ToString(), 420 Tag.smallmoney => _smallmoney.Value.ToString("N4"), 421 Tag.sql_variant => _sql_variant.IntoString(), 422 Tag.text => _text, 423 Tag.time => _time.ToString("c"), 424 Tag.tinyint => _tinyint.ToString(), 425 Tag.uniqueidentifier => _uniqueidentifier.ToString("D"), 426 Tag.varbinary => _varbinary.AsSpan().UpperHex(), 427 Tag.varchar => _varchar, 428 Tag.xml => _xml, 429 _ => throw new InvalidOperationException($"The any variant {Var.ToString()} is invalid!"), 430 }})"; 431 readonly Result<any, Bottom> ITryInto<any, Bottom>.TryInto() => new(this); 432 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 433 #endregion 434 435 #region Operators 436 #endregion 437 438 #region Types 439 // MUST match sql_variant.Tag for overlapped variants! 440 public enum Tag: ulong { 441 NULL = ulong.MinValue, 442 bigint = 1ul, 443 binary = 2ul, 444 bit = 3ul, 445 @char = 4ul, 446 date = 5ul, 447 datetime = 6ul, 448 datetime2 = 7ul, 449 datetimeoffset = 8ul, 450 @decimal = 9ul, 451 @float = 10ul, 452 image = 11ul, 453 @int = 12ul, 454 money = 13ul, 455 nchar = 14ul, 456 ntext = 15ul, 457 nvarchar = 16ul, 458 real = 17ul, 459 smalldatetime = 18ul, 460 smallint = 19ul, 461 smallmoney = 20ul, 462 sql_variant = 21ul, 463 text = 22ul, 464 time = 23ul, 465 tinyint = 24ul, 466 uniqueidentifier = 25ul, 467 varbinary = 26ul, 468 varchar = 27ul, 469 xml = 28ul, 470 } 471 #endregion 472 } 473 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 16)] 474 public readonly struct bigint: IDataType, IInto<bigint>, IBinDeserializable<bigint> { 475 476 #region Type-level Constructors 477 #endregion 478 479 #region Instance Constructors 480 public bigint() => (Value, _isNULL) = (0L, ulong.MinValue); 481 public bigint(long val) => (Value, _isNULL) = (val, ulong.MaxValue); 482 #endregion 483 484 #region Type-level Fields 485 public static readonly bigint NULL = new(); 486 #endregion 487 488 #region Instance Fields 489 [FieldOffset(0)] internal readonly long Value; 490 [FieldOffset(8)] readonly ulong _isNULL; 491 #endregion 492 493 #region Type-level Properties 494 #endregion 495 496 #region Instance Properties 497 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 498 public readonly bool IsNULL => _isNULL == ulong.MinValue; 499 #endregion 500 501 #region Type-level Functions 502 public static Result<bigint, Error> De(ref Deserializer des) { 503 504 var des2 = des; 505 var resLong = des2.DeserializeLong(); 506 507 if (resLong.IsOK) { 508 des = des2; 509 return new(new bigint(resLong.Unwrap())); 510 } else { 511 return new(resLong.UnwrapErr()); 512 } 513 } 514 public static Result<bigint, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 515 516 var des2 = des; 517 var resLong = des2.DeserializeLong(); 518 519 if (resLong.IsOK) { 520 des = des2; 521 return new(new bigint(resLong.Unwrap())); 522 } else { 523 return new(resLong.UnwrapErr()); 524 } 525 } 526 public static bigint New(long val) => new(val); 527 #endregion 528 529 #region Instance Functions 530 public override readonly bool Equals(object? _) => false; 531 public override readonly int GetHashCode() => 0; 532 public readonly bigint Into() => this; 533 public readonly string IntoString() => ToString(); 534 public readonly any IntoAny() => any.bigint(this); 535 readonly string IInto<string>.Into() => IntoString(); 536 public readonly Unit Ser(ref Serializer ser) { 537 538 if (IsNULL) { 539 return ser.SerByte((byte)any.Tag.NULL); 540 } else { 541 _ = ser.SerByte((byte)any.Tag.bigint); 542 return ser.SerLong(Value); 543 } 544 } 545 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 546 547 if (IsNULL) { 548 return ser.SerializeByte((byte)any.Tag.NULL); 549 } else { 550 var res = ser.SerializeByte((byte)any.Tag.bigint); 551 return res.IsErr ? res : ser.SerializeLong(Value); 552 } 553 } 554 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 555 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 556 readonly Result<bigint, Bottom> ITryInto<bigint, Bottom>.TryInto() => new(this); 557 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 558 #endregion 559 560 #region Operators 561 public static implicit operator bigint(long val) => new(val); 562 #endregion 563 564 #region Types 565 #endregion 566 } 567 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 568 public readonly struct binary: IDataType, IInto<binary>, IBinDeserializable<binary> { 569 570 #region Type-level Constructors 571 #endregion 572 573 #region Instance Constructors 574 public binary() => Value = null; 575 internal binary(byte[] val) => Value = val; 576 #endregion 577 578 #region Type-level Fields 579 static readonly @string ushort1To8000 = "1–8000"; 580 public static readonly binary NULL = new(); 581 #endregion 582 583 #region Instance Fields 584 [FieldOffset(0)] internal readonly byte[]? Value; 585 #endregion 586 587 #region Type-level Properties 588 #endregion 589 590 #region Instance Properties 591 public readonly object Val => Value; 592 public readonly bool IsNULL => Value is null; 593 #endregion 594 595 #region Type-level Functions 596 public static Result<binary, Error> De(ref Deserializer des) { 597 598 var des2 = des; 599 var resUshort = des2.DeserializeUshort(); 600 601 if (resUshort.IsOK) { 602 var len = resUshort.Unwrap(); 603 604 if (len is > ushort.MinValue and <= 8000) { 605 var bytes = des2.Read(len); 606 607 if ((uint)bytes.Length == len) { 608 des = des2; 609 return new(new binary(bytes.ToArray())); 610 } else { 611 return new(Error.InvalidLength((ulong)bytes.Length, (@ushort)len)); 612 } 613 } else { 614 return new(Error.InvalidLength(len, ushort1To8000)); 615 } 616 } else { 617 return new(resUshort.UnwrapErr()); 618 } 619 } 620 public static Result<binary, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 621 622 var des2 = des; 623 var resUshort = des2.DeserializeUshort(); 624 625 if (resUshort.IsOK) { 626 var len = resUshort.Unwrap(); 627 628 if (len is > ushort.MinValue and <= 8000) { 629 Span<byte> bytes = stackalloc byte[len]; 630 uint l; 631 des2.Deserialize(bytes); 632 633 if ((l = des2.Deserialize(bytes)) == len) { 634 des = des2; 635 return new(new binary(bytes.ToArray())); 636 } else { 637 return new(Error.InvalidLength((ulong)l, (@ushort)len)); 638 } 639 } else { 640 return new(Error.InvalidLength(len, ushort1To8000)); 641 } 642 } else { 643 return new(resUshort.UnwrapErr()); 644 } 645 } 646 public static Result<binary, OutOfRangeErr> New(byte[] val) => val.Length is 0 or > 8000 ? new(OutOfRangeErr.OutOfRange) : new(new binary(val)); 647 public static Result<binary, OutOfRangeErr> New(ReadOnlySpan<byte> val) => val.Length is 0 or > 8000 ? new(OutOfRangeErr.OutOfRange) : new(new binary(val.ToArray())); 648 #endregion 649 650 #region Instance Functions 651 public override readonly bool Equals(object? _) => false; 652 public override readonly int GetHashCode() => 0; 653 public readonly binary Into() => this; 654 public readonly any IntoAny() => any.binary(this); 655 public readonly string IntoString() => ToString(); 656 readonly string IInto<string>.Into() => IntoString(); 657 public readonly Unit Ser(ref Serializer ser) { 658 659 if (IsNULL) { 660 return ser.SerByte((byte)any.Tag.NULL); 661 } else { 662 _ = ser.SerByte((byte)any.Tag.binary); 663 _ = ser.SerUshort((ushort)Value!.Length); 664 return ser.Ser(Value.AsSpan()); 665 } 666 } 667 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 668 669 if (IsNULL) { 670 return ser.SerializeByte((byte)any.Tag.NULL); 671 } else { 672 var res = ser.SerializeByte((byte)any.Tag.binary); 673 if (res.IsErr) { return res; } 674 var res2 = ser.SerializeUshort((ushort)Value!.Length); 675 return res2.IsErr ? res2 : ser.Serialize(Value.AsSpan()); 676 } 677 } 678 public override readonly string ToString() => IsNULL ? "NULL" : Value.AsSpan().UpperHex(); 679 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => Value!.Length > col.MaxLength.Unwrap(); 680 readonly Result<binary, Bottom> ITryInto<binary, Bottom>.TryInto() => new(this); 681 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 682 #endregion 683 684 #region Operators 685 #endregion 686 687 #region Types 688 #endregion 689 } 690 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 1, Size = 2)] 691 public readonly struct bit: IDataType, IInto<bit>, IBinDeserializable<bit> { 692 693 #region Type-level Constructors 694 #endregion 695 696 #region Instance Constructors 697 public bit() => (Value, _isNULL) = (false, byte.MinValue); 698 public bit(bool val) => (Value, _isNULL) = (val, byte.MaxValue); 699 #endregion 700 701 #region Type-level Fields 702 public static readonly bit NULL = new(); 703 #endregion 704 705 #region Instance Fields 706 [FieldOffset(0)] internal readonly bool Value; 707 [FieldOffset(1)] readonly byte _isNULL; 708 #endregion 709 710 #region Type-level Properties 711 #endregion 712 713 #region Instance Properties 714 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 715 public readonly bool IsNULL => _isNULL == byte.MinValue; 716 #endregion 717 718 #region Type-level Functions 719 public static Result<bit, Error> De(ref Deserializer des) { 720 721 var des2 = des; 722 var resBool = des2.DeserializeBool(); 723 724 if (resBool.IsOK) { 725 des = des2; 726 return new(new bit(resBool.Unwrap())); 727 } else { 728 return new(resBool.UnwrapErr()); 729 } 730 } 731 public static Result<bit, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 732 733 var des2 = des; 734 var resBool = des2.DeserializeBool(); 735 736 if (resBool.IsOK) { 737 des = des2; 738 return new(new bit(resBool.Unwrap())); 739 } else { 740 return new(resBool.UnwrapErr()); 741 } 742 } 743 public static bit New(bool val) => new(val); 744 #endregion 745 746 #region Instance Functions 747 public override readonly bool Equals(object? _) => false; 748 public override readonly int GetHashCode() => 0; 749 public readonly bit Into() => this; 750 public readonly string IntoString() => ToString(); 751 public readonly any IntoAny() => any.bit(this); 752 readonly string IInto<string>.Into() => IntoString(); 753 public readonly Unit Ser(ref Serializer ser) { 754 755 if (IsNULL) { 756 return ser.SerByte((byte)any.Tag.NULL); 757 } else { 758 _ = ser.SerByte((byte)any.Tag.bit); 759 return ser.SerBool(Value); 760 } 761 } 762 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 763 764 if (IsNULL) { 765 return ser.SerializeByte((byte)any.Tag.NULL); 766 } else { 767 var res = ser.SerializeByte((byte)any.Tag.bit); 768 return res.IsErr ? res : ser.SerializeBool(Value); 769 } 770 } 771 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 772 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 773 readonly Result<bit, Bottom> ITryInto<bit, Bottom>.TryInto() => new(this); 774 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 775 #endregion 776 777 #region Operators 778 public static implicit operator bit(bool val) => new(val); 779 #endregion 780 781 #region Types 782 #endregion 783 } 784 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 785 public readonly struct @char: IDataType, IInto<@char>, ISerializable, IBinDeserializable<@char> { 786 787 #region Type-level Constructors 788 #endregion 789 790 #region Instance Constructors 791 public @char() => Value = null; 792 internal @char(string val) => Value = val; 793 #endregion 794 795 #region Type-level Fields 796 static readonly @string ushort1To8000 = "1–8000"; 797 public static readonly @char NULL = new(); 798 #endregion 799 800 #region Instance Fields 801 [FieldOffset(0)] internal readonly string? Value; 802 #endregion 803 804 #region Type-level Properties 805 #endregion 806 807 #region Instance Properties 808 public readonly object Val => Value; 809 public readonly bool IsNULL => Value is null; 810 #endregion 811 812 #region Type-level Functions 813 public static Result<@char, Error> De(ref Deserializer des) { 814 815 var des2 = des; 816 var resUshort = des2.DeserializeUshort(); 817 818 if (resUshort.IsOK) { 819 var len = resUshort.Unwrap(); 820 821 if (len is > ushort.MinValue and <= 8000) { 822 var chars = des2.DeserializeChars(len); 823 824 if ((uint)chars.Length == len) { 825 des = des2; 826 return new(new @char(new string(chars))); 827 } else { 828 return new(Error.InvalidLength((ulong)chars.Length, (@ushort)len)); 829 } 830 } else { 831 return new(Error.InvalidLength(len, ushort1To8000)); 832 } 833 } else { 834 return new(resUshort.UnwrapErr()); 835 } 836 } 837 public static Result<@char, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 838 839 var des2 = des; 840 var resUshort = des2.DeserializeUshort(); 841 842 if (resUshort.IsOK) { 843 var len = resUshort.Unwrap(); 844 845 if (len is > ushort.MinValue and <= 8000) { 846 uint l; 847 Span<char> chars = stackalloc char[len]; 848 849 if ((l = des2.DeserializeChars(chars)) == len) { 850 des = des2; 851 return new(new @char(new string(chars))); 852 } else { 853 return new(Error.InvalidLength((ulong)l, (@ushort)len)); 854 } 855 } else { 856 return new(Error.InvalidLength(len, ushort1To8000)); 857 } 858 } else { 859 return new(resUshort.UnwrapErr()); 860 } 861 } 862 public static Result<@char, OutOfRangeErr> New(string val) => val.Length is 0 or > 8000 ? new(OutOfRangeErr.OutOfRange) : new(new @char(val)); 863 #endregion 864 865 #region Instance Functions 866 public override readonly bool Equals(object? _) => false; 867 public override readonly int GetHashCode() => 0; 868 public readonly @char Into() => this; 869 public readonly string IntoString() => ToString(); 870 public readonly any IntoAny() => any.@char(this); 871 readonly string IInto<string>.Into() => IntoString(); 872 public readonly Unit Ser(ref Serializer ser) { 873 874 if (IsNULL) { 875 return ser.SerByte((byte)any.Tag.NULL); 876 } else { 877 _ = ser.SerByte((byte)any.Tag.@char); 878 _ = ser.SerUshort((ushort)Value!.Length); 879 return ser.SerChars(Value.AsSpan()); 880 } 881 } 882 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 883 884 if (IsNULL) { 885 return ser.SerializeByte((byte)any.Tag.NULL); 886 } else { 887 var res = ser.SerializeByte((byte)any.Tag.@char); 888 if (res.IsErr) { return res; } 889 var res2 = ser.SerializeUshort((ushort)Value!.Length); 890 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 891 } 892 } 893 public override readonly string ToString() => IsNULL ? "NULL" : Value; 894 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => Value!.Length > col.MaxLength.Unwrap(); 895 readonly Result<@char, Bottom> ITryInto<@char, Bottom>.TryInto() => new(this); 896 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 897 #endregion 898 899 #region Operators 900 #endregion 901 902 #region Types 903 #endregion 904 } 905 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 906 public readonly struct date: IDataType, IInto<date>, IBinDeserializable<date> { 907 908 #region Type-level Constructors 909 #endregion 910 911 #region Instance Constructors 912 public date() => Value = new System.DateTime(1, 1, 1, 1, 0, 0); 913 internal date(System.DateTime val) => Value = val; 914 #endregion 915 916 #region Type-level Fields 917 static readonly @string validDate = "DateTime.Date == DateTime"; 918 public static readonly date NULL = new(); 919 #endregion 920 921 #region Instance Fields 922 [FieldOffset(0)] internal readonly System.DateTime Value; 923 #endregion 924 925 #region Type-level Properties 926 #endregion 927 928 #region Instance Properties 929 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 930 public readonly bool IsNULL => Value.Hour == 1; 931 #endregion 932 933 #region Type-level Functions 934 public static Result<date, Error> De(ref Deserializer des) { 935 936 var des2 = des; 937 var resDateTime = des2.DeserializeDateTime(); 938 939 if (resDateTime.IsOK) { 940 var date = resDateTime.Unwrap(); 941 942 if (date == date.Date) { 943 des = des2; 944 return new(new date(date)); 945 } else { 946 return new(Error.InvalidValue(Unexpected.DateTime(date), validDate)); 947 } 948 } else { 949 return new(resDateTime.UnwrapErr()); 950 } 951 } 952 public static Result<date, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 953 954 var des2 = des; 955 var resDateTime = des2.DeserializeDateTime(); 956 957 if (resDateTime.IsOK) { 958 var date = resDateTime.Unwrap(); 959 960 if (date == date.Date) { 961 des = des2; 962 return new(new date(date)); 963 } else { 964 return new(Error.InvalidValue(Unexpected.DateTime(date), validDate)); 965 } 966 } else { 967 return new(resDateTime.UnwrapErr()); 968 } 969 } 970 public static date New(System.DateTime val) => new(val.Date); 971 #endregion 972 973 #region Instance Functions 974 public override readonly bool Equals(object? _) => false; 975 public override readonly int GetHashCode() => 0; 976 public readonly date Into() => this; 977 public readonly string IntoString() => ToString(); 978 readonly string IInto<string>.Into() => IntoString(); 979 public readonly any IntoAny() => any.date(this); 980 public readonly Unit Ser(ref Serializer ser) { 981 982 if (IsNULL) { 983 return ser.SerByte((byte)any.Tag.NULL); 984 } else { 985 _ = ser.SerByte((byte)any.Tag.date); 986 return ser.SerDateTime(Value); 987 } 988 } 989 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 990 991 if (IsNULL) { 992 return ser.SerializeByte((byte)any.Tag.NULL); 993 } else { 994 var res = ser.SerializeByte((byte)any.Tag.date); 995 return res.IsErr ? res : ser.SerializeDateTime(Value); 996 } 997 } 998 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("yyyy-MM-dd"); 999 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1000 readonly Result<date, Bottom> ITryInto<date, Bottom>.TryInto() => new(this); 1001 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1002 #endregion 1003 1004 #region Operators 1005 #endregion 1006 1007 #region Types 1008 #endregion 1009 } 1010 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 12)] 1011 public readonly struct datetime: IDataType, IInto<datetime>, IBinDeserializable<datetime> { 1012 1013 #region Type-level Constructors 1014 #endregion 1015 1016 #region Instance Constructors 1017 public datetime() => Value = SqlDateTime.Null; 1018 public datetime(SqlDateTime val) => Value = val; 1019 #endregion 1020 1021 #region Type-level Fields 1022 static readonly @string validDatetime = "The DateTime value from an SqlDateTime."; 1023 public static readonly datetime NULL = new(); 1024 #endregion 1025 1026 #region Instance Fields 1027 [FieldOffset(0)] internal readonly SqlDateTime Value; 1028 #endregion 1029 1030 #region Type-level Properties 1031 #endregion 1032 1033 #region Instance Properties 1034 public readonly object Val => Value; 1035 public readonly bool IsNULL => Value.IsNull; 1036 #endregion 1037 1038 #region Type-level Functions 1039 public static Result<datetime, Error> De(ref Deserializer des) { 1040 1041 var des2 = des; 1042 var resDateTime = des2.DeserializeDateTime(); 1043 1044 if (resDateTime.IsOK) { 1045 var date = resDateTime.Unwrap(); 1046 1047 if (date >= SqlDateTime.MinValue.Value && date <= SqlDateTime.MaxValue.Value && date == new SqlDateTime(date).Value) { 1048 des = des2; 1049 return new(new datetime(new SqlDateTime(date))); 1050 } else { 1051 return new(Error.InvalidValue(Unexpected.DateTime(date), validDatetime)); 1052 } 1053 } else { 1054 return new(resDateTime.UnwrapErr()); 1055 } 1056 } 1057 public static Result<datetime, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1058 1059 var des2 = des; 1060 var resDateTime = des2.DeserializeDateTime(); 1061 1062 if (resDateTime.IsOK) { 1063 var date = resDateTime.Unwrap(); 1064 1065 if (date >= SqlDateTime.MinValue.Value && date <= SqlDateTime.MaxValue.Value && date == new SqlDateTime(date).Value) { 1066 des = des2; 1067 return new(new datetime(new SqlDateTime(date))); 1068 } else { 1069 return new(Error.InvalidValue(Unexpected.DateTime(date), validDatetime)); 1070 } 1071 } else { 1072 return new(resDateTime.UnwrapErr()); 1073 } 1074 } 1075 public static datetime New(SqlDateTime val) => new(val); 1076 #endregion 1077 1078 #region Instance Functions 1079 public override readonly bool Equals(object? _) => false; 1080 public override readonly int GetHashCode() => 0; 1081 public readonly datetime Into() => this; 1082 public readonly string IntoString() => ToString(); 1083 readonly string IInto<string>.Into() => IntoString(); 1084 public readonly any IntoAny() => any.datetime(this); 1085 public readonly Unit Ser(ref Serializer ser) { 1086 1087 if (IsNULL) { 1088 return ser.SerByte((byte)any.Tag.NULL); 1089 } else { 1090 _ = ser.SerByte((byte)any.Tag.datetime); 1091 return ser.SerDateTime(Value.Value); 1092 } 1093 } 1094 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1095 1096 if (IsNULL) { 1097 return ser.SerializeByte((byte)any.Tag.NULL); 1098 } else { 1099 var res = ser.SerializeByte((byte)any.Tag.datetime); 1100 return res.IsErr ? res : ser.SerializeDateTime(Value.Value); 1101 } 1102 } 1103 public override readonly string ToString() => IsNULL ? "NULL" : Value.Value.ToString("yyyy-MM-ddTHH:mm:ss.fff"); 1104 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1105 readonly Result<datetime, Bottom> ITryInto<datetime, Bottom>.TryInto() => new(this); 1106 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1107 #endregion 1108 1109 #region Operators 1110 public static implicit operator SqlDateTime(datetime val) => val.Value; 1111 public static implicit operator datetime(SqlDateTime val) => new(val); 1112 #endregion 1113 1114 #region Types 1115 #endregion 1116 } 1117 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 16)] 1118 public readonly struct datetime2: IDataType, IInto<datetime2>, IBinDeserializable<datetime2> { 1119 1120 #region Type-level Constructors 1121 #endregion 1122 1123 #region Instance Constructors 1124 public datetime2() => (Value, _isNULL) = (System.DateTime.MinValue, ulong.MinValue); 1125 public datetime2(System.DateTime val) => (Value, _isNULL) = (val, ulong.MaxValue); 1126 #endregion 1127 1128 #region Type-level Fields 1129 public static readonly datetime2 NULL = new(); 1130 #endregion 1131 1132 #region Instance Fields 1133 [FieldOffset(0)] internal readonly System.DateTime Value; 1134 [FieldOffset(8)] readonly ulong _isNULL; 1135 #endregion 1136 1137 #region Type-level Properties 1138 #endregion 1139 1140 #region Instance Properties 1141 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 1142 public readonly bool IsNULL => _isNULL == ulong.MinValue; 1143 #endregion 1144 1145 #region Type-level Functions 1146 public static Result<datetime2, Error> De(ref Deserializer des) { 1147 1148 var des2 = des; 1149 var resDateTime = des2.DeserializeDateTime(); 1150 1151 if (resDateTime.IsOK) { 1152 des = des2; 1153 return new(new datetime2(resDateTime.Unwrap())); 1154 } else { 1155 return new(resDateTime.UnwrapErr()); 1156 } 1157 } 1158 public static Result<datetime2, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1159 1160 var des2 = des; 1161 var resDateTime = des2.DeserializeDateTime(); 1162 1163 if (resDateTime.IsOK) { 1164 des = des2; 1165 return new(new datetime2(resDateTime.Unwrap())); 1166 } else { 1167 return new(resDateTime.UnwrapErr()); 1168 } 1169 } 1170 public static datetime2 New(System.DateTime val) => new(val); 1171 #endregion 1172 1173 #region Instance Functions 1174 public override readonly bool Equals(object? _) => false; 1175 public override readonly int GetHashCode() => 0; 1176 public readonly datetime2 Into() => this; 1177 public readonly string IntoString() => ToString(); 1178 readonly string IInto<string>.Into() => IntoString(); 1179 public readonly any IntoAny() => any.datetime2(this); 1180 public readonly Unit Ser(ref Serializer ser) { 1181 1182 if (IsNULL) { 1183 return ser.SerByte((byte)any.Tag.NULL); 1184 } else { 1185 _ = ser.SerByte((byte)any.Tag.datetime2); 1186 return ser.SerDateTime(Value); 1187 } 1188 } 1189 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1190 1191 if (IsNULL) { 1192 return ser.SerializeByte((byte)any.Tag.NULL); 1193 } else { 1194 var res = ser.SerializeByte((byte)any.Tag.datetime2); 1195 return res.IsErr ? res : ser.SerializeDateTime(Value); 1196 } 1197 } 1198 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("O"); 1199 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1200 readonly Result<datetime2, Bottom> ITryInto<datetime2, Bottom>.TryInto() => new(this); 1201 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1202 #endregion 1203 1204 #region Operators 1205 public static implicit operator datetime2(System.DateTime val) => new(val); 1206 #endregion 1207 1208 #region Types 1209 #endregion 1210 } 1211 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 24)] 1212 public readonly struct datetimeoffset: IDataType, IInto<datetimeoffset>, IBinDeserializable<datetimeoffset> { 1213 1214 #region Type-level Constructors 1215 #endregion 1216 1217 #region Instance Constructors 1218 public datetimeoffset() => (Value, _isNULL) = (System.DateTimeOffset.MinValue, ulong.MinValue); 1219 public datetimeoffset(System.DateTimeOffset val) => (Value, _isNULL) = (val, ulong.MaxValue); 1220 #endregion 1221 1222 #region Type-level Fields 1223 public static readonly datetimeoffset NULL = new(); 1224 #endregion 1225 1226 #region Instance Fields 1227 [FieldOffset(0)] internal readonly System.DateTimeOffset Value; 1228 [FieldOffset(16)] readonly ulong _isNULL; 1229 #endregion 1230 1231 #region Type-level Properties 1232 #endregion 1233 1234 #region Instance Properties 1235 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 1236 public readonly bool IsNULL => _isNULL == ulong.MinValue; 1237 #endregion 1238 1239 #region Type-level Functions 1240 public static Result<datetimeoffset, Error> De(ref Deserializer des) { 1241 1242 var des2 = des; 1243 var resDateTimeOffset = des2.DeserializeDateTimeOffset(); 1244 1245 if (resDateTimeOffset.IsOK) { 1246 des = des2; 1247 return new(new datetimeoffset(resDateTimeOffset.Unwrap())); 1248 } else { 1249 return new(resDateTimeOffset.UnwrapErr()); 1250 } 1251 } 1252 public static Result<datetimeoffset, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1253 1254 var des2 = des; 1255 var resDateTimeOffset = des2.DeserializeDateTimeOffset(); 1256 1257 if (resDateTimeOffset.IsOK) { 1258 des = des2; 1259 return new(new datetimeoffset(resDateTimeOffset.Unwrap())); 1260 } else { 1261 return new(resDateTimeOffset.UnwrapErr()); 1262 } 1263 } 1264 public static datetimeoffset New(System.DateTimeOffset val) => new(val); 1265 #endregion 1266 1267 #region Instance Functions 1268 public override readonly bool Equals(object? _) => false; 1269 public override readonly int GetHashCode() => 0; 1270 public readonly datetimeoffset Into() => this; 1271 public readonly string IntoString() => ToString(); 1272 readonly string IInto<string>.Into() => IntoString(); 1273 public readonly any IntoAny() => any.datetimeoffset(this); 1274 public readonly Unit Ser(ref Serializer ser) { 1275 1276 if (IsNULL) { 1277 return ser.SerByte((byte)any.Tag.NULL); 1278 } else { 1279 _ = ser.SerByte((byte)any.Tag.datetimeoffset); 1280 return ser.SerDateTimeOffset(Value); 1281 } 1282 } 1283 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1284 1285 if (IsNULL) { 1286 return ser.SerializeByte((byte)any.Tag.NULL); 1287 } else { 1288 var res = ser.SerializeByte((byte)any.Tag.datetimeoffset); 1289 return res.IsErr ? res : ser.SerializeDateTimeOffset(Value); 1290 } 1291 } 1292 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("O"); 1293 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1294 readonly Result<datetimeoffset, Bottom> ITryInto<datetimeoffset, Bottom>.TryInto() => new(this); 1295 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1296 #endregion 1297 1298 #region Operators 1299 public static implicit operator datetimeoffset(System.DateTimeOffset val) => new(val); 1300 #endregion 1301 1302 #region Types 1303 #endregion 1304 } 1305 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 20)] 1306 public readonly struct @decimal: IDataType, IInto<@decimal>, IBinDeserializable<@decimal> { 1307 1308 #region Type-level Constructors 1309 #endregion 1310 1311 #region Instance Constructors 1312 public @decimal() => Value = SqlDecimal.Null; 1313 public @decimal(SqlDecimal val) => Value = val; 1314 #endregion 1315 1316 #region Type-level Fields 1317 static readonly @string fourInts = "4 ints"; 1318 static readonly @string validSqlDecimal = "Expected the precision, scale, sign, and data of an SqlDecimal; however despite successfully deserializing those components, they did not lead to a valid SqlDecimal."; 1319 public static readonly @decimal NULL = new(); 1320 #endregion 1321 1322 #region Instance Fields 1323 [FieldOffset(0)] internal readonly SqlDecimal Value; 1324 #endregion 1325 1326 #region Type-level Properties 1327 #endregion 1328 1329 #region Instance Properties 1330 public readonly object Val => Value; 1331 public readonly bool IsNULL => Value.IsNull; 1332 #endregion 1333 1334 #region Type-level Functions 1335 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Need to since we shouldn't make assumptions about how SqlDecimal with throw an exception.")] 1336 public static Result<@decimal, Error> De(ref Deserializer des) { 1337 1338 var des2 = des; 1339 var resByte = des2.DeserializeByte(); 1340 1341 if (resByte.IsOK) { 1342 var prec = resByte.Unwrap(); 1343 resByte = des2.DeserializeByte(); 1344 1345 if (resByte.IsOK) { 1346 var scale = resByte.Unwrap(); 1347 var resBool = des2.DeserializeBool(); 1348 1349 if (resBool.IsOK) { 1350 var data = des2.DeserializeSliceUnsafe<int>(4u); 1351 1352 if (data.Length == 4) { 1353 try { 1354 var val = new SqlDecimal(prec, scale, resBool.Unwrap(), data[0], data[1], data[2], data[3]); 1355 des = des2; 1356 return new(new @decimal(val)); 1357 } catch (Exception) { 1358 return new(Error.Custom(validSqlDecimal)); 1359 } 1360 } else { 1361 return new(Error.InvalidLength((ulong)data.Length, fourInts)); 1362 } 1363 } else { 1364 return new(resBool.UnwrapErr()); 1365 } 1366 } else { 1367 return new(resByte.UnwrapErr()); 1368 } 1369 } else { 1370 return new(resByte.UnwrapErr()); 1371 } 1372 } 1373 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Need to since we shouldn't make assumptions about how SqlDecimal with throw an exception.")] 1374 public static Result<@decimal, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1375 1376 var des2 = des; 1377 var resByte = des2.DeserializeByte(); 1378 1379 if (resByte.IsOK) { 1380 var prec = resByte.Unwrap(); 1381 resByte = des2.DeserializeByte(); 1382 1383 if (resByte.IsOK) { 1384 var scale = resByte.Unwrap(); 1385 var resBool = des2.DeserializeBool(); 1386 1387 if (resBool.IsOK) { 1388 Span<byte> data = stackalloc byte[16]; 1389 1390 if (des2.Deserialize(data) == 4) { 1391 try { 1392 unsafe { 1393 fixed (byte* ptr = data) { 1394 var ptr2 = (int*)ptr; 1395 var val = new SqlDecimal(prec, scale, resBool.Unwrap(), *ptr2++, *ptr2++, *ptr2++, *ptr2); 1396 des = des2; 1397 return new(new @decimal(val)); 1398 } 1399 } 1400 } catch (Exception) { 1401 return new(Error.Custom(validSqlDecimal)); 1402 } 1403 } else { 1404 return new(Error.InvalidLength((ulong)data.Length, fourInts)); 1405 } 1406 } else { 1407 return new(resBool.UnwrapErr()); 1408 } 1409 } else { 1410 return new(resByte.UnwrapErr()); 1411 } 1412 } else { 1413 return new(resByte.UnwrapErr()); 1414 } 1415 } 1416 [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "Should not happen.")] 1417 static Prod<SqlDecimal, SqlDecimal> GetBoundary(byte prec, byte scale) => prec switch { 1418 1 => scale switch { 1419 <= 1 => new(new(prec, scale, false, 9, 0, 0, 0), new(prec, scale, true, 9, 0, 0, 0)), 1420 _ => throw new ArgumentOutOfRangeException(), 1421 }, 1422 2 => scale switch { 1423 <= 2 => new(new(prec, scale, false, 99, 0, 0, 0), new(prec, scale, true, 99, 0, 0, 0)), 1424 _ => throw new ArgumentOutOfRangeException(), 1425 }, 1426 3 => scale switch { 1427 <= 3 => new(new(prec, scale, false, 999, 0, 0, 0), new(prec, scale, true, 999, 0, 0, 0)), 1428 _ => throw new ArgumentOutOfRangeException(), 1429 }, 1430 4 => scale switch { 1431 <= 4 => new(new(prec, scale, false, 9999, 0, 0, 0), new(prec, scale, true, 9999, 0, 0, 0)), 1432 _ => throw new ArgumentOutOfRangeException(), 1433 }, 1434 5 => scale switch { 1435 <= 5 => new(new(prec, scale, false, 99999, 0, 0, 0), new(prec, scale, true, 99999, 0, 0, 0)), 1436 _ => throw new ArgumentOutOfRangeException(), 1437 }, 1438 6 => scale switch { 1439 <= 6 => new(new(prec, scale, false, 999999, 0, 0, 0), new(prec, scale, true, 999999, 0, 0, 0)), 1440 _ => throw new ArgumentOutOfRangeException(), 1441 }, 1442 7 => scale switch { 1443 <= 7 => new(new(prec, scale, false, 9999999, 0, 0, 0), new(prec, scale, true, 9999999, 0, 0, 0)), 1444 _ => throw new ArgumentOutOfRangeException(), 1445 }, 1446 8 => scale switch { 1447 <= 8 => new(new(prec, scale, false, 99999999, 0, 0, 0), new(prec, scale, true, 99999999, 0, 0, 0)), 1448 _ => throw new ArgumentOutOfRangeException(), 1449 }, 1450 9 => scale switch { 1451 <= 9 => new(new(prec, scale, false, 999999999, 0, 0, 0), new(prec, scale, true, 999999999, 0, 0, 0)), 1452 _ => throw new ArgumentOutOfRangeException(), 1453 }, 1454 10 => scale switch { 1455 <= 10 => new(new(prec, scale, false, 1410065407, 2, 0, 0), new(prec, scale, true, 1410065407, 2, 0, 0)), 1456 _ => throw new ArgumentOutOfRangeException(), 1457 }, 1458 11 => scale switch { 1459 <= 11 => new(new(prec, scale, false, 1215752191, 23, 0, 0), new(prec, scale, true, 1215752191, 23, 0, 0)), 1460 _ => throw new ArgumentOutOfRangeException(), 1461 }, 1462 12 => scale switch { 1463 <= 12 => new(new(prec, scale, false, -727379969, 232, 0, 0), new(prec, scale, true, -727379969, 232, 0, 0)), 1464 _ => throw new ArgumentOutOfRangeException(), 1465 }, 1466 13 => scale switch { 1467 <= 13 => new(new(prec, scale, false, 1316134911, 2328, 0, 0), new(prec, scale, true, 1316134911, 2328, 0, 0)), 1468 _ => throw new ArgumentOutOfRangeException(), 1469 }, 1470 14 => scale switch { 1471 <= 14 => new(new(prec, scale, false, 276447231, 23283, 0, 0), new(prec, scale, true, 276447231, 23283, 0, 0)), 1472 _ => throw new ArgumentOutOfRangeException(), 1473 }, 1474 15 => scale switch { 1475 <= 15 => new(new(prec, scale, false, -1530494977, 232830, 0, 0), new(prec, scale, true, -1530494977, 232830, 0, 0)), 1476 _ => throw new ArgumentOutOfRangeException(), 1477 }, 1478 16 => scale switch { 1479 <= 16 => new(new(prec, scale, false, 1874919423, 2328306, 0, 0), new(prec, scale, true, 1874919423, 2328306, 0, 0)), 1480 _ => throw new ArgumentOutOfRangeException(), 1481 }, 1482 17 => scale switch { 1483 <= 17 => new(new(prec, scale, false, 1569325055, 23283064, 0, 0), new(prec, scale, true, 1569325055, 23283064, 0, 0)), 1484 _ => throw new ArgumentOutOfRangeException(), 1485 }, 1486 18 => scale switch { 1487 <= 18 => new(new(prec, scale, false, -1486618625, 232830643, 0, 0), new(prec, scale, true, -1486618625, 232830643, 0, 0)), 1488 _ => throw new ArgumentOutOfRangeException(), 1489 }, 1490 19 => scale switch { 1491 <= 19 => new(new(prec, scale, false, -1981284353, -1966660860, 0, 0), new(prec, scale, true, -1981284353, -1966660860, 0, 0)), 1492 _ => throw new ArgumentOutOfRangeException(), 1493 }, 1494 20 => scale switch { 1495 <= 20 => new(new(prec, scale, false, 1661992959, 1808227885, 5, 0), new(prec, scale, true, 1661992959, 1808227885, 5, 0)), 1496 _ => throw new ArgumentOutOfRangeException(), 1497 }, 1498 21 => scale switch { 1499 <= 21 => new(new(prec, scale, false, -559939585, 902409669, 54, 0), new(prec, scale, true, -559939585, 902409669, 54, 0)), 1500 _ => throw new ArgumentOutOfRangeException(), 1501 }, 1502 22 => scale switch { 1503 <= 22 => new(new(prec, scale, false, -1304428545, 434162106, 542, 0), new(prec, scale, true, -1304428545, 434162106, 542, 0)), 1504 _ => throw new ArgumentOutOfRangeException(), 1505 }, 1506 23 => scale switch { 1507 <= 23 => new(new(prec, scale, false, -159383553, 46653770, 5421, 0), new(prec, scale, true, -159383553, 46653770, 5421, 0)), 1508 _ => throw new ArgumentOutOfRangeException(), 1509 }, 1510 24 => scale switch { 1511 <= 24 => new(new(prec, scale, false, -1593835521, 466537709, 54210, 0), new(prec, scale, true, -1593835521, 466537709, 54210, 0)), 1512 _ => throw new ArgumentOutOfRangeException(), 1513 }, 1514 25 => scale switch { 1515 <= 25 => new(new(prec, scale, false, 1241513983, 370409800, 542101, 0), new(prec, scale, true, 1241513983, 370409800, 542101, 0)), 1516 _ => throw new ArgumentOutOfRangeException(), 1517 }, 1518 26 => scale switch { 1519 <= 26 => new(new(prec, scale, false, -469762049, -590869294, 5421010, 0), new(prec, scale, true, -469762049, -590869294, 5421010, 0)), 1520 _ => throw new ArgumentOutOfRangeException(), 1521 }, 1522 27 => scale switch { 1523 <= 27 => new(new(prec, scale, false, -402653185, -1613725636, 54210108, 0), new(prec, scale, true, -402653185, -1613725636, 54210108, 0)), 1524 _ => throw new ArgumentOutOfRangeException(), 1525 }, 1526 28 => scale switch { 1527 <= 28 => new(new(prec, scale, false, 268435455, 1042612833, 542101086, 0), new(prec, scale, true, 268435455, 1042612833, 542101086, 0)), 1528 _ => throw new ArgumentOutOfRangeException(), 1529 }, 1530 29 => scale switch { 1531 <= 29 => new(new(prec, scale, false, -1610612737, 1836193738, 1126043566, 1), new(prec, scale, true, -1610612737, 1836193738, 1126043566, 1)), 1532 _ => throw new ArgumentOutOfRangeException(), 1533 }, 1534 30 => scale switch { 1535 <= 30 => new(new(prec, scale, false, 1073741823, 1182068202, -1624466224, 12), new(prec, scale, true, 1073741823, 1182068202, -1624466224, 12)), 1536 _ => throw new ArgumentOutOfRangeException(), 1537 }, 1538 31 => scale switch { 1539 <= 31 => new(new(prec, scale, false, 2147483647, -1064219866, 935206946, 126), new(prec, scale, true, 2147483647, -1064219866, 935206946, 126)), 1540 _ => throw new ArgumentOutOfRangeException(), 1541 }, 1542 32 => scale switch { 1543 <= 32 => new(new(prec, scale, false, -1, -2052264064, 762134875, 1262), new(prec, scale, true, -1, -2052264064, 762134875, 1262)), 1544 _ => throw new ArgumentOutOfRangeException(), 1545 }, 1546 33 => scale switch { 1547 <= 33 => new(new(prec, scale, false, -1, 952195849, -968585837, 12621), new(prec, scale, true, -1, 952195849, -968585837, 12621)), 1548 _ => throw new ArgumentOutOfRangeException(), 1549 }, 1550 34 => scale switch { 1551 <= 34 => new(new(prec, scale, false, -1, 932023907, -1095923776, 126217), new(prec, scale, true, -1, 932023907, -1095923776, 126217)), 1552 _ => throw new ArgumentOutOfRangeException(), 1553 }, 1554 35 => scale switch { 1555 <= 35 => new(new(prec, scale, false, -1, 730304487, 1925664130, 1262177), new(prec, scale, true, -1, 730304487, 1925664130, 1262177)), 1556 _ => throw new ArgumentOutOfRangeException(), 1557 }, 1558 36 => scale switch { 1559 <= 36 => new(new(prec, scale, false, -1, -1286889713, 2076772117, 12621774), new(prec, scale, true, -1, -1286889713, 2076772117, 12621774)), 1560 _ => throw new ArgumentOutOfRangeException(), 1561 }, 1562 37 => scale switch { 1563 <= 37 => new(new(prec, scale, false, -1, 16004767, -707115303, 126217744), new(prec, scale, true, -1, 16004767, -707115303, 126217744)), 1564 _ => throw new ArgumentOutOfRangeException(), 1565 }, 1566 38 => scale switch { 1567 <= 38 => new(new(prec, scale, false, -1, 160047679, 1518781562, 1262177448), new(prec, scale, true, -1, 160047679, 1518781562, 1262177448)), 1568 _ => throw new ArgumentOutOfRangeException(), 1569 }, 1570 _ => throw new ArgumentOutOfRangeException(), 1571 }; 1572 public static @decimal New(SqlDecimal val) => new(val); 1573 #endregion 1574 1575 #region Instance Functions 1576 public override readonly bool Equals(object? _) => false; 1577 public override readonly int GetHashCode() => 0; 1578 public readonly @decimal Into() => this; 1579 public readonly string IntoString() => ToString(); 1580 public readonly any IntoAny() => any.@decimal(this); 1581 readonly string IInto<string>.Into() => IntoString(); 1582 public readonly Unit Ser(ref Serializer ser) { 1583 1584 if (IsNULL) { 1585 return ser.SerByte((byte)any.Tag.NULL); 1586 } else { 1587 _ = ser.SerByte((byte)any.Tag.@decimal); 1588 _ = ser.SerByte(Value.Precision); 1589 _ = ser.SerByte(Value.Scale); 1590 _ = ser.SerBool(Value.IsPositive); 1591 return ser.SerSliceUnsafe<int>(Value.Data.AsSpan()); 1592 } 1593 } 1594 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1595 1596 if (IsNULL) { 1597 return ser.SerializeByte((byte)any.Tag.NULL); 1598 } else { 1599 var res = ser.SerializeByte((byte)any.Tag.@decimal); 1600 if (res.IsErr) { return res; } 1601 res = ser.SerializeByte(Value.Precision); 1602 if (res.IsErr) { return res; } 1603 res = ser.SerializeByte(Value.Scale); 1604 if (res.IsErr) { return res; } 1605 var res2 = ser.SerializeBool(Value.IsPositive); 1606 if (res2.IsErr) { return res2; } 1607 unsafe { fixed (int* ptr = Value.Data) { return ser.Serialize(new(ptr, 16)); } } 1608 } 1609 } 1610 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 1611 readonly bool IDataType.TruncationWillOccur(in Column col, bool numericRoundAbort) { 1612 1613 var scale = col.Scale.Unwrap(); 1614 1615 if (numericRoundAbort) { 1616 1617 if (Value.Scale > scale) { 1618 return true; 1619 } else if (Value.Precision - Value.Scale > col.Precision.Unwrap() - scale) { 1620 var bounds = GetBoundary(col.Precision.Unwrap(), scale); 1621 return (Value < bounds.Item0).IsTrue || (Value > bounds.Item1).IsTrue; 1622 } else { 1623 return false; 1624 } 1625 } else { 1626 var rounded = SqlDecimal.Round(Value, scale); 1627 var bounds = GetBoundary(col.Precision.Unwrap(), scale); 1628 return (rounded < bounds.Item0).IsTrue || (rounded > bounds.Item1).IsTrue; 1629 } 1630 } 1631 readonly Result<@decimal, Bottom> ITryInto<@decimal, Bottom>.TryInto() => new(this); 1632 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1633 #endregion 1634 1635 #region Operators 1636 public static implicit operator SqlDecimal(@decimal val) => val.Value; 1637 public static implicit operator @decimal(SqlDecimal val) => new(val); 1638 #endregion 1639 1640 #region Types 1641 #endregion 1642 } 1643 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 1644 public readonly struct @float: IDataType, IInto<@float>, IBinDeserializable<@float> { 1645 1646 #region Type-level Constructors 1647 #endregion 1648 1649 #region Instance Constructors 1650 public @float() => Value = double.NaN; 1651 internal @float(double val) => Value = val; 1652 #endregion 1653 1654 #region Type-level Fields 1655 static readonly @string finiteDouble = "A finite double"; 1656 public static readonly @float NULL = new(); 1657 #endregion 1658 1659 #region Instance Fields 1660 [FieldOffset(0)] internal readonly double Value; 1661 #endregion 1662 1663 #region Type-level Properties 1664 #endregion 1665 1666 #region Instance Properties 1667 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 1668 public readonly bool IsNULL => double.IsNaN(Value); 1669 #endregion 1670 1671 #region Type-level Functions 1672 public static Result<@float, Error> De(ref Deserializer des) { 1673 1674 var des2 = des; 1675 var resDouble = des2.DeserializeDouble(); 1676 1677 if (resDouble.IsOK) { 1678 var val = resDouble.Unwrap(); 1679 1680 if (double.IsFinite(val)) { 1681 des = des2; 1682 return new(new @float(val)); 1683 } else { 1684 return new(Error.InvalidValue(Unexpected.Double(val), finiteDouble)); 1685 } 1686 } else { 1687 return new(resDouble.UnwrapErr()); 1688 } 1689 } 1690 public static Result<@float, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1691 1692 var des2 = des; 1693 var resDouble = des2.DeserializeDouble(); 1694 1695 if (resDouble.IsOK) { 1696 var val = resDouble.Unwrap(); 1697 1698 if (double.IsFinite(val)) { 1699 des = des2; 1700 return new(new @float(val)); 1701 } else { 1702 return new(Error.InvalidValue(Unexpected.Double(val), finiteDouble)); 1703 } 1704 } else { 1705 return new(resDouble.UnwrapErr()); 1706 } 1707 } 1708 public static Result<@float, OutOfRangeErr> New(double val) => double.IsFinite(val) ? new(new @float(val)) : new(OutOfRangeErr.OutOfRange); 1709 #endregion 1710 1711 #region Instance Functions 1712 public override readonly bool Equals(object? _) => false; 1713 public override readonly int GetHashCode() => 0; 1714 public readonly @float Into() => this; 1715 public readonly string IntoString() => ToString(); 1716 readonly string IInto<string>.Into() => IntoString(); 1717 public readonly any IntoAny() => any.@float(this); 1718 public readonly Unit Ser(ref Serializer ser) { 1719 1720 if (IsNULL) { 1721 return ser.SerByte((byte)any.Tag.NULL); 1722 } else { 1723 _ = ser.SerByte((byte)any.Tag.@float); 1724 return ser.SerDouble(Value); 1725 } 1726 } 1727 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1728 1729 if (IsNULL) { 1730 return ser.SerializeByte((byte)any.Tag.NULL); 1731 } else { 1732 var res = ser.SerializeByte((byte)any.Tag.@float); 1733 return res.IsErr ? res : ser.SerializeDouble(Value); 1734 } 1735 } 1736 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("G17"); 1737 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1738 readonly Result<@float, Bottom> ITryInto<@float, Bottom>.TryInto() => new(this); 1739 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1740 #endregion 1741 1742 #region Operators 1743 #endregion 1744 1745 #region Types 1746 #endregion 1747 } 1748 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 1749 public readonly struct image: IDataType, IInto<image>, IBinDeserializable<image> { 1750 1751 #region Type-level Constructors 1752 #endregion 1753 1754 #region Instance Constructors 1755 public image() => Value = null; 1756 internal image(byte[] val) => Value = val; 1757 #endregion 1758 1759 #region Type-level Fields 1760 public static readonly image NULL = new(); 1761 #endregion 1762 1763 #region Instance Fields 1764 [FieldOffset(0)] internal readonly byte[]? Value; 1765 #endregion 1766 1767 #region Type-level Properties 1768 #endregion 1769 1770 #region Instance Properties 1771 public readonly object Val => Value; 1772 public readonly bool IsNULL => Value is null; 1773 #endregion 1774 1775 #region Type-level Functions 1776 public static Result<image, Error> De(ref Deserializer des) { 1777 1778 var des2 = des; 1779 var resUint = des2.DeserializeUint(); 1780 1781 if (resUint.IsOK) { 1782 var len = resUint.Unwrap(); 1783 var bytes = des2.Read(len); 1784 1785 if ((uint)bytes.Length == len) { 1786 des = des2; 1787 return new(new image(bytes.ToArray())); 1788 } else { 1789 return new(Error.InvalidLength((ulong)bytes.Length, (@uint)len)); 1790 } 1791 } else { 1792 return new(resUint.UnwrapErr()); 1793 } 1794 } 1795 public static Result<image, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1796 1797 var des2 = des; 1798 var resUint = des2.DeserializeUint(); 1799 1800 if (resUint.IsOK) { 1801 var len = resUint.Unwrap(); 1802 uint l; 1803 var bytes = new byte[len]; 1804 1805 if ((l = des2.Deserialize(bytes.AsSpan())) == len) { 1806 des = des2; 1807 return new(new image(bytes)); 1808 } else { 1809 return new(Error.InvalidLength((ulong)l, (@uint)len)); 1810 } 1811 } else { 1812 return new(resUint.UnwrapErr()); 1813 } 1814 } 1815 public static image New(byte[] val) => new(val); 1816 public static image New(ReadOnlySpan<byte> val) => new(val.ToArray()); 1817 #endregion 1818 1819 #region Instance Functions 1820 public override readonly bool Equals(object? _) => false; 1821 public override readonly int GetHashCode() => 0; 1822 public readonly image Into() => this; 1823 public readonly string IntoString() => ToString(); 1824 public readonly any IntoAny() => any.image(this); 1825 readonly string IInto<string>.Into() => IntoString(); 1826 public readonly Unit Ser(ref Serializer ser) { 1827 1828 if (IsNULL) { 1829 return ser.SerByte((byte)any.Tag.NULL); 1830 } else { 1831 _ = ser.SerByte((byte)any.Tag.image); 1832 _ = ser.SerUint((uint)Value!.Length); 1833 return ser.Ser(Value.AsSpan()); 1834 } 1835 } 1836 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1837 1838 if (IsNULL) { 1839 return ser.SerializeByte((byte)any.Tag.NULL); 1840 } else { 1841 var res = ser.SerializeByte((byte)any.Tag.image); 1842 if (res.IsErr) { return res; } 1843 var res2 = ser.SerializeUint((uint)Value!.Length); 1844 return res2.IsErr ? res2 : ser.Serialize(Value.AsSpan()); 1845 } 1846 } 1847 public override readonly string ToString() => IsNULL ? "NULL" : Value.AsSpan().UpperHex(); 1848 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1849 readonly Result<image, Bottom> ITryInto<image, Bottom>.TryInto() => new(this); 1850 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1851 #endregion 1852 1853 #region Operators 1854 #endregion 1855 1856 #region Types 1857 #endregion 1858 } 1859 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 8)] 1860 public readonly struct @int: IDataType, IInto<@int>, IBinDeserializable<@int> { 1861 1862 #region Type-level Constructors 1863 #endregion 1864 1865 #region Instance Constructors 1866 public @int() => (Value, _isNULL) = (0, uint.MinValue); 1867 public @int(int val) => (Value, _isNULL) = (val, uint.MaxValue); 1868 #endregion 1869 1870 #region Type-level Fields 1871 public static readonly @int NULL = new(); 1872 #endregion 1873 1874 #region Instance Fields 1875 [FieldOffset(0)] internal readonly int Value; 1876 [FieldOffset(4)] readonly uint _isNULL; 1877 #endregion 1878 1879 #region Type-level Properties 1880 #endregion 1881 1882 #region Instance Properties 1883 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 1884 public readonly bool IsNULL => _isNULL == uint.MinValue; 1885 #endregion 1886 1887 #region Type-level Functions 1888 public static Result<@int, Error> De(ref Deserializer des) { 1889 1890 var des2 = des; 1891 var resInt = des2.DeserializeInt(); 1892 1893 if (resInt.IsOK) { 1894 des = des2; 1895 return new(new @int(resInt.Unwrap())); 1896 } else { 1897 return new(resInt.UnwrapErr()); 1898 } 1899 } 1900 public static Result<@int, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 1901 1902 var des2 = des; 1903 var resInt = des2.DeserializeInt(); 1904 1905 if (resInt.IsOK) { 1906 des = des2; 1907 return new(new @int(resInt.Unwrap())); 1908 } else { 1909 return new(resInt.UnwrapErr()); 1910 } 1911 } 1912 public static @int New(int val) => new(val); 1913 #endregion 1914 1915 #region Instance Functions 1916 public override readonly bool Equals(object? _) => false; 1917 public override readonly int GetHashCode() => 0; 1918 public readonly @int Into() => this; 1919 public readonly string IntoString() => ToString(); 1920 readonly string IInto<string>.Into() => IntoString(); 1921 public readonly any IntoAny() => any.@int(this); 1922 public readonly Unit Ser(ref Serializer ser) { 1923 1924 if (IsNULL) { 1925 return ser.SerByte((byte)any.Tag.NULL); 1926 } else { 1927 _ = ser.SerByte((byte)any.Tag.@int); 1928 return ser.SerInt(Value); 1929 } 1930 } 1931 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 1932 1933 if (IsNULL) { 1934 return ser.SerializeByte((byte)any.Tag.NULL); 1935 } else { 1936 var res = ser.SerializeByte((byte)any.Tag.@int); 1937 return res.IsErr ? res : ser.SerializeInt(Value); 1938 } 1939 } 1940 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 1941 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 1942 readonly Result<@int, Bottom> ITryInto<@int, Bottom>.TryInto() => new(this); 1943 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 1944 #endregion 1945 1946 #region Operators 1947 public static implicit operator @int(int val) => new(val); 1948 #endregion 1949 1950 #region Types 1951 #endregion 1952 } 1953 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 16)] 1954 public readonly struct money: IDataType, IInto<money>, IBinDeserializable<money> { 1955 1956 #region Type-level Constructors 1957 #endregion 1958 1959 #region Instance Constructors 1960 public money() => Value = SqlMoney.Null; 1961 public money(SqlMoney val) => Value = val; 1962 #endregion 1963 1964 #region Type-level Fields 1965 static readonly @string validMoney = "The decimal value from an SqlMoney"; 1966 public static readonly money NULL = new(); 1967 #endregion 1968 1969 #region Instance Fields 1970 [FieldOffset(0)] internal readonly SqlMoney Value; 1971 #endregion 1972 1973 #region Type-level Properties 1974 #endregion 1975 1976 #region Instance Properties 1977 public readonly object Val => Value; 1978 public readonly bool IsNULL => Value.IsNull; 1979 #endregion 1980 1981 #region Type-level Functions 1982 public static Result<money, Error> De(ref Deserializer des) { 1983 1984 var des2 = des; 1985 var resDecimal = des2.DeserializeDecimal(); 1986 1987 if (resDecimal.IsOK) { 1988 var dec = resDecimal.Unwrap(); 1989 1990 if (dec >= SqlMoney.MinValue.Value && dec <= SqlMoney.MaxValue.Value && dec == new SqlMoney(dec).Value) { 1991 des = des2; 1992 return new(new money(new SqlMoney(dec))); 1993 } else { 1994 return new(Error.InvalidValue(Unexpected.Decimal(dec), validMoney)); 1995 } 1996 } else { 1997 return new(resDecimal.UnwrapErr()); 1998 } 1999 } 2000 public static Result<money, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2001 2002 var des2 = des; 2003 var resDecimal = des2.DeserializeDecimal(); 2004 2005 if (resDecimal.IsOK) { 2006 var dec = resDecimal.Unwrap(); 2007 2008 if (dec >= SqlMoney.MinValue.Value && dec <= SqlMoney.MaxValue.Value && dec == new SqlMoney(dec).Value) { 2009 des = des2; 2010 return new(new money(new SqlMoney(dec))); 2011 } else { 2012 return new(Error.InvalidValue(Unexpected.Decimal(dec), validMoney)); 2013 } 2014 } else { 2015 return new(resDecimal.UnwrapErr()); 2016 } 2017 } 2018 public static money New(SqlMoney val) => new(val); 2019 #endregion 2020 2021 #region Instance Functions 2022 public override readonly bool Equals(object? _) => false; 2023 public override readonly int GetHashCode() => 0; 2024 public readonly money Into() => this; 2025 public readonly string IntoString() => ToString(); 2026 readonly string IInto<string>.Into() => IntoString(); 2027 public readonly any IntoAny() => any.money(this); 2028 public readonly Unit Ser(ref Serializer ser) { 2029 2030 if (IsNULL) { 2031 return ser.SerByte((byte)any.Tag.NULL); 2032 } else { 2033 _ = ser.SerByte((byte)any.Tag.money); 2034 return ser.SerDecimal(Value.Value); 2035 } 2036 } 2037 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2038 2039 if (IsNULL) { 2040 return ser.SerializeByte((byte)any.Tag.NULL); 2041 } else { 2042 var res = ser.SerializeByte((byte)any.Tag.money); 2043 return res.IsErr ? res : ser.SerializeDecimal(Value.Value); 2044 } 2045 } 2046 public override readonly string ToString() => IsNULL ? "NULL" : Value.Value.ToString("N4"); 2047 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2048 readonly Result<money, Bottom> ITryInto<money, Bottom>.TryInto() => new(this); 2049 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2050 #endregion 2051 2052 #region Operators 2053 public static implicit operator SqlMoney(money val) => val.Value; 2054 public static implicit operator money(SqlMoney val) => new(val); 2055 #endregion 2056 2057 #region Types 2058 #endregion 2059 } 2060 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 2061 public readonly struct nchar: IDataType, IInto<nchar>, IBinDeserializable<nchar> { 2062 2063 #region Type-level Constructors 2064 #endregion 2065 2066 #region Instance Constructors 2067 public nchar() => Value = null; 2068 internal nchar(string val) => Value = val; 2069 #endregion 2070 2071 #region Type-level Fields 2072 static readonly @string ushort1To4000 = "1–4000"; 2073 public static readonly nchar NULL = new(); 2074 #endregion 2075 2076 #region Instance Fields 2077 [FieldOffset(0)] internal readonly string? Value; 2078 #endregion 2079 2080 #region Type-level Properties 2081 #endregion 2082 2083 #region Instance Properties 2084 public readonly object Val => Value; 2085 public readonly bool IsNULL => Value is null; 2086 #endregion 2087 2088 #region Type-level Functions 2089 public static Result<nchar, Error> De(ref Deserializer des) { 2090 2091 var des2 = des; 2092 var resUshort = des2.DeserializeUshort(); 2093 2094 if (resUshort.IsOK) { 2095 var len = resUshort.Unwrap(); 2096 2097 if (len is > ushort.MinValue and <= 4000) { 2098 var chars = des2.DeserializeChars(len); 2099 2100 if ((uint)chars.Length == len) { 2101 des = des2; 2102 return new(new nchar(new string(chars))); 2103 } else { 2104 return new(Error.InvalidLength((ulong)chars.Length, (@ushort)len)); 2105 } 2106 } else { 2107 return new(Error.InvalidLength(len, ushort1To4000)); 2108 } 2109 } else { 2110 return new(resUshort.UnwrapErr()); 2111 } 2112 } 2113 public static Result<nchar, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2114 2115 var des2 = des; 2116 var resUshort = des2.DeserializeUshort(); 2117 2118 if (resUshort.IsOK) { 2119 var len = resUshort.Unwrap(); 2120 2121 if (len is > ushort.MinValue and <= 4000) { 2122 uint l; 2123 Span<char> chars = stackalloc char[len]; 2124 2125 if ((l = des2.DeserializeChars(chars)) == len) { 2126 des = des2; 2127 return new(new nchar(new string(chars))); 2128 } else { 2129 return new(Error.InvalidLength((ulong)l, (@ushort)len)); 2130 } 2131 } else { 2132 return new(Error.InvalidLength(len, ushort1To4000)); 2133 } 2134 } else { 2135 return new(resUshort.UnwrapErr()); 2136 } 2137 } 2138 public static Result<nchar, OutOfRangeErr> New(string val) => val.Length is 0 or > 4000 ? new(OutOfRangeErr.OutOfRange) : new(new nchar(val)); 2139 #endregion 2140 2141 #region Instance Functions 2142 public override readonly bool Equals(object? _) => false; 2143 public override readonly int GetHashCode() => 0; 2144 public readonly nchar Into() => this; 2145 public readonly string IntoString() => ToString(); 2146 public readonly any IntoAny() => any.nchar(this); 2147 readonly string IInto<string>.Into() => IntoString(); 2148 public readonly Unit Ser(ref Serializer ser) { 2149 2150 if (IsNULL) { 2151 return ser.SerByte((byte)any.Tag.NULL); 2152 } else { 2153 _ = ser.SerByte((byte)any.Tag.nchar); 2154 _ = ser.SerUshort((ushort)Value!.Length); 2155 return ser.SerChars(Value.AsSpan()); 2156 } 2157 } 2158 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2159 2160 if (IsNULL) { 2161 return ser.SerializeByte((byte)any.Tag.NULL); 2162 } else { 2163 var res = ser.SerializeByte((byte)any.Tag.nchar); 2164 if (res.IsErr) { return res; } 2165 var res2 = ser.SerializeUshort((ushort)Value!.Length); 2166 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 2167 } 2168 } 2169 public override readonly string ToString() => IsNULL ? "NULL" : Value; 2170 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => Value!.Length > col.MaxLength.Unwrap(); 2171 readonly Result<nchar, Bottom> ITryInto<nchar, Bottom>.TryInto() => new(this); 2172 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2173 #endregion 2174 2175 #region Operators 2176 #endregion 2177 2178 #region Types 2179 #endregion 2180 } 2181 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 2182 public readonly struct ntext: IDataType, IInto<ntext>, IBinDeserializable<ntext> { 2183 2184 #region Type-level Constructors 2185 #endregion 2186 2187 #region Instance Constructors 2188 public ntext() => Value = null; 2189 public ntext(string val) => Value = val; 2190 #endregion 2191 2192 #region Type-level Fields 2193 public static readonly ntext NULL = new(); 2194 #endregion 2195 2196 #region Instance Fields 2197 [FieldOffset(0)] internal readonly string? Value; 2198 #endregion 2199 2200 #region Type-level Properties 2201 #endregion 2202 2203 #region Instance Properties 2204 public readonly object Val => Value; 2205 public readonly bool IsNULL => Value is null; 2206 #endregion 2207 2208 #region Type-level Functions 2209 public static Result<ntext, Error> De(ref Deserializer des) { 2210 2211 var des2 = des; 2212 var resUint = des2.DeserializeUint(); 2213 2214 if (resUint.IsOK) { 2215 var len = resUint.Unwrap(); 2216 var chars = des2.DeserializeChars(len); 2217 2218 if ((uint)chars.Length == len) { 2219 des = des2; 2220 return new(new ntext(new string(chars))); 2221 } else { 2222 return new(Error.InvalidLength((ulong)chars.Length, (@uint)len)); 2223 } 2224 } else { 2225 return new(resUint.UnwrapErr()); 2226 } 2227 } 2228 public static Result<ntext, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2229 2230 var des2 = des; 2231 var resUint = des2.DeserializeUint(); 2232 2233 if (resUint.IsOK) { 2234 var len = resUint.Unwrap(); 2235 uint l; 2236 var chars = new char[len]; 2237 2238 if ((l = des2.DeserializeChars(chars)) == len) { 2239 des = des2; 2240 return new(new ntext(new string(chars))); 2241 } else { 2242 return new(Error.InvalidLength((ulong)l, (@uint)len)); 2243 } 2244 } else { 2245 return new(resUint.UnwrapErr()); 2246 } 2247 } 2248 public static ntext New(string val) => new(val); 2249 #endregion 2250 2251 #region Instance Functions 2252 public override readonly bool Equals(object? _) => false; 2253 public override readonly int GetHashCode() => 0; 2254 public readonly ntext Into() => this; 2255 public readonly string IntoString() => ToString(); 2256 public readonly any IntoAny() => any.ntext(this); 2257 readonly string IInto<string>.Into() => IntoString(); 2258 public readonly Unit Ser(ref Serializer ser) { 2259 2260 if (IsNULL) { 2261 return ser.SerByte((byte)any.Tag.NULL); 2262 } else { 2263 _ = ser.SerByte((byte)any.Tag.ntext); 2264 _ = ser.SerUint((uint)Value!.Length); 2265 return ser.SerChars(Value.AsSpan()); 2266 } 2267 } 2268 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2269 2270 if (IsNULL) { 2271 return ser.SerializeByte((byte)any.Tag.NULL); 2272 } else { 2273 var res = ser.SerializeByte((byte)any.Tag.ntext); 2274 if (res.IsErr) { return res; } 2275 var res2 = ser.SerializeUint((uint)Value!.Length); 2276 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 2277 } 2278 } 2279 public override readonly string ToString() => IsNULL ? "NULL" : Value; 2280 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2281 readonly Result<ntext, Bottom> ITryInto<ntext, Bottom>.TryInto() => new(this); 2282 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2283 #endregion 2284 2285 #region Operators 2286 public static implicit operator ntext(string val) => new(val); 2287 #endregion 2288 2289 #region Types 2290 #endregion 2291 } 2292 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 2293 public readonly struct nvarchar: IDataType, IInto<nvarchar>, IBinDeserializable<nvarchar> { 2294 2295 #region Type-level Constructors 2296 #endregion 2297 2298 #region Instance Constructors 2299 public nvarchar() => Value = null; 2300 public nvarchar(string val) => Value = val; 2301 #endregion 2302 2303 #region Type-level Fields 2304 public static readonly nvarchar NULL = new(); 2305 #endregion 2306 2307 #region Instance Fields 2308 [FieldOffset(0)] internal readonly string? Value; 2309 #endregion 2310 2311 #region Type-level Properties 2312 #endregion 2313 2314 #region Instance Properties 2315 public readonly object Val => Value; 2316 public readonly bool IsNULL => Value is null; 2317 #endregion 2318 2319 #region Type-level Functions 2320 public static Result<nvarchar, Error> De(ref Deserializer des) { 2321 2322 var des2 = des; 2323 var resUint = des2.DeserializeUint(); 2324 2325 if (resUint.IsOK) { 2326 var len = resUint.Unwrap(); 2327 var chars = des2.DeserializeChars(len); 2328 2329 if ((uint)chars.Length == len) { 2330 des = des2; 2331 return new(new nvarchar(new string(chars))); 2332 } else { 2333 return new(Error.InvalidLength((ulong)chars.Length, (@uint)len)); 2334 } 2335 } else { 2336 return new(resUint.UnwrapErr()); 2337 } 2338 } 2339 public static Result<nvarchar, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2340 2341 var des2 = des; 2342 var resUint = des2.DeserializeUint(); 2343 2344 if (resUint.IsOK) { 2345 var len = resUint.Unwrap(); 2346 uint l; 2347 var chars = new char[len]; 2348 2349 if ((l = des2.DeserializeChars(chars)) == len) { 2350 des = des2; 2351 return new(new nvarchar(new string(chars))); 2352 } else { 2353 return new(Error.InvalidLength((ulong)l, (@uint)len)); 2354 } 2355 } else { 2356 return new(resUint.UnwrapErr()); 2357 } 2358 } 2359 public static nvarchar New(string val) => new(val); 2360 #endregion 2361 2362 #region Instance Functions 2363 public override readonly bool Equals(object? _) => false; 2364 public override readonly int GetHashCode() => 0; 2365 public readonly nvarchar Into() => this; 2366 public readonly string IntoString() => ToString(); 2367 public readonly any IntoAny() => any.nvarchar(this); 2368 readonly string IInto<string>.Into() => IntoString(); 2369 public readonly Unit Ser(ref Serializer ser) { 2370 2371 if (IsNULL) { 2372 return ser.SerByte((byte)any.Tag.NULL); 2373 } else { 2374 _ = ser.SerByte((byte)any.Tag.nvarchar); 2375 _ = ser.SerUint((uint)Value!.Length); 2376 return ser.SerChars(Value.AsSpan()); 2377 } 2378 } 2379 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2380 2381 if (IsNULL) { 2382 return ser.SerializeByte((byte)any.Tag.NULL); 2383 } else { 2384 var res = ser.SerializeByte((byte)any.Tag.nvarchar); 2385 if (res.IsErr) { return res; } 2386 var res2 = ser.SerializeUint((uint)Value!.Length); 2387 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 2388 } 2389 } 2390 public override readonly string ToString() => IsNULL ? "NULL" : Value; 2391 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => col.MaxLength.IsSome && Value!.Length > col.MaxLength.Unwrap(); 2392 readonly Result<nvarchar, Bottom> ITryInto<nvarchar, Bottom>.TryInto() => new(this); 2393 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2394 #endregion 2395 2396 #region Operators 2397 public static implicit operator nvarchar(string val) => new(val); 2398 #endregion 2399 2400 #region Types 2401 #endregion 2402 } 2403 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 4)] 2404 public readonly struct real: IDataType, IInto<real>, IBinDeserializable<real> { 2405 2406 #region Type-level Constructors 2407 #endregion 2408 2409 #region Instance Constructors 2410 public real() => Value = float.NaN; 2411 internal real(float val) => Value = val; 2412 #endregion 2413 2414 #region Type-level Fields 2415 static readonly @string finiteFloat = "A finite float"; 2416 public static readonly real NULL = new(); 2417 #endregion 2418 2419 #region Instance Fields 2420 [FieldOffset(0)] internal readonly float Value; 2421 #endregion 2422 2423 #region Type-level Properties 2424 #endregion 2425 2426 #region Instance Properties 2427 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 2428 public readonly bool IsNULL => float.IsNaN(Value); 2429 #endregion 2430 2431 #region Type-level Functions 2432 public static Result<real, Error> De(ref Deserializer des) { 2433 2434 var des2 = des; 2435 var resFloat = des2.DeserializeFloat(); 2436 2437 if (resFloat.IsOK) { 2438 var val = resFloat.Unwrap(); 2439 2440 if (float.IsFinite(val)) { 2441 des = des2; 2442 return new(new real(val)); 2443 } else { 2444 return new(Error.InvalidValue(Unexpected.Float(val), finiteFloat)); 2445 } 2446 } else { 2447 return new(resFloat.UnwrapErr()); 2448 } 2449 } 2450 public static Result<real, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2451 2452 var des2 = des; 2453 var resFloat = des2.DeserializeFloat(); 2454 2455 if (resFloat.IsOK) { 2456 var val = resFloat.Unwrap(); 2457 2458 if (float.IsFinite(val)) { 2459 des = des2; 2460 return new(new real(val)); 2461 } else { 2462 return new(Error.InvalidValue(Unexpected.Float(val), finiteFloat)); 2463 } 2464 } else { 2465 return new(resFloat.UnwrapErr()); 2466 } 2467 } 2468 public static Result<real, OutOfRangeErr> New(float val) => float.IsFinite(val) ? new(new real(val)) : new(OutOfRangeErr.OutOfRange); 2469 #endregion 2470 2471 #region Instance Functions 2472 public override readonly bool Equals(object? _) => false; 2473 public override readonly int GetHashCode() => 0; 2474 public readonly real Into() => this; 2475 public readonly string IntoString() => ToString(); 2476 readonly string IInto<string>.Into() => IntoString(); 2477 public readonly any IntoAny() => any.real(this); 2478 public readonly Unit Ser(ref Serializer ser) { 2479 2480 if (IsNULL) { 2481 return ser.SerByte((byte)any.Tag.NULL); 2482 } else { 2483 _ = ser.SerByte((byte)any.Tag.real); 2484 return ser.SerFloat(Value); 2485 } 2486 } 2487 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2488 2489 if (IsNULL) { 2490 return ser.SerializeByte((byte)any.Tag.NULL); 2491 } else { 2492 var res = ser.SerializeByte((byte)any.Tag.real); 2493 return res.IsErr ? res : ser.SerializeFloat(Value); 2494 } 2495 } 2496 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("G9"); 2497 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2498 readonly Result<real, Bottom> ITryInto<real, Bottom>.TryInto() => new(this); 2499 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2500 #endregion 2501 2502 #region Operators 2503 #endregion 2504 2505 #region Types 2506 #endregion 2507 } 2508 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 12)] 2509 public readonly struct smalldatetime: IDataType, IInto<smalldatetime>, IBinDeserializable<smalldatetime> { 2510 2511 #region Type-level Constructors 2512 #endregion 2513 2514 #region Instance Constructors 2515 public smalldatetime() => Value = SqlDateTime.Null; 2516 internal smalldatetime(SqlDateTime val) => Value = val; 2517 #endregion 2518 2519 #region Type-level Fields 2520 internal static readonly SqlDateTime Max = new(new System.DateTime(2079, 6, 6, 23, 59, 0, 0)); 2521 internal static readonly SqlDateTime Min = new(new System.DateTime(1900, 1, 1, 0, 0, 0, 0)); 2522 // This represents the seconds and fractional seconds component as the number of ticks. 2523 // If the seconds and fractional seconds component of the SqlDateTime is greater than this value (i.e., 29.997), 2524 // then the SqlDateTime will be rounded up to the next minute; otherwise it will round down to the minute. 2525 const long BoundaryTicks = (29L * System.TimeSpan.TicksPerSecond) + (997L * System.TimeSpan.TicksPerMillisecond); 2526 static readonly @string validSmalldatetime = $"The DateTime value from an SqlDateTime bounded inclusively between {Min.Value.ToString("yyyy-MM-ddTHH:mm")} and {Max.Value.ToString("yyyy-MM-ddTHH:mm")} with seconds and fractional seconds set to 0."; 2527 public static readonly smalldatetime NULL = new(); 2528 #endregion 2529 2530 #region Instance Fields 2531 [FieldOffset(0)] internal readonly SqlDateTime Value; 2532 #endregion 2533 2534 #region Type-level Properties 2535 #endregion 2536 2537 #region Instance Properties 2538 public readonly object Val => Value; 2539 public readonly bool IsNULL => Value.IsNull; 2540 #endregion 2541 2542 #region Type-level Functions 2543 public static Result<smalldatetime, Error> De(ref Deserializer des) { 2544 2545 var des2 = des; 2546 var resDateTime = des2.DeserializeDateTime(); 2547 2548 if (resDateTime.IsOK) { 2549 var date = resDateTime.Unwrap(); 2550 2551 if (date >= Min.Value && date <= Max.Value && date.Ticks == (date.Date.Ticks + (date.Hour * System.TimeSpan.TicksPerHour) + (date.Minute * System.TimeSpan.TicksPerMinute)) && date == new SqlDateTime(date).Value) { 2552 des = des2; 2553 return new(new smalldatetime(new SqlDateTime(date))); 2554 } else { 2555 return new(Error.InvalidValue(Unexpected.DateTime(date), validSmalldatetime)); 2556 } 2557 } else { 2558 return new(resDateTime.UnwrapErr()); 2559 } 2560 } 2561 public static Result<smalldatetime, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2562 2563 var des2 = des; 2564 var resDateTime = des2.DeserializeDateTime(); 2565 2566 if (resDateTime.IsOK) { 2567 var date = resDateTime.Unwrap(); 2568 2569 if (date >= Min.Value && date <= Max.Value && date.Ticks == (date.Date.Ticks + (date.Hour * System.TimeSpan.TicksPerHour) + (date.Minute * System.TimeSpan.TicksPerMinute)) && date == new SqlDateTime(date).Value) { 2570 des = des2; 2571 return new(new smalldatetime(new SqlDateTime(date))); 2572 } else { 2573 return new(Error.InvalidValue(Unexpected.DateTime(date), validSmalldatetime)); 2574 } 2575 } else { 2576 return new(resDateTime.UnwrapErr()); 2577 } 2578 } 2579 public static Result<smalldatetime, OutOfRangeErr> New(SqlDateTime val) { 2580 // Get the tick count of the value minus the second and fractional seconds component. 2581 var ticks = val.Value.Date.Ticks + (val.Value.Hour * System.TimeSpan.TicksPerHour) + (val.Value.Minute * System.TimeSpan.TicksPerMinute); 2582 // Round to the nearest minute. 2583 val = new(val.Value.Ticks - ticks > BoundaryTicks ? new System.DateTime(ticks + System.TimeSpan.TicksPerMinute) : new System.DateTime(ticks)); 2584 return val < Min || val > Max ? new(OutOfRangeErr.OutOfRange) : new(new smalldatetime(val)); 2585 } 2586 #endregion 2587 2588 #region Instance Functions 2589 public override readonly bool Equals(object? _) => false; 2590 public override readonly int GetHashCode() => 0; 2591 public readonly smalldatetime Into() => this; 2592 public readonly string IntoString() => ToString(); 2593 readonly string IInto<string>.Into() => IntoString(); 2594 public readonly any IntoAny() => any.smalldatetime(this); 2595 public readonly Unit Ser(ref Serializer ser) { 2596 2597 if (IsNULL) { 2598 return ser.SerByte((byte)any.Tag.NULL); 2599 } else { 2600 _ = ser.SerByte((byte)any.Tag.smalldatetime); 2601 return ser.SerDateTime(Value.Value); 2602 } 2603 } 2604 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2605 2606 if (IsNULL) { 2607 return ser.SerializeByte((byte)any.Tag.NULL); 2608 } else { 2609 var res = ser.SerializeByte((byte)any.Tag.smalldatetime); 2610 return res.IsErr ? res : ser.SerializeDateTime(Value.Value); 2611 } 2612 } 2613 public override readonly string ToString() => IsNULL ? "NULL" : Value.Value.ToString("yyyy-MM-ddTHH:mm"); 2614 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2615 readonly Result<smalldatetime, Bottom> ITryInto<smalldatetime, Bottom>.TryInto() => new(this); 2616 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2617 #endregion 2618 2619 #region Operators 2620 public static implicit operator SqlDateTime(smalldatetime val) => val.Value; 2621 #endregion 2622 2623 #region Types 2624 #endregion 2625 } 2626 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2, Size = 4)] 2627 public readonly struct smallint: IDataType, IInto<smallint>, IBinDeserializable<smallint> { 2628 2629 #region Type-level Constructors 2630 #endregion 2631 2632 #region Instance Constructors 2633 public smallint() => (Value, _isNULL) = (0, ushort.MinValue); 2634 public smallint(short val) => (Value, _isNULL) = (val, ushort.MaxValue); 2635 #endregion 2636 2637 #region Type-level Fields 2638 public static readonly smallint NULL = new(); 2639 #endregion 2640 2641 #region Instance Fields 2642 [FieldOffset(0)] internal readonly short Value; 2643 [FieldOffset(2)] readonly ushort _isNULL; 2644 #endregion 2645 2646 #region Type-level Properties 2647 #endregion 2648 2649 #region Instance Properties 2650 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 2651 public readonly bool IsNULL => _isNULL == ushort.MinValue; 2652 #endregion 2653 2654 #region Type-level Functions 2655 public static Result<smallint, Error> De(ref Deserializer des) { 2656 2657 var des2 = des; 2658 var resShort = des2.DeserializeShort(); 2659 2660 if (resShort.IsOK) { 2661 des = des2; 2662 return new(new smallint(resShort.Unwrap())); 2663 } else { 2664 return new(resShort.UnwrapErr()); 2665 } 2666 } 2667 public static Result<smallint, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2668 2669 var des2 = des; 2670 var resShort = des2.DeserializeShort(); 2671 2672 if (resShort.IsOK) { 2673 des = des2; 2674 return new(new smallint(resShort.Unwrap())); 2675 } else { 2676 return new(resShort.UnwrapErr()); 2677 } 2678 } 2679 public static smallint New(short val) => new(val); 2680 #endregion 2681 2682 #region Instance Functions 2683 public override readonly bool Equals(object? _) => false; 2684 public override readonly int GetHashCode() => 0; 2685 public readonly smallint Into() => this; 2686 public readonly string IntoString() => ToString(); 2687 readonly string IInto<string>.Into() => IntoString(); 2688 public readonly any IntoAny() => any.smallint(this); 2689 public readonly Unit Ser(ref Serializer ser) { 2690 2691 if (IsNULL) { 2692 return ser.SerByte((byte)any.Tag.NULL); 2693 } else { 2694 _ = ser.SerByte((byte)any.Tag.smallint); 2695 return ser.SerShort(Value); 2696 } 2697 } 2698 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2699 2700 if (IsNULL) { 2701 return ser.SerializeByte((byte)any.Tag.NULL); 2702 } else { 2703 var res = ser.SerializeByte((byte)any.Tag.smallint); 2704 return res.IsErr ? res : ser.SerializeShort(Value); 2705 } 2706 } 2707 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 2708 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2709 readonly Result<smallint, Bottom> ITryInto<smallint, Bottom>.TryInto() => new(this); 2710 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2711 #endregion 2712 2713 #region Operators 2714 public static implicit operator smallint(short val) => new(val); 2715 #endregion 2716 2717 #region Types 2718 #endregion 2719 } 2720 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 16)] 2721 public readonly struct smallmoney: IDataType, IInto<smallmoney>, IBinDeserializable<smallmoney> { 2722 2723 #region Type-level Constructors 2724 #endregion 2725 2726 #region Instance Constructors 2727 public smallmoney() => Value = SqlMoney.Null; 2728 internal smallmoney(SqlMoney val) => Value = val; 2729 #endregion 2730 2731 #region Type-level Fields 2732 internal static readonly SqlMoney Max = new(214748.3647m); 2733 internal static readonly SqlMoney Min = new(-214748.3648m); 2734 static readonly @string validSmallmoney = $"The decimal value from an SqlMoney bounded inclusively between {Min.Value.ToString("N4")} and {Max.Value.ToString("N4")}."; 2735 public static readonly smallmoney NULL = new(); 2736 #endregion 2737 2738 #region Instance Fields 2739 [FieldOffset(0)] internal readonly SqlMoney Value; 2740 #endregion 2741 2742 #region Type-level Properties 2743 #endregion 2744 2745 #region Instance Properties 2746 public readonly object Val => Value; 2747 public readonly bool IsNULL => Value.IsNull; 2748 #endregion 2749 2750 #region Type-level Functions 2751 public static Result<smallmoney, Error> De(ref Deserializer des) { 2752 2753 var des2 = des; 2754 var resDecimal = des2.DeserializeDecimal(); 2755 2756 if (resDecimal.IsOK) { 2757 var dec = resDecimal.Unwrap(); 2758 2759 if (dec >= Min.Value && dec <= Max.Value && dec == new SqlMoney(dec).Value) { 2760 des = des2; 2761 return new(new smallmoney(new SqlMoney(dec))); 2762 } else { 2763 return new(Error.InvalidValue(Unexpected.Decimal(dec), validSmallmoney)); 2764 } 2765 } else { 2766 return new(resDecimal.UnwrapErr()); 2767 } 2768 } 2769 public static Result<smallmoney, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 2770 2771 var des2 = des; 2772 var resDecimal = des2.DeserializeDecimal(); 2773 2774 if (resDecimal.IsOK) { 2775 var dec = resDecimal.Unwrap(); 2776 2777 if (dec >= Min.Value && dec <= Max.Value && dec == new SqlMoney(dec).Value) { 2778 des = des2; 2779 return new(new smallmoney(new SqlMoney(dec))); 2780 } else { 2781 return new(Error.InvalidValue(Unexpected.Decimal(dec), validSmallmoney)); 2782 } 2783 } else { 2784 return new(resDecimal.UnwrapErr()); 2785 } 2786 } 2787 public static Result<smallmoney, OutOfRangeErr> New(SqlMoney val) => val < Min || val > Max ? new(OutOfRangeErr.OutOfRange) : new(new smallmoney(val)); 2788 #endregion 2789 2790 #region Instance Functions 2791 public override readonly bool Equals(object? _) => false; 2792 public override readonly int GetHashCode() => 0; 2793 public readonly smallmoney Into() => this; 2794 public readonly string IntoString() => ToString(); 2795 readonly string IInto<string>.Into() => IntoString(); 2796 public readonly any IntoAny() => any.smallmoney(this); 2797 public readonly Unit Ser(ref Serializer ser) { 2798 2799 if (IsNULL) { 2800 return ser.SerByte((byte)any.Tag.NULL); 2801 } else { 2802 _ = ser.SerByte((byte)any.Tag.smallmoney); 2803 return ser.SerDecimal(Value.Value); 2804 } 2805 } 2806 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 2807 2808 if (IsNULL) { 2809 return ser.SerializeByte((byte)any.Tag.NULL); 2810 } else { 2811 var res = ser.SerializeByte((byte)any.Tag.smallmoney); 2812 return res.IsErr ? res : ser.SerializeDecimal(Value.Value); 2813 } 2814 } 2815 public override readonly string ToString() => IsNULL ? "NULL" : Value.Value.ToString("N4"); 2816 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 2817 readonly Result<smallmoney, Bottom> ITryInto<smallmoney, Bottom>.TryInto() => new(this); 2818 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 2819 #endregion 2820 2821 #region Operators 2822 public static implicit operator SqlMoney(smallmoney val) => val.Value; 2823 #endregion 2824 2825 #region Types 2826 #endregion 2827 } 2828 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 32)] 2829 public readonly struct sql_variant: ISum<Unit, long, byte[], bool, string, System.DateTime, SqlDateTime, System.DateTime, System.DateTimeOffset, SqlDecimal, double, int, SqlMoney, string, string, float, SqlDateTime, short, SqlMoney, System.TimeSpan, byte, System.Guid, byte[], string>, IDataType, IInto<sql_variant>, IBinDeserializable<sql_variant> { 2830 2831 #region Type-level Constructors 2832 #endregion 2833 2834 #region Instance Constructors 2835 public sql_variant() => (_bigint, _binary, _bit, _char, _date, _datetime, _datetime2, _datetimeoffset, _decimal, _float, _int, _money, _nchar, _nvarchar, _real, _smalldatetime, _smallint, _smallmoney, _time, _tinyint, _uniqueidentifier, _varbinary, _varchar, Var) = (SQLServer.bigint.NULL.Value, SQLServer.binary.NULL.Value!, SQLServer.bit.NULL.Value, SQLServer.@char.NULL.Value!, SQLServer.date.NULL.Value, SQLServer.datetime.NULL.Value, SQLServer.datetime2.NULL.Value, SQLServer.datetimeoffset.NULL.Value, SQLServer.@decimal.NULL.Value, SQLServer.@float.NULL.Value, SQLServer.@int.NULL.Value, SQLServer.money.NULL.Value, SQLServer.nchar.NULL.Value!, SQLServer.nvarchar.NULL.Value!, SQLServer.real.NULL.Value, SQLServer.smalldatetime.NULL.Value, SQLServer.smallint.NULL.Value, SQLServer.smallmoney.NULL.Value, SQLServer.time.NULL.Value, SQLServer.tinyint.NULL.Value, SQLServer.uniqueidentifier.NULL.Value, SQLServer.varbinary.NULL.Value!, SQLServer.varchar.NULL.Value!, Tag.NULL); 2836 sql_variant(Tag kind, byte[] val) : this() => (Var, _binary) = (kind, val); 2837 sql_variant(Tag kind, string val) : this() => (Var, _char) = (kind, val); 2838 sql_variant(Tag kind, System.DateTime val) : this() => (Var, _date) = (kind, val); 2839 sql_variant(Tag kind, SqlDateTime val) : this() => (Var, _datetime) = (kind, val); 2840 sql_variant(Tag kind, SqlMoney val) : this() => (Var, _money) = (kind, val); 2841 sql_variant(long val) : this() => (Var, _bigint) = (Tag.bigint, val); 2842 sql_variant(bool val) : this() => (Var, _bit) = (Tag.bit, val); 2843 sql_variant(System.DateTimeOffset val) : this() => (Var, _datetimeoffset) = (Tag.datetimeoffset, val); 2844 sql_variant(SqlDecimal val) : this() => (Var, _decimal) = (Tag.@decimal, val); 2845 sql_variant(double val) : this() => (Var, _float) = (Tag.@float, val); 2846 sql_variant(int val) : this() => (Var, _int) = (Tag.@int, val); 2847 sql_variant(float val) : this() => (Var, _real) = (Tag.real, val); 2848 sql_variant(short val) : this() => (Var, _smallint) = (Tag.smallint, val); 2849 sql_variant(System.TimeSpan val) : this() => (Var, _time) = (Tag.time, val); 2850 sql_variant(byte val) : this() => (Var, _tinyint) = (Tag.tinyint, val); 2851 sql_variant(System.Guid val) : this() => (Var, _uniqueidentifier) = (Tag.uniqueidentifier, val); 2852 #endregion 2853 2854 #region Type-level Fields 2855 static readonly @string ZeroTo4000= "0–4000"; 2856 static readonly @string ZeroTo8000 = "0–8000"; 2857 static readonly Fn<bigint, sql_variant> fn_bigint = (x) => new(x.Value); 2858 static readonly Fn<binary, sql_variant> fn_binary = (x) => new(Tag.binary, x.Value!); 2859 static readonly Fn<bit, sql_variant> fn_bit = (x) => new(x.Value); 2860 static readonly Fn<@char, sql_variant> fn_char = (x) => new(Tag.@char, x.Value!); 2861 static readonly Fn<date, sql_variant> fn_date = (x) => new(x.Value); 2862 static readonly Fn<datetime, sql_variant> fn_datetime = (x) => new(Tag.datetime, x.Value); 2863 static readonly Fn<datetime2, sql_variant> fn_datetime2 = (x) => new(x.Value); 2864 static readonly Fn<datetimeoffset, sql_variant> fn_datetimeoffset = (x) => new(x.Value); 2865 static readonly Fn<@decimal, sql_variant> fn_decimal = (x) => new(x.Value); 2866 static readonly Fn<@float, sql_variant> fn_float = (x) => new(x.Value); 2867 static readonly Fn<@int, sql_variant> fn_int = (x) => new(x.Value); 2868 static readonly Fn<money, sql_variant> fn_money = (x) => new(Tag.money, x.Value); 2869 static readonly Fn<nchar, sql_variant> fn_nchar = (x) => new(Tag.nchar, x.Value!); 2870 static readonly Fn<nvarchar, Result<sql_variant, Error>> fn_nvarchar = (x) => x.Value!.Length > 4000 ? new(Error.InvalidLength((ulong)x.Value.Length, ZeroTo4000)) : new(new sql_variant(Tag.nvarchar, x.Value)); 2871 static readonly Fn<real, sql_variant> fn_real = (x) => new(x.Value); 2872 static readonly Fn<smalldatetime, sql_variant> fn_smalldatetime = (x) => new(Tag.smalldatetime, x.Value); 2873 static readonly Fn<smallint, sql_variant> fn_smallint = (x) => new(x.Value); 2874 static readonly Fn<smallmoney, sql_variant> fn_smallmoney = (x) => new(Tag.smallmoney, x.Value); 2875 static readonly Fn<time, sql_variant> fn_time = (x) => new(x.Value); 2876 static readonly Fn<tinyint, sql_variant> fn_tinyint = (x) => new(x.Value); 2877 static readonly Fn<uniqueidentifier, sql_variant> fn_uniqueidentifier = (x) => new(x.Value); 2878 static readonly Fn<varbinary, Result<sql_variant, Error>> fn_varbinary = (x) => x.Value!.Length > 8000 ? new(Error.InvalidLength((ulong)x.Value.Length, ZeroTo8000)) : new(new sql_variant(Tag.varbinary, x.Value)); 2879 static readonly Fn<varchar, Result<sql_variant, Error>> fn_varchar = (x) => x.Value!.Length > 8000 ? new(Error.InvalidLength((ulong)x.Value.Length, ZeroTo8000)) : new(new sql_variant(Tag.varchar, x.Value)); 2880 static readonly string[] variants = Enum.GetNames<Tag>(); 2881 public static readonly sql_variant NULL = new(); 2882 #endregion 2883 2884 #region Instance Fields 2885 [FieldOffset(0)] readonly long _bigint; 2886 [FieldOffset(0)] readonly bool _bit; 2887 [FieldOffset(0)] readonly System.DateTime _date; 2888 [FieldOffset(0)] readonly SqlDateTime _datetime; 2889 [FieldOffset(0)] readonly System.DateTime _datetime2; 2890 [FieldOffset(0)] readonly System.DateTimeOffset _datetimeoffset; 2891 [FieldOffset(0)] readonly SqlDecimal _decimal; 2892 [FieldOffset(0)] readonly double _float; 2893 [FieldOffset(0)] readonly int _int; 2894 [FieldOffset(0)] readonly SqlMoney _money; 2895 [FieldOffset(0)] readonly float _real; 2896 [FieldOffset(0)] readonly SqlDateTime _smalldatetime; 2897 [FieldOffset(0)] readonly short _smallint; 2898 [FieldOffset(0)] readonly SqlMoney _smallmoney; 2899 [FieldOffset(0)] readonly System.TimeSpan _time; 2900 [FieldOffset(0)] readonly byte _tinyint; 2901 [FieldOffset(0)] readonly System.Guid _uniqueidentifier; 2902 [FieldOffset(20)] public readonly Tag Var; 2903 [FieldOffset(24)] readonly byte[] _binary; 2904 [FieldOffset(24)] readonly string _char; 2905 [FieldOffset(24)] readonly string _nchar; 2906 [FieldOffset(24)] readonly string _nvarchar; 2907 [FieldOffset(24)] readonly byte[] _varbinary; 2908 [FieldOffset(24)] readonly string _varchar; 2909 #endregion 2910 2911 #region Type-level Properties 2912 #endregion 2913 2914 #region Instance Properties 2915 public readonly Var24 Variant => Var switch { 2916 Tag.NULL => Var24.V0, 2917 Tag.bigint => Var24.V1, 2918 Tag.binary => Var24.V2, 2919 Tag.bit => Var24.V3, 2920 Tag.@char => Var24.V4, 2921 Tag.date => Var24.V5, 2922 Tag.datetime => Var24.V6, 2923 Tag.datetime2 => Var24.V7, 2924 Tag.datetimeoffset => Var24.V8, 2925 Tag.@decimal => Var24.V9, 2926 Tag.@float => Var24.V10, 2927 Tag.@int => Var24.V11, 2928 Tag.money => Var24.V12, 2929 Tag.nchar => Var24.V13, 2930 Tag.nvarchar => Var24.V14, 2931 Tag.real => Var24.V15, 2932 Tag.smalldatetime => Var24.V16, 2933 Tag.smallint => Var24.V17, 2934 Tag.smallmoney => Var24.V18, 2935 Tag.time => Var24.V19, 2936 Tag.tinyint => Var24.V20, 2937 Tag.uniqueidentifier => Var24.V21, 2938 Tag.varbinary => Var24.V22, 2939 Tag.varchar => Var24.V23, 2940 _ => throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is invalid!"), 2941 }; 2942 public readonly Unit Variant0 => IntoNULL(); 2943 public readonly long Variant1 => IntoBigint(); 2944 public readonly byte[] Variant2 => IntoBinary(); 2945 public readonly bool Variant3 => IntoBit(); 2946 public readonly string Variant4 => IntoChar(); 2947 public readonly System.DateTime Variant5 => IntoDate(); 2948 public readonly SqlDateTime Variant6 => IntoDatetime(); 2949 public readonly System.DateTime Variant7 => IntoDatetime2(); 2950 public readonly System.DateTimeOffset Variant8 => IntoDatetimeoffset(); 2951 public readonly SqlDecimal Variant9 => IntoDecimal(); 2952 public readonly double Variant10 => IntoFloat(); 2953 public readonly int Variant11 => IntoInt(); 2954 public readonly SqlMoney Variant12 => IntoMoney(); 2955 public readonly string Variant13 => IntoNchar(); 2956 public readonly string Variant14 => IntoNvarchar(); 2957 public readonly float Variant15 => IntoReal(); 2958 public readonly SqlDateTime Variant16 => IntoSmalldatetime(); 2959 public readonly short Variant17 => IntoSmallint(); 2960 public readonly SqlMoney Variant18 => IntoSmallmoney(); 2961 public readonly System.TimeSpan Variant19 => IntoTime(); 2962 public readonly byte Variant20 => IntoTinyint(); 2963 public readonly System.Guid Variant21 => IntoUniqueidentifier(); 2964 public readonly byte[] Variant22 => IntoVarbinary(); 2965 public readonly string Variant23 => IntoVarchar(); 2966 public readonly object Val => Var switch { 2967 Tag.NULL => NULL, 2968 Tag.bigint => _bigint, 2969 Tag.binary => _binary, 2970 Tag.bit => _bit, 2971 Tag.@char => _char, 2972 Tag.date => _date, 2973 Tag.datetime => _datetime, 2974 Tag.datetime2 => _datetime2, 2975 Tag.datetimeoffset => _datetimeoffset, 2976 Tag.@decimal => _decimal, 2977 Tag.@float => _float, 2978 Tag.@int => _int, 2979 Tag.money => _money, 2980 Tag.nchar => _nchar, 2981 Tag.nvarchar => _nvarchar, 2982 Tag.real => _real, 2983 Tag.smalldatetime => _smalldatetime, 2984 Tag.smallint => _smallint, 2985 Tag.smallmoney => _smallmoney, 2986 Tag.time => _time, 2987 Tag.tinyint => _tinyint, 2988 Tag.uniqueidentifier => _uniqueidentifier, 2989 Tag.varbinary => _varbinary, 2990 Tag.varchar => _varchar, 2991 _ => throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is invalid!"), 2992 }; 2993 public readonly bool IsNULL => Var == Tag.NULL; 2994 #endregion 2995 2996 #region Type-level Functions 2997 public static Result<sql_variant, Error> De(ref Deserializer des) { 2998 2999 var des2 = des; 3000 var resByte = des2.DeserializeByte(); 3001 3002 if (resByte.IsOK) { 3003 var tag = (Tag)resByte.Unwrap(); 3004 var resSql_variant = tag switch { 3005 Tag.NULL => new(NULL), 3006 Tag.bigint => SQLServer.bigint.De(ref des2).Map(fn_bigint), 3007 Tag.binary => SQLServer.binary.De(ref des2).Map(fn_binary), 3008 Tag.bit => SQLServer.bit.De(ref des2).Map(fn_bit), 3009 Tag.@char => SQLServer.@char.De(ref des2).Map(fn_char), 3010 Tag.date => SQLServer.date.De(ref des2).Map(fn_date), 3011 Tag.datetime => SQLServer.datetime.De(ref des2).Map(fn_datetime), 3012 Tag.datetime2 => SQLServer.datetime2.De(ref des2).Map(fn_datetime2), 3013 Tag.datetimeoffset => SQLServer.datetimeoffset.De(ref des2).Map(fn_datetimeoffset), 3014 Tag.@decimal => SQLServer.@decimal.De(ref des2).Map(fn_decimal), 3015 Tag.@float => SQLServer.@float.De(ref des2).Map(fn_float), 3016 Tag.@int => SQLServer.@int.De(ref des2).Map(fn_int), 3017 Tag.money => SQLServer.money.De(ref des2).Map(fn_money), 3018 Tag.nchar => SQLServer.nchar.De(ref des2).Map(fn_nchar), 3019 Tag.nvarchar => SQLServer.nvarchar.De(ref des2).AndThen(fn_nvarchar), 3020 Tag.real => SQLServer.real.De(ref des2).Map(fn_real), 3021 Tag.smalldatetime => SQLServer.smalldatetime.De(ref des2).Map(fn_smalldatetime), 3022 Tag.smallint => SQLServer.smallint.De(ref des2).Map(fn_smallint), 3023 Tag.smallmoney => SQLServer.smallmoney.De(ref des2).Map(fn_smallmoney), 3024 Tag.time => SQLServer.time.De(ref des2).Map(fn_time), 3025 Tag.tinyint => SQLServer.tinyint.De(ref des2).Map(fn_tinyint), 3026 Tag.uniqueidentifier => SQLServer.uniqueidentifier.De(ref des2).Map(fn_uniqueidentifier), 3027 Tag.varbinary => SQLServer.varbinary.De(ref des2).AndThen(fn_varbinary), 3028 Tag.varchar => SQLServer.varchar.De(ref des2).AndThen(fn_varchar), 3029 _ => new(Error.UnknownVariant(tag.ToString(), variants)), 3030 }; 3031 if (resSql_variant.IsOK) { des = des2; } 3032 return resSql_variant; 3033 } else { 3034 return new(resByte.UnwrapErr()); 3035 } 3036 } 3037 public static Result<sql_variant, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3038 3039 var des2 = des; 3040 var resByte = des2.DeserializeByte(); 3041 3042 if (resByte.IsOK) { 3043 var tag = (Tag)resByte.Unwrap(); 3044 var resSql_variant = tag switch { 3045 Tag.NULL => new(NULL), 3046 Tag.bigint => SQLServer.bigint.Deserialize(ref des2).Map(fn_bigint), 3047 Tag.binary => SQLServer.binary.Deserialize(ref des2).Map(fn_binary), 3048 Tag.bit => SQLServer.bit.Deserialize(ref des2).Map(fn_bit), 3049 Tag.@char => SQLServer.@char.Deserialize(ref des2).Map(fn_char), 3050 Tag.date => SQLServer.date.Deserialize(ref des2).Map(fn_date), 3051 Tag.datetime => SQLServer.datetime.Deserialize(ref des2).Map(fn_datetime), 3052 Tag.datetime2 => SQLServer.datetime2.Deserialize(ref des2).Map(fn_datetime2), 3053 Tag.datetimeoffset => SQLServer.datetimeoffset.Deserialize(ref des2).Map(fn_datetimeoffset), 3054 Tag.@decimal => SQLServer.@decimal.Deserialize(ref des2).Map(fn_decimal), 3055 Tag.@float => SQLServer.@float.Deserialize(ref des2).Map(fn_float), 3056 Tag.@int => SQLServer.@int.Deserialize(ref des2).Map(fn_int), 3057 Tag.money => SQLServer.money.Deserialize(ref des2).Map(fn_money), 3058 Tag.nchar => SQLServer.nchar.Deserialize(ref des2).Map(fn_nchar), 3059 Tag.nvarchar => SQLServer.nvarchar.Deserialize(ref des2).AndThen(fn_nvarchar), 3060 Tag.real => SQLServer.real.Deserialize(ref des2).Map(fn_real), 3061 Tag.smalldatetime => SQLServer.smalldatetime.Deserialize(ref des2).Map(fn_smalldatetime), 3062 Tag.smallint => SQLServer.smallint.Deserialize(ref des2).Map(fn_smallint), 3063 Tag.smallmoney => SQLServer.smallmoney.Deserialize(ref des2).Map(fn_smallmoney), 3064 Tag.time => SQLServer.time.Deserialize(ref des2).Map(fn_time), 3065 Tag.tinyint => SQLServer.tinyint.Deserialize(ref des2).Map(fn_tinyint), 3066 Tag.uniqueidentifier => SQLServer.uniqueidentifier.Deserialize(ref des2).Map(fn_uniqueidentifier), 3067 Tag.varbinary => SQLServer.varbinary.Deserialize(ref des2).AndThen(fn_varbinary), 3068 Tag.varchar => SQLServer.varchar.Deserialize(ref des2).AndThen(fn_varchar), 3069 _ => new(Error.UnknownVariant(tag.ToString(), variants)), 3070 }; 3071 if (resSql_variant.IsOK) { des = des2; } 3072 return resSql_variant; 3073 } else { 3074 return new(resByte.UnwrapErr()); 3075 } 3076 } 3077 public static sql_variant bigint(bigint val) => val.IsNULL ? NULL : new(val.Value); 3078 public static sql_variant binary(binary val) => val.IsNULL ? NULL : new(Tag.binary, val.Value!); 3079 public static sql_variant bit(bit val) => val.IsNULL ? NULL : new(val.Value); 3080 public static sql_variant @char(@char val) => val.IsNULL ? NULL : new(Tag.@char, val.Value!); 3081 public static sql_variant date(date val) => val.IsNULL ? NULL : new(Tag.date, val.Value!); 3082 public static sql_variant datetime(datetime val) => val.IsNULL ? NULL : new(Tag.datetime, val.Value); 3083 public static sql_variant datetime2(datetime2 val) => val.IsNULL ? NULL : new(Tag.datetime2, val.Value!); 3084 public static sql_variant datetimeoffset(datetimeoffset val) => val.IsNULL ? NULL : new(val.Value); 3085 public static sql_variant @decimal(@decimal val) => val.IsNULL ? NULL : new(val.Value); 3086 public static sql_variant @float(@float val) => val.IsNULL ? NULL : new(val.Value); 3087 public static sql_variant @int(@int val) => val.IsNULL ? NULL : new(val.Value); 3088 public static sql_variant money(money val) => val.IsNULL ? NULL : new(Tag.money, val.Value); 3089 public static sql_variant nchar(nchar val) => val.IsNULL ? NULL : new(Tag.nchar, val.Value!); 3090 public static Result<sql_variant, OutOfRangeErr> nvarchar(nvarchar val) => val.IsNULL ? new(NULL) : val.Value!.Length > 4000 ? new(OutOfRangeErr.OutOfRange) : new(new sql_variant(Tag.nvarchar, val.Value)); 3091 public static sql_variant real(real val) => val.IsNULL ? NULL : new(val.Value); 3092 public static sql_variant smalldatetime(smalldatetime val) => val.IsNULL ? NULL : new(Tag.smalldatetime, val.Value); 3093 public static sql_variant smallint(smallint val) => val.IsNULL ? NULL : new(val.Value); 3094 public static sql_variant smallmoney(smallmoney val) => val.IsNULL ? NULL : new(Tag.smallmoney, val.Value); 3095 public static sql_variant time(time val) => val.IsNULL ? NULL : new(val.Value); 3096 public static sql_variant tinyint(tinyint val) => val.IsNULL ? NULL : new(val.Value); 3097 public static sql_variant uniqueidentifier(uniqueidentifier val) => val.IsNULL ? NULL : new(val.Value); 3098 public static Result<sql_variant, OutOfRangeErr> varbinary(varbinary val) => val.IsNULL ? new(NULL) : val.Value!.Length > 8000 ? new(OutOfRangeErr.OutOfRange) : new(new sql_variant(Tag.varbinary, val.Value)); 3099 public static Result<sql_variant, OutOfRangeErr> varchar(varchar val) => val.IsNULL ? new(NULL) : val.Value!.Length > 8000 ? new(OutOfRangeErr.OutOfRange) : new(new sql_variant(Tag.varchar, val.Value)); 3100 #endregion 3101 3102 #region Instance Functions 3103 public override readonly bool Equals(object? _) => false; 3104 public override readonly int GetHashCode() => 0; 3105 public readonly sql_variant Into() => this; 3106 public readonly string IntoString() => ToString(); 3107 readonly string IInto<string>.Into() => IntoString(); 3108 public readonly Unit IntoNULL() => Var == Tag.NULL ? new Unit() : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not NULL!"); 3109 public readonly long IntoBigint() => Var == Tag.bigint ? _bigint : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not bigint!"); 3110 public readonly byte[] IntoBinary() => Var == Tag.binary ? _binary : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not binary!"); 3111 public readonly bool IntoBit() => Var == Tag.bit ? _bit : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not bit!"); 3112 public readonly string IntoChar() => Var == Tag.@char ? _char : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not @char!"); 3113 public readonly System.DateTime IntoDate() => Var == Tag.date ? _date : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not date!"); 3114 public readonly SqlDateTime IntoDatetime() => Var == Tag.datetime ? _datetime : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not datetime!"); 3115 public readonly System.DateTime IntoDatetime2() => Var == Tag.datetime2 ? _datetime2 : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not datetime2!"); 3116 public readonly System.DateTimeOffset IntoDatetimeoffset() => Var == Tag.datetimeoffset ? _datetimeoffset : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not datetimeoffset!"); 3117 public readonly SqlDecimal IntoDecimal() => Var == Tag.@decimal ? _decimal : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not @decimal!"); 3118 public readonly double IntoFloat() => Var == Tag.@float ? _float : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not @float!"); 3119 public readonly int IntoInt() => Var == Tag.@int ? _int : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not an @int!"); 3120 public readonly SqlMoney IntoMoney() => Var == Tag.money ? _money : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not money!"); 3121 public readonly string IntoNchar() => Var == Tag.nchar ? _nchar : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not an nchar!"); 3122 public readonly string IntoNvarchar() => Var == Tag.nvarchar ? _nvarchar : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not an nvarchar!"); 3123 public readonly float IntoReal() => Var == Tag.real ? _real : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not real!"); 3124 public readonly SqlDateTime IntoSmalldatetime() => Var == Tag.smalldatetime ? _smalldatetime : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not smalldatetime!"); 3125 public readonly short IntoSmallint() => Var == Tag.smallint ? _smallint : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not smallint!"); 3126 public readonly SqlMoney IntoSmallmoney() => Var == Tag.smallmoney ? _smallmoney : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not smallmoney!"); 3127 public readonly System.TimeSpan IntoTime() => Var == Tag.time ? _time : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not time!"); 3128 public readonly byte IntoTinyint() => Var == Tag.tinyint ? _tinyint : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not tinyint!"); 3129 public readonly System.Guid IntoUniqueidentifier() => Var == Tag.uniqueidentifier ? _uniqueidentifier : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not uniqueidentifier!"); 3130 public readonly byte[] IntoVarbinary() => Var == Tag.varbinary ? _varbinary : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not varbinary!"); 3131 public readonly string IntoVarchar() => Var == Tag.varchar ? _varchar : throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is not varchar!"); 3132 public readonly any IntoAny() => any.sql_variant(this); 3133 public readonly Unit Ser(ref Serializer ser) { 3134 3135 if (Var == Tag.NULL) { 3136 return ser.SerByte((byte)any.Tag.NULL); 3137 } else { 3138 _ = ser.SerByte((byte)any.Tag.sql_variant); 3139 return Var switch { 3140 Tag.bigint => new bigint(_bigint).Ser(ref ser), 3141 Tag.binary => new binary(_binary).Ser(ref ser), 3142 Tag.bit => new bit(_bit).Ser(ref ser), 3143 Tag.@char => new @char(_char).Ser(ref ser), 3144 Tag.date => new date(_date).Ser(ref ser), 3145 Tag.datetime => new datetime(_datetime).Ser(ref ser), 3146 Tag.datetime2 => new datetime2(_datetime2).Ser(ref ser), 3147 Tag.datetimeoffset => new datetimeoffset(_datetimeoffset).Ser(ref ser), 3148 Tag.@decimal => new @decimal(_decimal).Ser(ref ser), 3149 Tag.@float => new @float(_float).Ser(ref ser), 3150 Tag.@int => new @int(_int).Ser(ref ser), 3151 Tag.money => new money(_money).Ser(ref ser), 3152 Tag.nchar => new nchar(_nchar).Ser(ref ser), 3153 Tag.nvarchar => new nvarchar(_nvarchar).Ser(ref ser), 3154 Tag.real => new real(_real).Ser(ref ser), 3155 Tag.smalldatetime => new smalldatetime(_smalldatetime).Ser(ref ser), 3156 Tag.smallint => new smallint(_smallint).Ser(ref ser), 3157 Tag.smallmoney => new smallmoney(_smallmoney).Ser(ref ser), 3158 Tag.time => new time(_time).Ser(ref ser), 3159 Tag.tinyint => new tinyint(_tinyint).Ser(ref ser), 3160 Tag.uniqueidentifier => new uniqueidentifier(_uniqueidentifier).Ser(ref ser), 3161 Tag.varbinary => new varbinary(_varbinary).Ser(ref ser), 3162 Tag.varchar => new varchar(_varchar).Ser(ref ser), 3163 _ => throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is invalid!"), 3164 }; 3165 } 3166 } 3167 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3168 3169 if (Var == Tag.NULL) { 3170 return ser.SerializeByte((byte)any.Tag.NULL); 3171 } else { 3172 var res = ser.SerializeByte((byte)any.Tag.sql_variant); 3173 return res.IsOK 3174 ? Var switch { 3175 Tag.bigint => new bigint(_bigint).Serialize<TSer, TOK, TErr>(ref ser), 3176 Tag.binary => new binary(_binary).Serialize<TSer, TOK, TErr>(ref ser), 3177 Tag.bit => new bit(_bit).Serialize<TSer, TOK, TErr>(ref ser), 3178 Tag.@char => new @char(_char).Serialize<TSer, TOK, TErr>(ref ser), 3179 Tag.date => new date(_date).Serialize<TSer, TOK, TErr>(ref ser), 3180 Tag.datetime => new datetime(_datetime).Serialize<TSer, TOK, TErr>(ref ser), 3181 Tag.datetime2 => new datetime2(_datetime2).Serialize<TSer, TOK, TErr>(ref ser), 3182 Tag.datetimeoffset => new datetimeoffset(_datetimeoffset).Serialize<TSer, TOK, TErr>(ref ser), 3183 Tag.@decimal => new @decimal(_decimal).Serialize<TSer, TOK, TErr>(ref ser), 3184 Tag.@float => new @float(_float).Serialize<TSer, TOK, TErr>(ref ser), 3185 Tag.@int => new @int(_int).Serialize<TSer, TOK, TErr>(ref ser), 3186 Tag.money => new money(_money).Serialize<TSer, TOK, TErr>(ref ser), 3187 Tag.nchar => new nchar(_nchar).Serialize<TSer, TOK, TErr>(ref ser), 3188 Tag.nvarchar => new nvarchar(_nvarchar).Serialize<TSer, TOK, TErr>(ref ser), 3189 Tag.real => new real(_real).Serialize<TSer, TOK, TErr>(ref ser), 3190 Tag.smalldatetime => new smalldatetime(_smalldatetime).Serialize<TSer, TOK, TErr>(ref ser), 3191 Tag.smallint => new smallint(_smallint).Serialize<TSer, TOK, TErr>(ref ser), 3192 Tag.smallmoney => new smallmoney(_smallmoney).Serialize<TSer, TOK, TErr>(ref ser), 3193 Tag.time => new time(_time).Serialize<TSer, TOK, TErr>(ref ser), 3194 Tag.tinyint => new tinyint(_tinyint).Serialize<TSer, TOK, TErr>(ref ser), 3195 Tag.uniqueidentifier => new uniqueidentifier(_uniqueidentifier).Serialize<TSer, TOK, TErr>(ref ser), 3196 Tag.varbinary => new varbinary(_varbinary).Serialize<TSer, TOK, TErr>(ref ser), 3197 Tag.varchar => new varchar(_varchar).Serialize<TSer, TOK, TErr>(ref ser), 3198 _ => throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is invalid!"), 3199 } 3200 : new(res.UnwrapErr()); 3201 } 3202 } 3203 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1065:Do not raise exceptions in unexpected locations", Justification = "Need to do this in the event an unhandled variant exists.")] 3204 public override readonly string ToString() => $@"{Var.ToString()}({Var switch { 3205 Tag.NULL => "NULL", 3206 Tag.bigint => _bigint.ToString(), 3207 Tag.binary => _binary.AsSpan().UpperHex(), 3208 Tag.bit => _bit.ToString(), 3209 Tag.@char => _char, 3210 Tag.date => _date.ToString("yyyy-MM-dd"), 3211 Tag.datetime => _datetime.Value.ToString("yyyy-MM-ddTHH:mm:ss.fff"), 3212 Tag.datetime2 => _datetime2.ToString("O"), 3213 Tag.datetimeoffset => _datetimeoffset.ToString("O"), 3214 Tag.@decimal => _decimal.ToString(), 3215 Tag.@float => _float.ToString("G17"), 3216 Tag.@int => _int.ToString(), 3217 Tag.money => _money.Value.ToString("N4"), 3218 Tag.nchar => _nchar, 3219 Tag.nvarchar => _nvarchar, 3220 Tag.real => _real.ToString("G9"), 3221 Tag.smalldatetime => _smalldatetime.Value.ToString("yyyy-MM-ddTHH:mm"), 3222 Tag.smallint => _smallint.ToString(), 3223 Tag.smallmoney => _smallmoney.Value.ToString("N4"), 3224 Tag.time => _time.ToString("c"), 3225 Tag.tinyint => _tinyint.ToString(), 3226 Tag.uniqueidentifier => _uniqueidentifier.ToString("D"), 3227 Tag.varbinary => _varbinary.AsSpan().UpperHex(), 3228 Tag.varchar => _varchar, 3229 _ => throw new InvalidOperationException($"The sql_variant variant {Var.ToString()} is invalid!"), 3230 }})"; 3231 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 3232 readonly Result<sql_variant, Bottom> ITryInto<sql_variant, Bottom>.TryInto() => new(this); 3233 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3234 #endregion 3235 3236 #region Operators 3237 #endregion 3238 3239 #region Types 3240 // MUST match any.Tag for overlapped variants! 3241 public enum Tag: uint { 3242 NULL = (uint)any.Tag.NULL, 3243 bigint = (uint)any.Tag.bigint, 3244 binary = (uint)any.Tag.binary, 3245 bit = (uint)any.Tag.bit, 3246 @char = (uint)any.Tag.@char, 3247 date = (uint)any.Tag.date, 3248 datetime = (uint)any.Tag.datetime, 3249 datetime2 = (uint)any.Tag.datetime2, 3250 datetimeoffset = (uint)any.Tag.datetimeoffset, 3251 @decimal = (uint)any.Tag.@decimal, 3252 @float = (uint)any.Tag.@float, 3253 @int = (uint)any.Tag.@int, 3254 money = (uint)any.Tag.money, 3255 nchar = (uint)any.Tag.nchar, 3256 nvarchar = (uint)any.Tag.nvarchar, 3257 real = (uint)any.Tag.real, 3258 smalldatetime = (uint)any.Tag.smalldatetime, 3259 smallint = (uint)any.Tag.smallint, 3260 smallmoney = (uint)any.Tag.smallmoney, 3261 time = (uint)any.Tag.time, 3262 tinyint = (uint)any.Tag.tinyint, 3263 uniqueidentifier = (uint)any.Tag.uniqueidentifier, 3264 varbinary = (uint)any.Tag.varbinary, 3265 varchar = (uint)any.Tag.varchar, 3266 } 3267 #endregion 3268 } 3269 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 3270 public readonly struct text: IDataType, IInto<text>, IBinDeserializable<text> { 3271 3272 #region Type-level Constructors 3273 #endregion 3274 3275 #region Instance Constructors 3276 public text() => Value = null; 3277 public text(string val) => Value = val; 3278 #endregion 3279 3280 #region Type-level Fields 3281 public static readonly text NULL = new(); 3282 #endregion 3283 3284 #region Instance Fields 3285 [FieldOffset(0)] internal readonly string? Value; 3286 #endregion 3287 3288 #region Type-level Properties 3289 #endregion 3290 3291 #region Instance Properties 3292 public readonly object Val => Value; 3293 public readonly bool IsNULL => Value is null; 3294 #endregion 3295 3296 #region Type-level Functions 3297 public static Result<text, Error> De(ref Deserializer des) { 3298 3299 var des2 = des; 3300 var resUint = des2.DeserializeUint(); 3301 3302 if (resUint.IsOK) { 3303 var len = resUint.Unwrap(); 3304 var chars = des2.DeserializeChars(len); 3305 3306 if ((uint)chars.Length == len) { 3307 des = des2; 3308 return new(new text(new string(chars))); 3309 } else { 3310 return new(Error.InvalidLength((ulong)chars.Length, (@uint)len)); 3311 } 3312 } else { 3313 return new(resUint.UnwrapErr()); 3314 } 3315 } 3316 public static Result<text, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3317 3318 var des2 = des; 3319 var resUint = des2.DeserializeUint(); 3320 3321 if (resUint.IsOK) { 3322 var len = resUint.Unwrap(); 3323 uint l; 3324 var chars = new char[len]; 3325 3326 if ((l = des2.DeserializeChars(chars)) == len) { 3327 des = des2; 3328 return new(new text(new string(chars))); 3329 } else { 3330 return new(Error.InvalidLength((ulong)l, (@uint)len)); 3331 } 3332 } else { 3333 return new(resUint.UnwrapErr()); 3334 } 3335 } 3336 public static text New(string val) => new(val); 3337 #endregion 3338 3339 #region Instance Functions 3340 public override readonly bool Equals(object? _) => false; 3341 public override readonly int GetHashCode() => 0; 3342 public readonly text Into() => this; 3343 public readonly string IntoString() => ToString(); 3344 public readonly any IntoAny() => any.text(this); 3345 readonly string IInto<string>.Into() => IntoString(); 3346 public readonly Unit Ser(ref Serializer ser) { 3347 3348 if (IsNULL) { 3349 return ser.SerByte((byte)any.Tag.NULL); 3350 } else { 3351 _ = ser.SerByte((byte)any.Tag.text); 3352 _ = ser.SerUint((uint)Value!.Length); 3353 return ser.SerChars(Value.AsSpan()); 3354 } 3355 } 3356 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3357 3358 if (IsNULL) { 3359 return ser.SerializeByte((byte)any.Tag.NULL); 3360 } else { 3361 var res = ser.SerializeByte((byte)any.Tag.text); 3362 if (res.IsErr) { return res; } 3363 var res2 = ser.SerializeUint((uint)Value!.Length); 3364 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 3365 } 3366 } 3367 public override readonly string ToString() => IsNULL ? "NULL" : Value; 3368 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 3369 readonly Result<text, Bottom> ITryInto<text, Bottom>.TryInto() => new(this); 3370 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3371 #endregion 3372 3373 #region Operators 3374 public static implicit operator text(string val) => new(val); 3375 #endregion 3376 3377 #region Types 3378 #endregion 3379 } 3380 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 3381 public readonly struct time: IDataType, IInto<time>, IBinDeserializable<time> { 3382 3383 #region Type-level Constructors 3384 #endregion 3385 3386 #region Instance Constructors 3387 public time() => Value = new System.TimeSpan(System.TimeSpan.TicksPerDay); 3388 internal time(System.TimeSpan val) => Value = val; 3389 #endregion 3390 3391 #region Type-level Fields 3392 static readonly @string validTime = $"[{System.TimeSpan.Zero.ToString("c")}, {new System.TimeSpan(System.TimeSpan.TicksPerDay).ToString("c")})"; 3393 public static readonly time NULL = new(); 3394 #endregion 3395 3396 #region Instance Fields 3397 [FieldOffset(0)] internal readonly System.TimeSpan Value; 3398 #endregion 3399 3400 #region Type-level Properties 3401 #endregion 3402 3403 #region Instance Properties 3404 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 3405 public readonly bool IsNULL => Value.Ticks == System.TimeSpan.TicksPerDay; 3406 #endregion 3407 3408 #region Type-level Functions 3409 public static Result<time, Error> De(ref Deserializer des) { 3410 3411 var des2 = des; 3412 var resTimeSpan = des2.DeserializeTimeSpan(); 3413 3414 if (resTimeSpan.IsOK) { 3415 var time = resTimeSpan.Unwrap(); 3416 3417 if (time >= System.TimeSpan.Zero && time < new System.TimeSpan(System.TimeSpan.TicksPerDay)) { 3418 des = des2; 3419 return new(new time(time)); 3420 } else { 3421 return new(Error.InvalidValue(Unexpected.TimeSpan(time), validTime)); 3422 } 3423 } else { 3424 return new(resTimeSpan.UnwrapErr()); 3425 } 3426 } 3427 public static Result<time, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3428 3429 var des2 = des; 3430 var resTimeSpan = des2.DeserializeTimeSpan(); 3431 3432 if (resTimeSpan.IsOK) { 3433 var time = resTimeSpan.Unwrap(); 3434 3435 if (time >= System.TimeSpan.Zero && time < new System.TimeSpan(System.TimeSpan.TicksPerDay)) { 3436 des = des2; 3437 return new(new time(time)); 3438 } else { 3439 return new(Error.InvalidValue(Unexpected.TimeSpan(time), validTime)); 3440 } 3441 } else { 3442 return new(resTimeSpan.UnwrapErr()); 3443 } 3444 } 3445 public static Result<time, OutOfRangeErr> New(System.TimeSpan val) => val < System.TimeSpan.Zero || val.Ticks >= System.TimeSpan.TicksPerDay ? new(OutOfRangeErr.OutOfRange) : new(new time(val)); 3446 #endregion 3447 3448 #region Instance Functions 3449 public override readonly bool Equals(object? _) => false; 3450 public override readonly int GetHashCode() => 0; 3451 public readonly time Into() => this; 3452 public readonly string IntoString() => ToString(); 3453 public readonly any IntoAny() => any.time(this); 3454 readonly string IInto<string>.Into() => IntoString(); 3455 public readonly Unit Ser(ref Serializer ser) { 3456 3457 if (IsNULL) { 3458 return ser.SerByte((byte)any.Tag.NULL); 3459 } else { 3460 _ = ser.SerByte((byte)any.Tag.time); 3461 return ser.SerTimeSpan(Value); 3462 } 3463 } 3464 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3465 3466 if (IsNULL) { 3467 return ser.SerializeByte((byte)any.Tag.NULL); 3468 } else { 3469 var res = ser.SerializeByte((byte)any.Tag.time); 3470 return res.IsErr ? res : ser.SerializeTimeSpan(Value); 3471 } 3472 } 3473 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("c"); 3474 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 3475 readonly Result<time, Bottom> ITryInto<time, Bottom>.TryInto() => new(this); 3476 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3477 #endregion 3478 3479 #region Operators 3480 #endregion 3481 3482 #region Types 3483 #endregion 3484 } 3485 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 1, Size = 2)] 3486 public readonly struct tinyint: IDataType, IInto<tinyint>, IBinDeserializable<tinyint> { 3487 3488 #region Type-level Constructors 3489 #endregion 3490 3491 #region Instance Constructors 3492 public tinyint() => (Value, _isNULL) = (byte.MinValue, byte.MinValue); 3493 public tinyint(byte val) => (Value, _isNULL) = (val, byte.MaxValue); 3494 #endregion 3495 3496 #region Type-level Fields 3497 public static readonly tinyint NULL = new(); 3498 #endregion 3499 3500 #region Instance Fields 3501 [FieldOffset(0)] internal readonly byte Value; 3502 [FieldOffset(1)] readonly byte _isNULL; 3503 #endregion 3504 3505 #region Type-level Properties 3506 #endregion 3507 3508 #region Instance Properties 3509 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 3510 public readonly bool IsNULL => _isNULL == byte.MinValue; 3511 #endregion 3512 3513 #region Type-level Functions 3514 public static Result<tinyint, Error> De(ref Deserializer des) { 3515 3516 var des2 = des; 3517 var resByte = des2.DeserializeByte(); 3518 3519 if (resByte.IsOK) { 3520 des = des2; 3521 return new(new tinyint(resByte.Unwrap())); 3522 } else { 3523 return new(resByte.UnwrapErr()); 3524 } 3525 } 3526 public static Result<tinyint, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3527 3528 var des2 = des; 3529 var resByte = des2.DeserializeByte(); 3530 3531 if (resByte.IsOK) { 3532 des = des2; 3533 return new(new tinyint(resByte.Unwrap())); 3534 } else { 3535 return new(resByte.UnwrapErr()); 3536 } 3537 } 3538 public static tinyint New(byte val) => new(val); 3539 #endregion 3540 3541 #region Instance Functions 3542 public override readonly bool Equals(object? _) => false; 3543 public override readonly int GetHashCode() => 0; 3544 public readonly tinyint Into() => this; 3545 public readonly string IntoString() => ToString(); 3546 readonly string IInto<string>.Into() => IntoString(); 3547 public readonly any IntoAny() => any.tinyint(this); 3548 public readonly Unit Ser(ref Serializer ser) { 3549 3550 if (IsNULL) { 3551 return ser.SerByte((byte)any.Tag.NULL); 3552 } else { 3553 _ = ser.SerByte((byte)any.Tag.tinyint); 3554 return ser.SerByte(Value); 3555 } 3556 } 3557 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3558 3559 if (IsNULL) { 3560 return ser.SerializeByte((byte)any.Tag.NULL); 3561 } else { 3562 var res = ser.SerializeByte((byte)any.Tag.tinyint); 3563 return res.IsErr ? res : ser.SerializeByte(Value); 3564 } 3565 } 3566 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString(); 3567 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 3568 readonly Result<tinyint, Bottom> ITryInto<tinyint, Bottom>.TryInto() => new(this); 3569 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3570 #endregion 3571 3572 #region Operators 3573 public static implicit operator tinyint(byte val) => new(val); 3574 #endregion 3575 3576 #region Types 3577 #endregion 3578 } 3579 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 4, Size = 20)] 3580 public readonly struct uniqueidentifier: IDataType, IInto<uniqueidentifier>, IBinDeserializable<uniqueidentifier> { 3581 3582 #region Type-level Constructors 3583 #endregion 3584 3585 #region Instance Constructors 3586 public uniqueidentifier() => (Value, _isNULL) = (System.Guid.Empty, uint.MinValue); 3587 public uniqueidentifier(System.Guid val) => (Value, _isNULL) = (val, uint.MaxValue); 3588 #endregion 3589 3590 #region Type-level Fields 3591 public static readonly uniqueidentifier NULL = new(); 3592 #endregion 3593 3594 #region Instance Fields 3595 [FieldOffset(0)] internal readonly System.Guid Value; 3596 [FieldOffset(16)] readonly uint _isNULL; 3597 #endregion 3598 3599 #region Type-level Properties 3600 #endregion 3601 3602 #region Instance Properties 3603 public readonly object Val => IsNULL ? throw new InvalidOperationException() : Value; 3604 public readonly bool IsNULL => _isNULL == uint.MinValue; 3605 #endregion 3606 3607 #region Type-level Functions 3608 public static Result<uniqueidentifier, Error> De(ref Deserializer des) { 3609 3610 var des2 = des; 3611 var resGuid = des2.DeserializeGuid(); 3612 3613 if (resGuid.IsOK) { 3614 des = des2; 3615 return new(new uniqueidentifier(resGuid.Unwrap())); 3616 } else { 3617 return new(resGuid.UnwrapErr()); 3618 } 3619 } 3620 public static Result<uniqueidentifier, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3621 3622 var des2 = des; 3623 var resGuid = des2.DeserializeGuid(); 3624 3625 if (resGuid.IsOK) { 3626 des = des2; 3627 return new(new uniqueidentifier(resGuid.Unwrap())); 3628 } else { 3629 return new(resGuid.UnwrapErr()); 3630 } 3631 } 3632 public static uniqueidentifier New(System.Guid val) => new(val); 3633 #endregion 3634 3635 #region Instance Functions 3636 public override readonly bool Equals(object? _) => false; 3637 public override readonly int GetHashCode() => 0; 3638 public readonly uniqueidentifier Into() => this; 3639 public readonly string IntoString() => ToString(); 3640 readonly string IInto<string>.Into() => IntoString(); 3641 public readonly any IntoAny() => any.uniqueidentifier(this); 3642 public readonly Unit Ser(ref Serializer ser) { 3643 3644 if (IsNULL) { 3645 return ser.SerByte((byte)any.Tag.NULL); 3646 } else { 3647 _ = ser.SerByte((byte)any.Tag.uniqueidentifier); 3648 return ser.SerGuid(Value); 3649 } 3650 } 3651 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3652 3653 if (IsNULL) { 3654 return ser.SerializeByte((byte)any.Tag.NULL); 3655 } else { 3656 var res = ser.SerializeByte((byte)any.Tag.uniqueidentifier); 3657 return res.IsErr ? res : ser.SerializeGuid(Value); 3658 } 3659 } 3660 public override readonly string ToString() => IsNULL ? "NULL" : Value.ToString("D"); 3661 readonly bool IDataType.TruncationWillOccur(in Column _, bool _2) => false; 3662 readonly Result<uniqueidentifier, Bottom> ITryInto<uniqueidentifier, Bottom>.TryInto() => new(this); 3663 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3664 #endregion 3665 3666 #region Operators 3667 public static implicit operator uniqueidentifier(System.Guid val) => new(val); 3668 #endregion 3669 3670 #region Types 3671 #endregion 3672 } 3673 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 3674 public readonly struct varbinary: IDataType, IInto<varbinary>, IBinDeserializable<varbinary> { 3675 3676 #region Type-level Constructors 3677 #endregion 3678 3679 #region Instance Constructors 3680 public varbinary() => Value = null; 3681 internal varbinary(byte[] val) => Value = val; 3682 #endregion 3683 3684 #region Type-level Fields 3685 public static readonly varbinary NULL = new(); 3686 #endregion 3687 3688 #region Instance Fields 3689 [FieldOffset(0)] internal readonly byte[]? Value; 3690 #endregion 3691 3692 #region Type-level Properties 3693 #endregion 3694 3695 #region Instance Properties 3696 public readonly object Val => Value; 3697 public readonly bool IsNULL => Value is null; 3698 #endregion 3699 3700 #region Type-level Functions 3701 public static Result<varbinary, Error> De(ref Deserializer des) { 3702 3703 var des2 = des; 3704 var resUint = des2.DeserializeUint(); 3705 3706 if (resUint.IsOK) { 3707 var len = resUint.Unwrap(); 3708 var bytes = des2.Read(len); 3709 3710 if ((uint)bytes.Length == len) { 3711 des = des2; 3712 return new(new varbinary(bytes.ToArray())); 3713 } else { 3714 return new(Error.InvalidLength((ulong)bytes.Length, (@uint)len)); 3715 } 3716 } else { 3717 return new(resUint.UnwrapErr()); 3718 } 3719 } 3720 public static Result<varbinary, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3721 3722 var des2 = des; 3723 var resUint = des2.DeserializeUint(); 3724 3725 if (resUint.IsOK) { 3726 var len = resUint.Unwrap(); 3727 uint l; 3728 var bytes = new byte[len]; 3729 3730 if ((l = des2.Deserialize(bytes)) == len) { 3731 des = des2; 3732 return new(new varbinary(bytes)); 3733 } else { 3734 return new(Error.InvalidLength((ulong)l, (@uint)len)); 3735 } 3736 } else { 3737 return new(resUint.UnwrapErr()); 3738 } 3739 } 3740 public static varbinary New(byte[] val) => new(val); 3741 public static varbinary New(ReadOnlySpan<byte> val) => new(val.ToArray()); 3742 #endregion 3743 3744 #region Instance Functions 3745 public override readonly bool Equals(object? _) => false; 3746 public override readonly int GetHashCode() => 0; 3747 public readonly varbinary Into() => this; 3748 public readonly string IntoString() => ToString(); 3749 public readonly any IntoAny() => any.varbinary(this); 3750 readonly string IInto<string>.Into() => IntoString(); 3751 public readonly Unit Ser(ref Serializer ser) { 3752 3753 if (IsNULL) { 3754 return ser.SerByte((byte)any.Tag.NULL); 3755 } else { 3756 _ = ser.SerByte((byte)any.Tag.varbinary); 3757 _ = ser.SerUint((uint)Value!.Length); 3758 return ser.Ser(Value.AsSpan()); 3759 } 3760 } 3761 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3762 3763 if (IsNULL) { 3764 return ser.SerializeByte((byte)any.Tag.NULL); 3765 } else { 3766 var res = ser.SerializeByte((byte)any.Tag.varbinary); 3767 if (res.IsErr) { return res; } 3768 var res2 = ser.SerializeUint((uint)Value!.Length); 3769 return res2.IsErr ? res2 : ser.Serialize(Value.AsSpan()); 3770 } 3771 } 3772 public override readonly string ToString() => IsNULL ? "NULL" : Value.AsSpan().UpperHex(); 3773 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => col.MaxLength.IsSome && Value!.Length > col.MaxLength.Unwrap(); 3774 readonly Result<varbinary, Bottom> ITryInto<varbinary, Bottom>.TryInto() => new(this); 3775 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3776 #endregion 3777 3778 #region Operators 3779 #endregion 3780 3781 #region Types 3782 #endregion 3783 } 3784 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 3785 public readonly struct varchar: IDataType, IInto<varchar>, IBinDeserializable<varchar> { 3786 3787 #region Type-level Constructors 3788 #endregion 3789 3790 #region Instance Constructors 3791 public varchar() => Value = null; 3792 public varchar(string val) => Value = val; 3793 #endregion 3794 3795 #region Type-level Fields 3796 public static readonly varchar NULL = new(); 3797 #endregion 3798 3799 #region Instance Fields 3800 [FieldOffset(0)] internal readonly string? Value; 3801 #endregion 3802 3803 #region Type-level Properties 3804 #endregion 3805 3806 #region Instance Properties 3807 public readonly object Val => Value; 3808 public readonly bool IsNULL => Value is null; 3809 #endregion 3810 3811 #region Type-level Functions 3812 public static Result<varchar, Error> De(ref Deserializer des) { 3813 3814 var des2 = des; 3815 var resUint = des2.DeserializeUint(); 3816 3817 if (resUint.IsOK) { 3818 var len = resUint.Unwrap(); 3819 var chars = des2.DeserializeChars(len); 3820 3821 if ((uint)chars.Length == len) { 3822 des = des2; 3823 return new(new varchar(new string(chars))); 3824 } else { 3825 return new(Error.InvalidLength((ulong)chars.Length, (@uint)len)); 3826 } 3827 } else { 3828 return new(resUint.UnwrapErr()); 3829 } 3830 } 3831 public static Result<varchar, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3832 3833 var des2 = des; 3834 var resUint = des2.DeserializeUint(); 3835 3836 if (resUint.IsOK) { 3837 var len = resUint.Unwrap(); 3838 uint l; 3839 var chars = new char[len]; 3840 3841 if ((l = des2.DeserializeChars(chars)) == len) { 3842 des = des2; 3843 return new(new varchar(new string(chars))); 3844 } else { 3845 return new(Error.InvalidLength((ulong)l, (@uint)len)); 3846 } 3847 } else { 3848 return new(resUint.UnwrapErr()); 3849 } 3850 } 3851 public static varchar New(string val) => new(val); 3852 #endregion 3853 3854 #region Instance Functions 3855 public override readonly bool Equals(object? _) => false; 3856 public override readonly int GetHashCode() => 0; 3857 public readonly varchar Into() => this; 3858 public readonly string IntoString() => ToString(); 3859 public readonly any IntoAny() => any.varchar(this); 3860 readonly string IInto<string>.Into() => IntoString(); 3861 public readonly Unit Ser(ref Serializer ser) { 3862 3863 if (IsNULL) { 3864 return ser.SerByte((byte)any.Tag.NULL); 3865 } else { 3866 _ = ser.SerByte((byte)any.Tag.varchar); 3867 _ = ser.SerUint((uint)Value!.Length); 3868 return ser.SerChars(Value.AsSpan()); 3869 } 3870 } 3871 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3872 3873 if (IsNULL) { 3874 return ser.SerializeByte((byte)any.Tag.NULL); 3875 } else { 3876 var res = ser.SerializeByte((byte)any.Tag.varchar); 3877 if (res.IsErr) { return res; } 3878 var res2 = ser.SerializeUint((uint)Value!.Length); 3879 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 3880 } 3881 } 3882 public override readonly string ToString() => IsNULL ? "NULL" : Value; 3883 readonly bool IDataType.TruncationWillOccur(in Column col, bool _) => col.MaxLength.IsSome && Value!.Length > col.MaxLength.Unwrap(); 3884 readonly Result<varchar, Bottom> ITryInto<varchar, Bottom>.TryInto() => new(this); 3885 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3886 #endregion 3887 3888 #region Operators 3889 public static implicit operator varchar(string val) => new(val); 3890 #endregion 3891 3892 #region Types 3893 #endregion 3894 } 3895 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)] 3896 public readonly struct xml: IDataType, IInto<xml>, IBinDeserializable<xml> { 3897 3898 #region Type-level Constructors 3899 #endregion 3900 3901 #region Instance Constructors 3902 public xml() => Value = null; 3903 public xml(string val) => Value = val; 3904 #endregion 3905 3906 #region Type-level Fields 3907 public static readonly xml NULL = new(); 3908 #endregion 3909 3910 #region Instance Fields 3911 [FieldOffset(0)] internal readonly string? Value; 3912 #endregion 3913 3914 #region Type-level Properties 3915 #endregion 3916 3917 #region Instance Properties 3918 public readonly object Val => Value; 3919 public readonly bool IsNULL => Value is null; 3920 #endregion 3921 3922 #region Type-level Functions 3923 public static Result<xml, Error> De(ref Deserializer des) { 3924 3925 var des2 = des; 3926 var resUint = des2.DeserializeUint(); 3927 3928 if (resUint.IsOK) { 3929 var len = resUint.Unwrap(); 3930 var chars = des2.DeserializeChars(len); 3931 3932 if ((uint)chars.Length == len) { 3933 des = des2; 3934 return new(new xml(new string(chars))); 3935 } else { 3936 return new(Error.InvalidLength((ulong)chars.Length, (@uint)len)); 3937 } 3938 } else { 3939 return new(resUint.UnwrapErr()); 3940 } 3941 } 3942 public static Result<xml, Error> Deserialize<TDes>(ref TDes des) where TDes: notnull, IDeserializer { 3943 3944 var des2 = des; 3945 var resUint = des2.DeserializeUint(); 3946 3947 if (resUint.IsOK) { 3948 var len = resUint.Unwrap(); 3949 uint l; 3950 var chars = new char[len]; 3951 3952 if ((l = des2.DeserializeChars(chars)) == len) { 3953 des = des2; 3954 return new(new xml(new string(chars))); 3955 } else { 3956 return new(Error.InvalidLength((ulong)l, (@uint)len)); 3957 } 3958 } else { 3959 return new(resUint.UnwrapErr()); 3960 } 3961 } 3962 public static xml New(string val) => new(val); 3963 #endregion 3964 3965 #region Instance Functions 3966 public override readonly bool Equals(object? _) => false; 3967 public override readonly int GetHashCode() => 0; 3968 public readonly xml Into() => this; 3969 public readonly string IntoString() => ToString(); 3970 public readonly any IntoAny() => any.xml(this); 3971 readonly string IInto<string>.Into() => IntoString(); 3972 public readonly Unit Ser(ref Serializer ser) { 3973 3974 if (IsNULL) { 3975 return ser.SerByte((byte)any.Tag.NULL); 3976 } else { 3977 _ = ser.SerByte((byte)any.Tag.xml); 3978 _ = ser.SerUint((uint)Value!.Length); 3979 return ser.SerChars(Value.AsSpan()); 3980 } 3981 } 3982 public readonly Result<TOK, TErr> Serialize<TSer, TOK, TErr>(ref TSer ser) where TOK: notnull where TErr: notnull, IError where TSer: notnull, ISerializer<TOK, TErr> { 3983 3984 if (IsNULL) { 3985 return ser.SerializeByte((byte)any.Tag.NULL); 3986 } else { 3987 var res = ser.SerializeByte((byte)any.Tag.xml); 3988 if (res.IsErr) { return res; } 3989 var res2 = ser.SerializeUint((uint)Value!.Length); 3990 return res2.IsErr ? res2 : ser.SerializeChars(Value.AsSpan()); 3991 } 3992 } 3993 public override readonly string ToString() => IsNULL ? "NULL" : Value; 3994 readonly bool IDataType.TruncationWillOccur(in Column _, bool _1) => false; 3995 readonly Result<xml, Bottom> ITryInto<xml, Bottom>.TryInto() => new(this); 3996 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 3997 #endregion 3998 3999 #region Operators 4000 public static implicit operator xml(string val) => new(val); 4001 #endregion 4002 4003 #region Types 4004 #endregion 4005 } 4006 [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 1, Size = 1)] 4007 public readonly struct OutOfRangeErr: IClone<OutOfRangeErr>, IEquality<OutOfRangeErr>, IError, IHashable, IInto<OutOfRangeErr> { 4008 4009 #region Type-level Constructors 4010 #endregion 4011 4012 #region Instance Constructors 4013 public OutOfRangeErr() { } 4014 #endregion 4015 4016 #region Type-level Fields 4017 public static readonly OutOfRangeErr OutOfRange = new(); 4018 #endregion 4019 4020 #region Instance Fields 4021 #endregion 4022 4023 #region Type-level Properties 4024 #endregion 4025 4026 #region Instance Properties 4027 #endregion 4028 4029 #region Type-level Functions 4030 #endregion 4031 4032 #region Instance Functions 4033 public readonly OutOfRangeErr Clone() => this; 4034 public readonly bool Equals(in OutOfRangeErr _) => true; 4035 public override readonly bool Equals(object? obj) => obj is OutOfRangeErr val; 4036 public override readonly int GetHashCode() => 0; 4037 public readonly Unit Hash<THasher>(ref THasher hasher) where THasher: notnull, IHasher => new Unit(); 4038 public readonly OutOfRangeErr Into() => this; 4039 public readonly string IntoString() => ToString(); 4040 readonly string IInto<string>.Into() => IntoString(); 4041 public readonly Maybe<IError> Source() => Maybe<IError>.None(); 4042 public readonly Maybe<StackTrace> StackTrace() => Maybe<StackTrace>.None(); 4043 public override readonly string ToString() => "OutOfRange"; 4044 readonly Result<OutOfRangeErr, Bottom> ITryInto<OutOfRangeErr, Bottom>.TryInto() => new(this); 4045 readonly Result<string, Bottom> ITryInto<string, Bottom>.TryInto() => new(ToString()); 4046 #endregion 4047 4048 #region Operators 4049 public static bool operator !=(OutOfRangeErr _, OutOfRangeErr _1) => false; 4050 public static bool operator ==(OutOfRangeErr _, OutOfRangeErr _1) => true; 4051 #endregion 4052 4053 #region Types 4054 #endregion 4055 } 4056 public ref struct anyIter { 4057 4058 #region Type-level Constructors 4059 #endregion 4060 4061 #region Instance Constructors 4062 public anyIter() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!"); 4063 anyIter(Deserializer des, uint arity) { 4064 4065 _des = des; 4066 _arity = arity; 4067 _index = uint.MinValue; 4068 _err = Maybe<Error>.None(); 4069 } 4070 #endregion 4071 4072 #region Type-level Fields 4073 #endregion 4074 4075 #region Instance Fields 4076 Deserializer _des; 4077 readonly uint _arity; 4078 uint _index; 4079 [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0032:Use auto property", Justification = "Prefer fields.")] 4080 Maybe<Error> _err; 4081 #endregion 4082 4083 #region Type-level Properties 4084 #endregion 4085 4086 #region Instance Properties 4087 public readonly NonZeroUshort Arity => NonZeroUshort.New((ushort)_arity).Unwrap(); 4088 public readonly Deserializer Deserializer => _des; 4089 public readonly Maybe<Error> Error => _err; 4090 #endregion 4091 4092 #region Type-level Functions 4093 // RefProds serialize one less than their arity—which is restricted to 1–256—followed by sequentially serializing their underlying 4094 // elements by calling each elements Deserialize function. 4095 internal static anyIter New(Deserializer des, out Maybe<Error> err) { 4096 4097 var resArity = des.DeserializeByte(); 4098 err = resArity.Err(); 4099 return resArity.IsOK ? new(des, resArity.Unwrap() + 1u) : default; 4100 } 4101 #endregion 4102 4103 #region Instance Functions 4104 public override readonly bool Equals(object? _) => false; 4105 public override readonly int GetHashCode() => 0; 4106 public Maybe<Result<any, Error>> Next() { 4107 4108 if (_index == _arity) { 4109 return Maybe<Result<any, Error>>.None(); 4110 } else if (_err.IsSome) { 4111 return new(new(_err.Unwrap())); 4112 } else { 4113 var resAny = any.De(ref _des); 4114 4115 if (resAny.IsOK) { 4116 _index++; 4117 } else { 4118 _err = resAny.Err(); 4119 } 4120 return new(resAny); 4121 } 4122 } 4123 public override readonly string ToString() => string.Empty; 4124 #endregion 4125 4126 #region Operators 4127 #endregion 4128 4129 #region Types 4130 #endregion 4131 } 4132 #endregion 4133 4134 #region Namespaces 4135 #endregion 4136 } 4137 #endregion