Current section
Files
Jump to
Current section
Files
src/prequel@error@validation_error.erl
-module(prequel@error@validation_error).
-compile([no_auto_import, nowarn_unused_vars]).
-export([to_report/3]).
-export_type([validation_error/0]).
-type validation_error() :: {lower_bound_greater_than_upper_bound,
gleam@option:option(binary()),
prequel@span:span(),
prequel@ast:cardinality()} |
{duplicate_entity_name,
gleam@option:option(binary()),
prequel@ast:entity(),
prequel@ast:entity()} |
{duplicate_relationship_name,
gleam@option:option(binary()),
prequel@ast:relationship(),
prequel@ast:relationship()}.
-spec main_span(validation_error()) -> prequel@span:span().
main_span(Error) ->
case Error of
{lower_bound_greater_than_upper_bound, _, _, Cardinality} ->
erlang:element(2, Cardinality);
{duplicate_entity_name, _, _, Other_entity} ->
erlang:element(2, Other_entity);
{duplicate_relationship_name, _, _, Other_relationship} ->
erlang:element(2, Other_relationship)
end.
-spec name(validation_error()) -> binary().
name(Error) ->
case Error of
{lower_bound_greater_than_upper_bound, _, _, _} ->
<<"Lower bound greater than upper bound"/utf8>>;
{duplicate_entity_name, _, _, _} ->
<<"Duplicate entity name"/utf8>>;
{duplicate_relationship_name, _, _, _} ->
<<"Duplicate relationship name"/utf8>>
end.
-spec code(validation_error()) -> binary().
code(Error) ->
case Error of
{lower_bound_greater_than_upper_bound, _, _, _} ->
<<"VE001"/utf8>>;
{duplicate_entity_name, _, _, _} ->
<<"VE002"/utf8>>;
{duplicate_relationship_name, _, _, _} ->
<<"VE003"/utf8>>
end.
-spec message(validation_error()) -> binary().
message(Error) ->
case Error of
{lower_bound_greater_than_upper_bound, _, _, _} ->
<<"The lower bound of a cardinality should always be lower than its upper bound"/utf8>>;
{duplicate_entity_name, _, _, _} ->
<<"Two entities cannot have the same name. Here is the first one..."/utf8>>;
{duplicate_relationship_name, _, _, _} ->
<<"Two relationships cannot have the same name. Here is the first one..."/utf8>>
end.
-spec blocks(validation_error()) -> non_empty_list:non_empty_list(prequel@internals@report:report_block()).
blocks(Error) ->
case Error of
{lower_bound_greater_than_upper_bound, _, Enclosing_entity, Cardinality} ->
non_empty_list:new(
{context_block, Enclosing_entity},
[{error_block,
erlang:element(2, Cardinality),
none,
message(Error)}]
);
{duplicate_entity_name, _, One, Other} ->
non_empty_list:new(
{error_block, erlang:element(2, One), none, message(Error)},
[{error_block,
erlang:element(2, Other),
none,
<<"...and here is the other one"/utf8>>}]
);
{duplicate_relationship_name, _, One@1, Other@1} ->
non_empty_list:new(
{error_block, erlang:element(2, One@1), none, message(Error)},
[{error_block,
erlang:element(2, Other@1),
none,
<<"...and here is the other one"/utf8>>}]
)
end.
-spec to_report(validation_error(), binary(), binary()) -> prequel@internals@report:report().
to_report(Error, File_name, Source_code) ->
Main_span = main_span(Error),
Line = erlang:element(2, Main_span),
Column = erlang:element(4, Main_span),
Name = name(Error),
Code = code(Error),
Blocks = blocks(Error),
{report,
File_name,
Source_code,
Name,
Code,
Line,
Column,
Blocks,
erlang:element(2, Error)}.