encoding/cbor: rename diagnose to to_diagnostic_format to be clearer

This commit is contained in:
Laytan Laats
2024-03-04 17:26:19 +01:00
parent 9fc8587e2c
commit 04bd3cc525
3 changed files with 21 additions and 21 deletions
+12 -12
View File
@@ -307,23 +307,23 @@ destroy :: proc(val: Value, allocator := context.allocator) {
} }
/* /*
diagnose either writes or returns a human-readable representation of the value, to_diagnostic_format either writes or returns a human-readable representation of the value,
optionally formatted, defined as the diagnostic format in section 8 of RFC 8949. optionally formatted, defined as the diagnostic format in [[RFC 8949 Section 8;https://www.rfc-editor.org/rfc/rfc8949.html#name-diagnostic-notation]].
Incidentally, if the CBOR does not contain any of the additional types defined on top of JSON Incidentally, if the CBOR does not contain any of the additional types defined on top of JSON
this will also be valid JSON. this will also be valid JSON.
*/ */
diagnose :: proc { to_diagnostic_format :: proc {
diagnostic_string, to_diagnostic_format_string,
diagnose_to_writer, to_diagnostic_format_writer,
} }
// Turns the given CBOR value into a human-readable string. // Turns the given CBOR value into a human-readable string.
// See docs on the proc group `diagnose` for more info. // See docs on the proc group `diagnose` for more info.
diagnostic_string :: proc(val: Value, padding := 0, allocator := context.allocator) -> (string, mem.Allocator_Error) #optional_allocator_error { to_diagnostic_format_string :: proc(val: Value, padding := 0, allocator := context.allocator) -> (string, mem.Allocator_Error) #optional_allocator_error {
b := strings.builder_make(allocator) b := strings.builder_make(allocator)
w := strings.to_stream(&b) w := strings.to_stream(&b)
err := diagnose_to_writer(w, val, padding) err := to_diagnostic_format_writer(w, val, padding)
if err == .EOF { if err == .EOF {
// The string builder stream only returns .EOF, and only if it can't write (out of memory). // The string builder stream only returns .EOF, and only if it can't write (out of memory).
return "", .Out_Of_Memory return "", .Out_Of_Memory
@@ -335,7 +335,7 @@ diagnostic_string :: proc(val: Value, padding := 0, allocator := context.allocat
// Writes the given CBOR value into the writer as human-readable text. // Writes the given CBOR value into the writer as human-readable text.
// See docs on the proc group `diagnose` for more info. // See docs on the proc group `diagnose` for more info.
diagnose_to_writer :: proc(w: io.Writer, val: Value, padding := 0) -> io.Error { to_diagnostic_format_writer :: proc(w: io.Writer, val: Value, padding := 0) -> io.Error {
@(require_results) @(require_results)
indent :: proc(padding: int) -> int { indent :: proc(padding: int) -> int {
padding := padding padding := padding
@@ -421,7 +421,7 @@ diagnose_to_writer :: proc(w: io.Writer, val: Value, padding := 0) -> io.Error {
newline(w, padding) or_return newline(w, padding) or_return
for entry, i in v { for entry, i in v {
diagnose(w, entry, padding) or_return to_diagnostic_format(w, entry, padding) or_return
if i != len(v)-1 { if i != len(v)-1 {
comma(w, padding) or_return comma(w, padding) or_return
newline(w, padding) or_return newline(w, padding) or_return
@@ -444,9 +444,9 @@ diagnose_to_writer :: proc(w: io.Writer, val: Value, padding := 0) -> io.Error {
newline(w, padding) or_return newline(w, padding) or_return
for entry, i in v { for entry, i in v {
diagnose(w, entry.key, padding) or_return to_diagnostic_format(w, entry.key, padding) or_return
io.write_string(w, ": ") or_return io.write_string(w, ": ") or_return
diagnose(w, entry.value, padding) or_return to_diagnostic_format(w, entry.value, padding) or_return
if i != len(v)-1 { if i != len(v)-1 {
comma(w, padding) or_return comma(w, padding) or_return
newline(w, padding) or_return newline(w, padding) or_return
@@ -460,7 +460,7 @@ diagnose_to_writer :: proc(w: io.Writer, val: Value, padding := 0) -> io.Error {
case ^Tag: case ^Tag:
io.write_u64(w, v.number) or_return io.write_u64(w, v.number) or_return
io.write_string(w, "(") or_return io.write_string(w, "(") or_return
diagnose(w, v.value, padding) or_return to_diagnostic_format(w, v.value, padding) or_return
io.write_string(w, ")") or_return io.write_string(w, ")") or_return
case Simple: case Simple:
io.write_string(w, "simple(") or_return io.write_string(w, "simple(") or_return
+2 -2
View File
@@ -117,8 +117,8 @@ Example:
assert(derr == nil) assert(derr == nil)
defer cbor.destroy(decoded) defer cbor.destroy(decoded)
// Turn the CBOR into a human readable representation. // Turn the CBOR into a human readable representation defined as the diagnostic format in [[RFC 8949 Section 8;https://www.rfc-editor.org/rfc/rfc8949.html#name-diagnostic-notation]].
diagnosis, eerr := cbor.diagnose(decoded) diagnosis, eerr := cbor.to_diagnostic_format(decoded)
assert(eerr == nil) assert(eerr == nil)
defer delete(diagnosis) defer delete(diagnosis)
+7 -7
View File
@@ -212,7 +212,7 @@ test_marshalling :: proc(t: ^testing.T) {
ev(t, derr, nil) ev(t, derr, nil)
defer cbor.destroy(decoded) defer cbor.destroy(decoded)
diagnosis, eerr := cbor.diagnose(decoded) diagnosis, eerr := cbor.to_diagnostic_format(decoded)
ev(t, eerr, nil) ev(t, eerr, nil)
defer delete(diagnosis) defer delete(diagnosis)
@@ -379,7 +379,7 @@ test_marshalling_maybe :: proc(t: ^testing.T) {
val, derr := cbor.decode(string(data)) val, derr := cbor.decode(string(data))
expect_value(t, derr, nil) expect_value(t, derr, nil)
expect_value(t, cbor.diagnose(val), "1") expect_value(t, cbor.to_diagnostic_format(val), "1")
maybe_dest: Maybe(int) maybe_dest: Maybe(int)
uerr := cbor.unmarshal(string(data), &maybe_dest) uerr := cbor.unmarshal(string(data), &maybe_dest)
@@ -396,7 +396,7 @@ test_marshalling_nil_maybe :: proc(t: ^testing.T) {
val, derr := cbor.decode(string(data)) val, derr := cbor.decode(string(data))
expect_value(t, derr, nil) expect_value(t, derr, nil)
expect_value(t, cbor.diagnose(val), "nil") expect_value(t, cbor.to_diagnostic_format(val), "nil")
maybe_dest: Maybe(int) maybe_dest: Maybe(int)
uerr := cbor.unmarshal(string(data), &maybe_dest) uerr := cbor.unmarshal(string(data), &maybe_dest)
@@ -432,7 +432,7 @@ test_marshalling_union :: proc(t: ^testing.T) {
val, derr := cbor.decode(string(data)) val, derr := cbor.decode(string(data))
expect_value(t, derr, nil) expect_value(t, derr, nil)
expect_value(t, cbor.diagnose(val, -1), `1010(["My_Distinct", "Hello, World!"])`) expect_value(t, cbor.to_diagnostic_format(val, -1), `1010(["My_Distinct", "Hello, World!"])`)
dest: My_Union dest: My_Union
uerr := cbor.unmarshal(string(data), &dest) uerr := cbor.unmarshal(string(data), &dest)
@@ -455,7 +455,7 @@ test_marshalling_union :: proc(t: ^testing.T) {
val, derr := cbor.decode(string(data)) val, derr := cbor.decode(string(data))
expect_value(t, derr, nil) expect_value(t, derr, nil)
expect_value(t, cbor.diagnose(val, -1), `1010(["My_Struct", {"my_enum": 1}])`) expect_value(t, cbor.to_diagnostic_format(val, -1), `1010(["My_Struct", {"my_enum": 1}])`)
dest: My_Union_No_Nil dest: My_Union_No_Nil
uerr := cbor.unmarshal(string(data), &dest) uerr := cbor.unmarshal(string(data), &dest)
@@ -810,7 +810,7 @@ expect_decoding :: proc(t: ^testing.T, encoded: string, decoded: string, type: t
expect_value(t, reflect.union_variant_typeid(res), type, loc) expect_value(t, reflect.union_variant_typeid(res), type, loc)
expect_value(t, err, nil, loc) expect_value(t, err, nil, loc)
str := cbor.diagnose(res, padding=-1) str := cbor.to_diagnostic_format(res, padding=-1)
defer delete(str) defer delete(str)
expect_value(t, str, decoded, loc) expect_value(t, str, decoded, loc)
@@ -825,7 +825,7 @@ expect_tag :: proc(t: ^testing.T, encoded: string, nr: cbor.Tag_Number, value_de
if tag, is_tag := res.(^cbor.Tag); is_tag { if tag, is_tag := res.(^cbor.Tag); is_tag {
expect_value(t, tag.number, nr, loc) expect_value(t, tag.number, nr, loc)
str := cbor.diagnose(tag, padding=-1) str := cbor.to_diagnostic_format(tag, padding=-1)
defer delete(str) defer delete(str)
expect_value(t, str, value_decoded, loc) expect_value(t, str, value_decoded, loc)