Skip to content

Commit

Permalink
refactor!: fix credo rule around naming and guards (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Apr 18, 2024
1 parent cde0855 commit 8a8cfab
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.14.3
erlang 25.3
elixir 1.16.2
erlang 26.2.3
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ defmodule OnePiece.Commanded.TypeProvider do
defp find_mapping_by_name(mod, name) do
mod
|> Module.get_attribute(:type_mapping)
|> Enum.find(&is_mapping?(&1, name))
|> Enum.find(&mapping?(&1, name))
end

defp is_mapping?({_, current_name, _struct_mod}, name) do
defp mapping?({_, current_name, _struct_mod}, name) do
current_name == name
end

Expand Down
11 changes: 11 additions & 0 deletions apps/one_piece_result/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## Unreleased

## v1.0.0 - 2024-04-18

### Breaking changes

Fixed all the Credo warnings about using `is_*` predicates outside of guards.

- `OnePiece.Result.is_ok?/1` becomes `OnePiece.Result.ok?/1`
- `OnePiece.Result.is_ok_and?/1` becomes `OnePiece.Result.ok_and?/1`
- `OnePiece.Result.is_err?/1` becomes `OnePiece.Result.err?/1`
- `OnePiece.Result.is_err_and?/1` becomes `OnePiece.Result.err_and?/1`

## v0.4.0 - 2023-04-26

- Added `OnePiece.Result.unwrap/1`.
Expand Down
42 changes: 21 additions & 21 deletions apps/one_piece_result/lib/one_piece/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,72 +45,72 @@ defmodule OnePiece.Result do
iex> 42
...> |> OnePiece.Result.ok()
...> |> OnePiece.Result.is_ok?()
...> |> OnePiece.Result.ok?()
true
iex> "oops"
...> |> OnePiece.Result.err()
...> |> OnePiece.Result.is_ok?()
...> |> OnePiece.Result.ok?()
false
"""
@spec is_ok?(value :: t) :: boolean
def is_ok?({:ok, _}), do: true
def is_ok?({:error, _}), do: false
@spec ok?(value :: t) :: boolean
def ok?({:ok, _}), do: true
def ok?({:error, _}), do: false

@doc """
Returns true if the result is `t:ok/0` and the value inside of it matches a predicate.
iex> is_meaning_of_life = fn x -> x == 42 end
...> 42
...> |> OnePiece.Result.ok()
...> |> OnePiece.Result.is_ok_and?(is_meaning_of_life)
...> |> OnePiece.Result.ok_and?(is_meaning_of_life)
true
iex> is_meaning_of_life = fn x -> x == 42 end
...> "oops"
...> |> OnePiece.Result.err()
...> |> OnePiece.Result.is_ok_and?(is_meaning_of_life)
...> |> OnePiece.Result.ok_and?(is_meaning_of_life)
false
"""
@spec is_ok_and?(value :: t, predicate :: (any -> boolean)) :: boolean
def is_ok_and?({:error, _}, _func), do: false
def is_ok_and?({:ok, val}, func), do: func.(val) == true
@spec ok_and?(value :: t, predicate :: (any -> boolean)) :: boolean
def ok_and?({:error, _}, _func), do: false
def ok_and?({:ok, val}, func), do: func.(val) == true

@doc """
Returns true if the argument is an `t:err/0`.
iex> 42
...> |> OnePiece.Result.ok()
...> |> OnePiece.Result.is_err?()
...> |> OnePiece.Result.err?()
false
iex> "oops"
...> |> OnePiece.Result.err()
...> |> OnePiece.Result.is_err?()
...> |> OnePiece.Result.err?()
true
"""
@spec is_err?(value :: t) :: boolean
def is_err?({:ok, _}), do: false
def is_err?({:error, _}), do: true
@spec err?(value :: t) :: boolean
def err?({:ok, _}), do: false
def err?({:error, _}), do: true

@doc """
Returns true if the result is `t:err/0` and the value inside of it matches a predicate.
iex> is_not_found = fn err -> err == :not_found end
...> 42
...> |> OnePiece.Result.ok()
...> |> OnePiece.Result.is_err_and?(is_not_found)
...> |> OnePiece.Result.err_and?(is_not_found)
false
iex> is_not_found = fn err -> err == :not_found end
...> :not_found
...> |> OnePiece.Result.err()
...> |> OnePiece.Result.is_err_and?(is_not_found)
...> |> OnePiece.Result.err_and?(is_not_found)
true
"""
@spec is_err_and?(value :: t, predicate :: (any -> boolean)) :: boolean
def is_err_and?({:ok, _}, _func), do: false
def is_err_and?({:error, val}, func), do: func.(val) == true
@spec err_and?(value :: t, predicate :: (any -> boolean)) :: boolean
def err_and?({:ok, _}, _func), do: false
def err_and?({:error, val}, func), do: func.(val) == true

@doc """
Is valid if and only if an `t:ok/0` is supplied.
Expand Down Expand Up @@ -604,7 +604,7 @@ defmodule OnePiece.Result do
...> OnePiece.Result.reject_nil(nil, new_error)
{:error, "ooops"}
"""
@spec reject_nil(value :: any, on_nil :: any | (() -> any)) :: t
@spec reject_nil(value :: any, on_nil :: any | (-> any)) :: t
def reject_nil(nil, on_nil) when is_function(on_nil), do: err(on_nil.())
def reject_nil(nil, on_nil), do: err(on_nil)
def reject_nil({:ok, _} = response, _), do: response
Expand Down
2 changes: 1 addition & 1 deletion apps/one_piece_result/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule OnePiece.Result.MixProject do
use Mix.Project

@app :one_piece_result
@version "0.4.0"
@version "1.0.0"
@elixir_version "~> 1.13"
@source_url "https://github.com/straw-hat-team/beam-monorepo"

Expand Down

0 comments on commit 8a8cfab

Please sign in to comment.