./ct_report/coverage/mongoose_record_pp.COVER.html

1 %% Pretty prints a record including field names.
2 %% It's useful to print records with many fields.
3 -module(mongoose_record_pp).
4 -export([format/3]).
5
6 %% Takes a record tuple, record name, and record_info(fields, Name)
7 %% Returns `#state{field1 = Value, field2 = Value2}'.
8 %% usage:
9 %% `mongoose_record_pp:format(State, state, record_info(fields, state))'
10 -spec format(tuple(), atom(), [atom()]) -> binary().
11 format(Record, Name, FieldDefs) when element(1, Record) =:= Name ->
12
:-(
FieldVals = tl(tuple_to_list(Record)),
13
:-(
FieldKV = print_pairs(FieldVals, FieldDefs),
14
:-(
iolist_to_binary(["#", atom_to_list(Name), "{", FieldKV, "}"]);
15 format(Other, _, _) ->
16
:-(
iolist_to_binary(io_lib:format("~500p", [Other])).
17
18 print_pairs([Val], [Def]) ->
19 %% trailing comma
20
:-(
[pair(Val, Def)];
21 print_pairs([Val | VRest], [Def | DRest]) ->
22
:-(
[pair(Val, Def), ", " | print_pairs(VRest, DRest)];
23
:-(
print_pairs([], []) -> [].
24
25 pair(Val, Def) ->
26
:-(
io_lib:format("~p = ~500p", [Def, Val]).
Line Hits Source