Elixir/Ecto find_or_create with custom error I need to find_or_create user, but first, check if his email ends with a required domain.

Can I somehow write following code using pattern matching with/case block?

  def create_user(attrs \\ %{}) do
    %User{}
    |&gt; User.changeset(attrs)
    |&gt; Repo.insert()
  end

  def find_or_create_user(attrs \\ %{}) do
    if String.ends_with?(attrs.email, "@test.com") do
      user = Repo.get_by(User, %{email: attrs.email}) # returns nil or user
      if user do
        {:ok, user}
      else
        create_user(attrs) # returns {:ok, user} or {:error, :reason}
      end
    else
      {:error, :invalid_email_domain}
    end
  end
I think it is a perfect fit for with. It will be something like:

def find_or_create_user(attrs \\ %{}) do
  with  true &lt;- String.ends_with?(attrs.email, "@test.com"),
        nil &lt;- Repo.get_by(User, %{email: attrs.email}),
        {:ok, user} &lt;- create_user(attrs)
  do
    {:ok, user}
  else
    false -&gt; {:error, :invalid_email_domain}
    %User{} = user -&gt; {:ok, user}
    {:error, changeset} -&gt; {:error, changeset}
    _anything_else -&gt; {:error, "cannot find or create user"}
  end
end