-export(
[ return/1
, map/2
+ , map_error/2
+ , tag_error/2
, pipe/2
, lift_exn/1
, lift_exn/2
map({error, _}=Error, _) ->
Error.
+-spec map_error(t(A, B), fun((B) -> (C))) ->
+ t(A, C).
+map_error({ok, _}=Ok, _) ->
+ Ok;
+map_error({error, Reason}, F) ->
+ {error, F(Reason)}.
+
+-spec tag_error(t(A, Reason), Tag) ->
+ t(A, {Tag, Reason}).
+tag_error({ok, _}=Ok, _) ->
+ Ok;
+tag_error({error, Reason}, Tag) ->
+ {error, {Tag, Reason}}.
+
-spec pipe([F], X) ->
t(Ok, Error)
when X :: any()
try
{ok, F(X)}
catch Class:Reason ->
- {error, {Label, {Class, Reason}}}
+ tag_error({error, {Class, Reason}}, Label)
end
end.
-module(hope_result_SUITE).
-%% TODO: Import only what is used.
--include_lib("proper/include/proper.hrl").
-
%% Callbacks
-export(
[ all/0
, t_lift_exn/1
, t_return/1
, t_map/1
+ , t_map_error/1
+ , t_tag_error/1
]).
OtherTests =
[ t_return
, t_map
+ , t_map_error
+ , t_tag_error
],
Properties = [parallel],
[ {?GROUP_PIPE, Properties, PipeTests}
F = fun (foo) -> Y end,
{ok, Y} = hope_result:map({ok, X}, F),
{error, X} = hope_result:map({error, X}, F).
+
+t_map_error(_Cfg) ->
+ X = foo,
+ Y = bar,
+ XtoY = fun (foo) -> Y end,
+ {ok , X} = hope_result:map_error({ok , X}, XtoY),
+ {error, Y} = hope_result:map_error({error, X}, XtoY).
+
+t_tag_error(_Cfg) ->
+ X = foo,
+ Tag = bar,
+ {ok , X } = hope_result:tag_error({ok , X}, Tag),
+ {error, {Tag, X}} = hope_result:tag_error({error, X}, Tag).