@@ -20,8 +20,11 @@ const little_endian = switch (current_zig.minor) {
2020 else = > @compileError ("not support current version zig" ),
2121};
2222
23+ /// the Str Type
2324pub const Str = struct {
2425 str : []const u8 ,
26+
27+ /// get Str values
2528 pub fn value (self : Str ) []const u8 {
2629 return self .str ;
2730 }
@@ -32,8 +35,11 @@ pub fn wrapStr(str: []const u8) Str {
3235 return Str { .str = str };
3336}
3437
38+ /// the Bin Type
3539pub const Bin = struct {
3640 bin : []u8 ,
41+
42+ /// get bin values
3743 pub fn value (self : Bin ) []u8 {
3844 return self .bin ;
3945 }
@@ -44,6 +50,7 @@ pub fn wrapBin(bin: []u8) Bin {
4450 return Bin { .bin = bin };
4551}
4652
53+ /// the EXT Type
4754pub const EXT = struct {
4855 type : i8 ,
4956 data : []u8 ,
@@ -57,10 +64,14 @@ pub fn wrapEXT(t: i8, data: []u8) EXT {
5764 };
5865}
5966
60- // the map of payload
67+ /// the map of payload
6168pub const Map = std .StringHashMap (Payload );
6269
70+ /// Entity to store msgpack
71+ ///
72+ /// Note: The payload and its subvalues must have the same allocator
6373pub const Payload = union (enum ) {
74+ /// the error for Payload
6475 pub const Errors = error {
6576 NotMap ,
6677 };
@@ -76,6 +87,7 @@ pub const Payload = union(enum) {
7687 map : Map ,
7788 ext : EXT ,
7889
90+ /// put a new element to map payload
7991 pub fn mapPut (self : * Payload , key : []const u8 , val : Payload ) ! void {
8092 if (self .* != .map ) {
8193 return Errors .NotMap ;
@@ -85,36 +97,42 @@ pub const Payload = union(enum) {
8597 try self .map .put (new_key , val );
8698 }
8799
100+ /// get a NIL payload
88101 pub fn nilToPayload () Payload {
89102 return Payload {
90103 .nil = void {},
91104 };
92105 }
93106
107+ /// get a bool payload
94108 pub fn boolToPayload (val : bool ) Payload {
95109 return Payload {
96110 .bool = val ,
97111 };
98112 }
99113
114+ /// get a int payload
100115 pub fn intToPayload (val : i64 ) Payload {
101116 return Payload {
102117 .int = val ,
103118 };
104119 }
105120
121+ /// get a uint payload
106122 pub fn uintToPayload (val : u64 ) Payload {
107123 return Payload {
108124 .uint = val ,
109125 };
110126 }
111127
128+ /// get a float payload
112129 pub fn floatToPayload (val : f64 ) Payload {
113130 return Payload {
114131 .float = val ,
115132 };
116133 }
117134
135+ /// get a str payload
118136 pub fn strToPayload (val : []const u8 , allocator : Allocator ) ! Payload {
119137 // alloca memory
120138 const new_str = try allocator .alloc (u8 , val .len );
@@ -125,6 +143,7 @@ pub const Payload = union(enum) {
125143 };
126144 }
127145
146+ /// get a bin payload
128147 pub fn binToPayload (val : []const u8 , allocator : Allocator ) ! Payload {
129148 // alloca memory
130149 const new_bin = try allocator .alloc (u8 , val .len );
@@ -135,21 +154,22 @@ pub const Payload = union(enum) {
135154 };
136155 }
137156
157+ /// get an array payload
138158 pub fn arrPayload (len : usize , allocator : Allocator ) ! Payload {
139159 const arr = try allocator .alloc (Payload , len );
140160 return Payload {
141161 .arr = arr ,
142162 };
143163 }
144164
165+ /// get a map payload
145166 pub fn mapPayload (allocator : Allocator ) Payload {
146167 return Payload {
147168 .map = Map .init (allocator ),
148169 };
149170 }
150171
151- // TODO: add map support
152-
172+ /// get an ext payload
153173 pub fn extToPayload (t : i8 , data : []const u8 , allocator : Allocator ) ! Payload {
154174 // alloca memory
155175 const new_data = try allocator .alloc (u8 , data .len );
@@ -160,6 +180,8 @@ pub const Payload = union(enum) {
160180 };
161181 }
162182
183+ /// free the all memeory for this payload and sub payloads
184+ /// the allocator is payload's allocator
163185 pub fn free (self : * Payload , allocator : Allocator ) void {
164186 switch (self .* ) {
165187 .str = > {
@@ -240,7 +262,7 @@ const Markers = enum(u8) {
240262 NEGATIVE_FIXINT = 0xe0 ,
241263};
242264
243- /// error set
265+ /// A collection of errors that may occur when reading the payload
244266pub const MsGPackError = error {
245267 STR_DATA_LENGTH_TOO_LONG ,
246268 BIN_DATA_LENGTH_TOO_LONG ,
@@ -262,7 +284,7 @@ pub const MsGPackError = error{
262284 INTERNAL ,
263285};
264286
265- /// main function
287+ /// Create an instance of msgpack_pack
266288pub fn Pack (
267289 comptime WriteContext : type ,
268290 comptime ReadContext : type ,
@@ -728,7 +750,6 @@ pub fn Pack(
728750 try self .writeExtValue (ext );
729751 }
730752
731- /// write EXT
732753 fn writeExt (self : Self , ext : EXT ) ! void {
733754 const len = ext .data .len ;
734755 if (len == 1 ) {
@@ -752,6 +773,7 @@ pub fn Pack(
752773 }
753774 }
754775
776+ /// write payload
755777 pub fn write (self : Self , payload : Payload ) ! void {
756778 switch (payload ) {
757779 .nil = > {
@@ -817,13 +839,10 @@ pub fn Pack(
817839
818840 // TODO: add timestamp
819841
820- //// read
821-
822842 fn readFrom (self : Self , bytes : []u8 ) ! usize {
823843 return readFn (self .read_context , bytes );
824844 }
825845
826- /// read one byte
827846 fn readByte (self : Self ) ! u8 {
828847 var res = [1 ]u8 {0 };
829848 const len = try self .readFrom (& res );
@@ -835,7 +854,6 @@ pub fn Pack(
835854 return res [0 ];
836855 }
837856
838- /// read data
839857 fn readData (self : Self , allocator : Allocator , len : usize ) ! []u8 {
840858 const data = try allocator .alloc (u8 , len );
841859 errdefer allocator .free (data );
@@ -848,13 +866,11 @@ pub fn Pack(
848866 return data ;
849867 }
850868
851- /// read type marker u8
852869 fn readTypeMarkerU8 (self : Self ) ! u8 {
853870 const val = try self .readByte ();
854871 return val ;
855872 }
856873
857- /// convert marker u8 to marker
858874 fn markerU8To (_ : Self , marker_u8 : u8 ) Markers {
859875 var val = marker_u8 ;
860876
@@ -873,7 +889,6 @@ pub fn Pack(
873889 return @enumFromInt (val );
874890 }
875891
876- /// read type marker
877892 fn readTypeMarker (self : Self ) ! Markers {
878893 const val = try self .readTypeMarkerU8 ();
879894 return self .markerU8To (val );
@@ -887,13 +902,11 @@ pub fn Pack(
887902 }
888903 }
889904
890- /// read bool
891905 fn readBool (self : Self ) ! bool {
892906 const marker = try self .readTypeMarker ();
893907 return self .readBoolValue (marker );
894908 }
895909
896- /// read positive and negative fixint
897910 fn readFixintValue (_ : Self , marker_u8 : u8 ) i8 {
898911 return @bitCast (marker_u8 );
899912 }
@@ -1228,6 +1241,7 @@ pub fn Pack(
12281241 }
12291242 }
12301243
1244+ /// read a payload, please use payload.free to free the memory
12311245 pub fn read (self : Self , allocator : Allocator ) ! Payload {
12321246 var res : Payload = undefined ;
12331247
0 commit comments