1 module xserial.attribute; 2 3 import std.system : Endian; 4 import std.traits : isIntegral; 5 6 import xbuffer.varint : isVar; 7 8 import xserial.serial : EndianType; 9 10 /** 11 * Excludes the field from both encoding and decoding. 12 */ 13 enum Exclude; 14 15 /** 16 * Excludes the field from decoding, encode only. 17 */ 18 enum EncodeOnly; 19 20 /** 21 * Excludes the field from encoding, decode only. 22 */ 23 enum DecodeOnly; 24 25 /** 26 * Only encode/decode the field when the condition is met. 27 * The condition is placed inside an if statement and can access 28 * the variables and functions of the class/struct (without `this`). 29 * 30 * This attribute can be used with EncodeOnly and DecodeOnly. 31 */ 32 struct Condition { string condition; } 33 34 /** 35 * Indicates the endianness for the type and its subtypes. 36 */ 37 enum BigEndian; 38 39 /// ditto 40 enum LittleEndian; 41 42 /** 43 * Encodes and decodes as a Google varint. 44 */ 45 enum Var; 46 47 /** 48 * Indicates that the array has no length. It should only be used 49 * as last field in the class/struct. 50 */ 51 enum NoLength; 52 53 struct LengthImpl { string type; int endianness; } 54 55 template Length(T) if(isIntegral!T) { enum Length = LengthImpl(T.stringof, -1); } 56 57 template Length(T) if(isVar!T) { enum Length = LengthImpl(T.Base.stringof, EndianType.var); } 58 59 LengthImpl EndianLength(T)(Endian endianness) if(isIntegral!T) { return LengthImpl(T.stringof, endianness); } 60 61 struct Custom(T) if(is(T == struct) || is(T == class)) { alias C = T; } 62 63 unittest { // for code coverage 64 65 EndianLength!uint(Endian.bigEndian); 66 67 }