### Trace Identifiers ### ### use std::type; use std::binary; use std::array; use std::string; ## Is the `trace_id` valid ## ## Checks the `trace_id` argument to see if it is a valid ## trace id. A legal trace id is one of: ## ## * An array of integers in the range of [0..=255] of length 8 ## * A binary 16 byte value ## * A 32-byte hex-encoded string ## * An array of 16 int values ## * Regardless of representation, the value must not be all zeroes ## ## Returns a record when the representation is well-formed of the form: ## ## > ```tremor ## > { ## > "kind": "string"|"binary"|"array", # Depends on input ## > "valid": true|false, # True if well-formed and valid ## > "value": "" # Representation depends on `kind` ## > } ## > ``` ## ## Returns an empty record `{}` when the representation not well-formed ## fn is_valid(trace_id) of # String representation case(trace_id) when type::is_string(trace_id) => { "kind": "string", "valid": trace_id != "00000000000000000000000000000000" and string::bytes(trace_id) == 32, "value": trace_id } # Binary representation case(trace_id) when type::is_binary(trace_id) => let arr = binary::into_bytes(trace_id); { "kind": "binary", "valid": binary::len(arr) == 16 and trace_id != << 0:64, 0:64 >>, "value": trace_id } # Array representation case(trace_id) when type::is_array(trace_id) => { "kind": "array", "valid": array::len(arr) == 16 and trace_id != [ 0, 0, 0, 0, 0, 0, 0, 0], "value": trace_id } case _ => false end