package msgpack import "core:fmt" import "core:strings" import "core:testing" Dump_Options :: struct { max_depth: int, // 0 = unlimited max_items: int, // 0 = unlimited max_string: int, // 0 = unlimited } DUMP_DEFAULT :: Dump_Options { max_depth = 8, max_items = 16, max_string = 256, } dump :: proc { dump_object, dump_bytes, } dump_object :: proc(object: Object, opts := DUMP_DEFAULT, allocator := context.temp_allocator) -> string { b: strings.Builder strings.builder_init(&b, allocator) write_object(&b, object, opts, 0) return strings.to_string(b) } dump_bytes :: proc(data: []byte, opts := DUMP_DEFAULT, allocator := context.temp_allocator) -> string { d: Decoder object: Object rest := data done, err := decode_object(&d, &rest, &object, allocator) s := dump_object(object, opts, allocator) switch { case err != .None: return fmt.aprintf("{} ", s, err, len(data) - len(rest), allocator = allocator) case !done: return fmt.aprintf("{} ", s, allocator = allocator) case len(rest) > 0: return fmt.aprintf("{} <{} trailing bytes>", s, len(rest), allocator = allocator) } return s } @(private = "file") write_object :: proc(b: ^strings.Builder, object: Object, opts: Dump_Options, depth: int) { if opts.max_depth > 0 && depth >= opts.max_depth { strings.write_string(b, "…") return } switch obj in object { case Nil: strings.write_string(b, "nil") case bool: strings.write_string(b, "true" if obj else "false") case i64: fmt.sbprintf(b, "{}", obj) case u64: fmt.sbprintf(b, "{}", obj) case f32: fmt.sbprintf(b, "{}", obj) case f64: fmt.sbprintf(b, "{}", obj) case string: dump_string(b, obj, opts.max_string) case Bin: strings.write_string(b, "bin(0x") dump_hex(b, ([]byte)(obj), opts.max_string) strings.write_byte(b, ')') case Ext: fmt.sbprintf(b, "ext({}, 0x", obj.type) dump_hex(b, obj.data, opts.max_string) strings.write_byte(b, ')') case Array: strings.write_byte(b, '[') shown := shown_item(len(obj), opts) for e, i in obj[:shown] { if i > 0 {strings.write_string(b, ", ")} write_object(b, e, opts, depth + 1) } write_more(b, len(obj), shown) strings.write_byte(b, ']') case Map: strings.write_byte(b, '{') shown := shown_item(len(obj), opts) for p, i in obj[:shown] { if i > 0 {strings.write_string(b, ", ")} write_object(b, p.key, opts, depth + 1) strings.write_string(b, ": ") write_object(b, p.value, opts, depth + 1) } write_more(b, len(obj), shown) strings.write_byte(b, '}') case: strings.write_string(b, "") } } @(private = "file") shown_item :: proc(n: int, opts: Dump_Options) -> int { return min(n, opts.max_items) if opts.max_items > 0 else n } @(private = "file") write_more :: proc(b: ^strings.Builder, n, shown: int) { if shown == n {return} if shown > 0 {strings.write_string(b, ", ")} fmt.sbprintf(b, "… {} more", n - shown) } @(private = "file") dump_string :: proc(b: ^strings.Builder, s: string, max_len: int) { if max_len > 0 && len(s) > max_len { fmt.sbprintf(b, "%q", s[:max_len]) fmt.sbprintf(b, "…+{}", len(s) - max_len) } else { fmt.sbprintf(b, "%q", s) } } @(private = "file") dump_hex :: proc(b: ^strings.Builder, data: []byte, max_len: int) { hex := "0123456789abcdef" shown := len(data) if max_len > 0 && shown > max_len {shown = max_len} for i in 0 ..< shown { strings.write_byte(b, hex[data[i] >> 4]) strings.write_byte(b, hex[data[i] & 0xf]) } if shown < len(data) { fmt.sbprintf(b, "...+{}", len(data) - shown) } } @(test) test_dump :: proc(t: ^testing.T) { opts := Dump_Options { max_depth = 2, max_items = 2, max_string = 3, } v := Array{Nil{}, "abcdef", Array{Array{i64(1)}}, Map{{"k", true}}} testing.expect_value(t, dump(v, opts), `[nil, "abc"…+3, … 2 more]`) testing.expect_value(t, dump(Object(Array{Array{Array{}}, Bin{0xab}}), opts), `[[…], bin(0xab)]`) testing.expect_value(t, dump([]byte{0x92, 0x01}), `[1, ] `) testing.expect_value(t, dump([]byte{0x91, 0xc1}), `[] `) testing.expect_value(t, dump([]byte{0x01, 0x02}), `1 <1 trailing bytes>`) }