11const zigplug = @import ("root.zig" );
22
33const std = @import ("std" );
4- const msgpack = @import ("msgpack " );
4+ const bufzilla = @import ("bufzilla " );
55
66pub const State = struct {
77 context : * anyopaque ,
@@ -10,57 +10,64 @@ pub const State = struct {
1010 allocator : std.mem.Allocator ,
1111
1212 pub fn serialize (self : * const State , writer : * std.Io.Writer ) ! void {
13- var reader = std .Io .Reader . failing ;
14- var packer = msgpack . packIO ( & reader , writer );
13+ var aw = std .Io .Writer . Allocating . init ( self . allocator ) ;
14+ defer aw . deinit ( );
1515
16- var map = msgpack .Payload .mapPayload (self .allocator );
17- defer map .free (self .allocator );
16+ var w = bufzilla .Writer .init (& aw .writer );
1817
19- for (self .slice ) | parameter | {
20- const id = switch (parameter .* ) {
21- inline else = > | * p | p .options .id .? ,
22- };
23-
24- try map .mapPut (id , switch (parameter .* ) {
25- .bool = > | p | .boolToPayload (p .get ()),
26- .float = > | p | .floatToPayload (p .get ()),
27- .int = > | p | .intToPayload (p .get ()),
28- .uint = > | p | .uintToPayload (p .get ()),
29- });
18+ try w .startObject ();
3019
20+ for (self .slice ) | parameter |
3121 switch (parameter .* ) {
32- inline else = > | p | self .log .debug ("saved parameter '{s}' = {any}" , .{ id , p .get () }),
33- }
34- }
22+ inline else = > | * p | {
23+ const id = p .options .id .? ;
24+ const value = p .get ();
25+
26+ try w .writeAny (id );
27+ try w .writeAnyExplicit (@TypeOf (value ), value );
3528
36- try packer .write (map );
29+ self .log .debug ("saved parameter '{s}' = {any}" , .{ id , value });
30+ },
31+ };
32+
33+ try w .endContainer ();
34+
35+ const bytes = aw .written ();
36+ self .log .debug ("saving encoded state: {s}" , .{bytes });
37+ try writer .writeAll (bytes );
38+ try writer .flush ();
3739 }
3840
3941 pub fn deserialize (self : * State , reader : * std.Io.Reader ) ! void {
40- var writer = std .Io .Writer .failing ;
41- var packer = msgpack . packIO ( reader , & writer );
42+ var aw = std .Io .Writer .Allocating . init ( self . allocator ) ;
43+ defer aw . deinit ( );
4244
43- const decoded = try packer .read (self .allocator );
44- defer decoded .free (self .allocator );
45+ _ = try reader .streamRemaining (& aw .writer );
4546
46- for (self .slice ) | parameter | {
47- const id = switch (parameter .* ) {
48- inline else = > | * p | p .options .id .? ,
49- };
47+ const bytes = aw .written ();
48+ self .log .debug ("reading encoded state: {s}" , .{bytes });
5049
51- if (try decoded .mapGet (id )) | value | {
52- switch (parameter .* ) {
53- .bool = > | * p | p .set (value .bool ),
54- .float = > | * p | p .set (value .float ),
55- .int = > | * p | p .set (value .int ),
56- .uint = > | * p | p .set (value .uint ),
57- }
58- }
50+ var r = bufzilla .Reader (.{}).init (bytes );
5951
52+ for (self .slice ) | parameter |
6053 switch (parameter .* ) {
61- inline else = > | p | self .log .debug ("read parameter '{s}' = {any}" , .{ id , p .get () }),
62- }
63- }
54+ inline else = > | * p | {
55+ const id = p .options .id .? ;
56+ const decoded_value = try r .readPath (id );
57+ if (decoded_value ) | value | {
58+ switch (value ) {
59+ @TypeOf (p .* ).param_type .bufzillaValueTag () = > | v | {
60+ p .set (v );
61+ self .log .debug ("read parameter '{s}' = {any}" , .{ id , v });
62+ },
63+ else = > self .log .warn (
64+ "wrong type for parameter '{s}': expected {s}, got {s}" ,
65+ .{ id , @tagName (@TypeOf (p .* ).param_type ), @tagName (value ) },
66+ ),
67+ }
68+ } else self .log .warn ("did not find parameter '{s}'" , .{id });
69+ },
70+ };
6471 }
6572};
6673
@@ -98,7 +105,21 @@ pub fn Options(comptime T: type) type {
98105 };
99106}
100107
101- const ParameterType = enum { float , int , uint , bool };
108+ const ParameterType = enum {
109+ float ,
110+ int ,
111+ uint ,
112+ bool ,
113+
114+ pub fn bufzillaValueTag (self : ParameterType ) @typeInfo (bufzilla .Value ).@"union" .tag_type .? {
115+ return switch (self ) {
116+ .float = > .f64 ,
117+ .int = > .i64 ,
118+ .uint = > .u64 ,
119+ .bool = > .bool ,
120+ };
121+ }
122+ };
102123
103124pub const Parameter = union (ParameterType ) {
104125 fn Inner (comptime T : type ) type {
0 commit comments