From 970fc1e9344bdbb308dfb375362f8343d4af70e3 Mon Sep 17 00:00:00 2001 From: ryantaylor Date: Fri, 18 Oct 2019 21:58:50 -0400 Subject: [PATCH 1/5] Rename ok to do_try and add Rust-like ok and err functions. --- lib/rop.ex | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/rop.ex b/lib/rop.ex index 05743a9..02fc50b 100644 --- a/lib/rop.ex +++ b/lib/rop.ex @@ -15,19 +15,28 @@ defmodule Rop do Raise the arguments else For example: - iex> ok({:ok, 1}) + iex> do_try({:ok, 1}) 1 - iex> ok({:error, "some"}) + iex> do_try({:error, "some"}) ** (RuntimeError) some - iex> ok({:anything, "some"}) + iex> do_try({:anything, "some"}) ** (ArgumentError) raise/1 and reraise/2 expect a module name, string or exception as the first argument, got: {:anything, \"some\"} """ - def ok({:ok, x}), do: x - def ok({:error, x}), do: raise x - def ok(x), do: raise x + def do_try({:ok, x}), do: x + def do_try({:error, x}), do: raise x + def do_try(x), do: raise x + @doc ~s""" + Wraps the value in an ok tagged tuple like {:ok, value} + """ + def ok(x), do: {:ok, x} + + @doc ~s""" + Wraps the value in an error tagged tuple like {:error, value} + """ + def err(x), do: {:error, x} @doc ~s""" No need to stop pipelining in case of an error somewhere in the middle From a356003a4838ef93704a71fb178daa54e6364e3f Mon Sep 17 00:00:00 2001 From: ryantaylor Date: Fri, 18 Oct 2019 21:59:19 -0400 Subject: [PATCH 2/5] Upgrade ExSpec and fix compiler warning. --- mix.exs | 2 +- mix.lock | 8 +++++--- test/rop_test.exs | 10 +++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mix.exs b/mix.exs index 4eef709..a3f1ba1 100644 --- a/mix.exs +++ b/mix.exs @@ -32,7 +32,7 @@ defmodule Rop.Mixfile do # Type "mix help deps" for more examples and options defp deps do [ - {:ex_spec, "~> 1.0", only: :test}, + {:ex_spec, "~> 2.0", only: :test}, {:earmark, "~> 0.2", only: :dev}, {:ex_doc, "~> 0.11", only: :dev} ] diff --git a/mix.lock b/mix.lock index 469cb0a..826e290 100644 --- a/mix.lock +++ b/mix.lock @@ -1,3 +1,5 @@ -%{"earmark": {:hex, :earmark, "0.2.0"}, - "ex_doc": {:hex, :ex_doc, "0.11.3"}, - "ex_spec": {:hex, :ex_spec, "1.0.0"}} +%{ + "earmark": {:hex, :earmark, "0.2.0", "bc1636bc2efa0c1c172a5bcbf7c8eb73632d8c4512a6c2dac02d2ae454750af6", [:mix], [], "hexpm"}, + "ex_doc": {:hex, :ex_doc, "0.11.3", "bb16cb3f4135d880ce25279dc19a9d70802bc4f4942f0c3de9e4862517ae3ace", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, repo: "hexpm", optional: true]}], "hexpm"}, + "ex_spec": {:hex, :ex_spec, "2.0.1", "8bdbd6fa85995fbf836ed799571d44be6f9ebbcace075209fd0ad06372c111cf", [:mix], [], "hexpm"}, +} diff --git a/test/rop_test.exs b/test/rop_test.exs index a3cf6f6..53deb00 100644 --- a/test/rop_test.exs +++ b/test/rop_test.exs @@ -5,7 +5,7 @@ defmodule RopTest do doctest Rop - ExSpec.describe ">>> macro" do + describe ">>> macro" do test "returns the piped value in case of happy path" do assert (0 |> inc >>> inc >>> inc) == {:ok, 3} end @@ -23,7 +23,7 @@ defmodule RopTest do end end - ExSpec.describe "try_catch macro" do + describe "try_catch macro" do test "catches and returns raised errors in a tagged tupple { :error, %SomeError{} } if something breaks" do assert (:raise |> try_catch(arithmetic_error)) == {:error, %ArithmeticError{}} end @@ -33,7 +33,7 @@ defmodule RopTest do end end - ExSpec.describe "tee" do + describe "tee" do test "passes the arguments through after executing them in the function" do a = (fn -> 0 |> simple_inc |> simple_inc |> tee(simple_sideeffect) @@ -50,7 +50,7 @@ defmodule RopTest do end end - ExSpec.describe "bind" do + describe "bind" do test "wraps a function to return a tagged tuple `{:ok, result}` from the returned value" do a = 0 |> simple_inc |> bind(simple_inc) assert a == {:ok, 2} @@ -80,6 +80,6 @@ defmodule RopTest do end defp arithmetic_error(:raise) do - 1 / 0 + raise ArithmeticError end end From bec2f21a46032f677127733b4ada331c0a781443 Mon Sep 17 00:00:00 2001 From: ryantaylor Date: Fri, 18 Oct 2019 23:09:36 -0400 Subject: [PATCH 3/5] Add cases to handle ok/error inputs in ok function. --- lib/rop.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/rop.ex b/lib/rop.ex index 02fc50b..2c7f065 100644 --- a/lib/rop.ex +++ b/lib/rop.ex @@ -31,6 +31,8 @@ defmodule Rop do @doc ~s""" Wraps the value in an ok tagged tuple like {:ok, value} """ + def ok({:ok, x}), do: {:ok, x} + def ok({:error, x}), do: {:error, x} def ok(x), do: {:ok, x} @doc ~s""" From 031efb5e23cea3d9e516b58e74ee4e10858e694b Mon Sep 17 00:00:00 2001 From: ryantaylor Date: Fri, 18 Oct 2019 23:42:54 -0400 Subject: [PATCH 4/5] Update try_catch to return tuple on success. --- lib/rop.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rop.ex b/lib/rop.ex index 2c7f065..bae13c9 100644 --- a/lib/rop.ex +++ b/lib/rop.ex @@ -97,7 +97,7 @@ defmodule Rop do quote do (fn -> try do - unquote(args) |> unquote(func) + {:ok, unquote(args) |> unquote(func)} rescue e -> {:error, e} end From 519244c82518bc8636478e1819a76b2d7fcfc24b Mon Sep 17 00:00:00 2001 From: ryantaylor Date: Fri, 18 Oct 2019 23:45:58 -0400 Subject: [PATCH 5/5] Fix test to conform to expectations. --- test/rop_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rop_test.exs b/test/rop_test.exs index 53deb00..e5f519d 100644 --- a/test/rop_test.exs +++ b/test/rop_test.exs @@ -28,8 +28,8 @@ defmodule RopTest do assert (:raise |> try_catch(arithmetic_error)) == {:error, %ArithmeticError{}} end - test "returns the value otherwise" do - assert (:pass |> try_catch(arithmetic_error)) == 1 + test "returns the piped value otherwise" do + assert (:pass |> try_catch(arithmetic_error)) == {:ok, 1} end end