-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathelvis_result.erl
162 lines (138 loc) · 3.92 KB
/
elvis_result.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-module(elvis_result).
%% API
-export([
new/3,
new/4,
status/1,
clean/1,
print/1
]).
-export([
get_file/1,
get_rules/1,
get_name/1,
get_items/1,
get_message/1,
get_info/1,
get_line_num/1
]).
%% Types
-export_type([
item/0,
rule/0,
file/0
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Records
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type item() ::
#{
message => string(),
info => list()
}.
-type rule() ::
#{
name => atom(),
items => [item()]
}.
-type file() ::
#{
file => elvis_file:file(),
rules => list()
}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Public
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% New
-spec new(item | rule | file, any(), any()) ->
item() | rule() | file().
new(item, Msg, Info) ->
new(item, Msg, Info, 0);
new(rule, Name, Results) ->
#{name => Name, items => Results};
new(file, File, Rules) ->
#{file => File, rules => Rules};
new(error, Msg, Info) ->
#{error_msg => Msg, info => Info}.
-spec new(item, term(), any(), any()) -> item().
new(item, Msg, Info, LineNum) ->
#{message => Msg,
info => Info,
line_num => LineNum}.
%% Getters
-spec get_file(file()) -> elvis_file:file().
get_file(#{file := File}) -> File.
-spec get_rules(file()) -> [rule()].
get_rules(#{rules := Rules}) -> Rules.
-spec get_name(rule()) -> atom().
get_name(#{name := Name}) -> Name.
-spec get_items(rule()) -> [item()].
get_items(#{items := Items}) -> Items;
get_items(_) -> [].
-spec get_message(item()) -> string().
get_message(#{message := Message}) -> Message.
-spec get_info(item()) -> list().
get_info(#{info := Info}) -> Info.
-spec get_line_num(item()) -> list().
get_line_num(#{line_num := LineNum}) -> LineNum.
%% Print
-spec print(item() | rule() | [file()]) -> ok.
print([]) ->
ok;
print([Result | Results]) ->
print(Result),
print(Results);
%% File
print(#{file := File, rules := Rules}) ->
Path = elvis_file:path(File),
Status = case status(Rules) of
ok -> "{{green-bold}}OK";
fail -> "{{red-bold}}FAIL"
end,
elvis_utils:notice("# ~s [~s{{white-bold}}]", [Path, Status]),
print(Rules);
%% Rule
print(#{items := []}) ->
ok;
print(#{name := Name, items := Items}) ->
elvis_utils:notice(" - ~s", [atom_to_list(Name)]),
print(Items);
%% Item
print(#{message := Msg, info := Info}) ->
elvis_utils:notice(" - " ++ Msg, Info);
%% Error
print(#{error_msg := Msg, info := Info}) ->
elvis_utils:error_prn(Msg, Info).
-spec status([file() | rule()]) -> ok | fail.
status([]) ->
ok;
status([#{rules := Rules} | Files]) ->
case status(Rules) of
fail -> fail;
ok -> status(Files)
end;
status([#{items := []} | Rules]) ->
status(Rules);
status(_Rules) ->
fail.
%% @doc Removes files that don't have any failures.
-spec clean([file() | rule()]) -> [file() | rule()].
clean(Files)->
clean(Files, []).
%% @private
-spec clean([file() | rule()], [file() | rule()]) -> [file() | rule()].
clean([], Result) ->
lists:reverse(Result);
clean([#{rules := []} | Files], Result) ->
clean(Files, Result);
clean([File = #{rules := Rules, file := FileInfo} | Files], Result) ->
CleanRules = clean(Rules),
FileInfo1 = maps:remove(content, FileInfo),
FileInfo2 = maps:remove(parse_tree, FileInfo1),
NewFile = File#{rules => CleanRules,
file => FileInfo2},
clean(Files, [NewFile | Result]);
clean([#{items := []} | Rules], Result) ->
clean(Rules, Result);
clean([Rule | Rules], Result) ->
clean(Rules, [Rule | Result]).