RustlePoc
A POC using Elixir -> Rustler -> Rust
NOTES
There seem to be a couple of different approaches to error handling with a Rust NIF.
The more Rust way seems to be let it blow up, which on the Elixir side becomes an ElrangError
:
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: IoError("No such file or directory (os error 2)")', src/lib.rs:16:60
** (ErlangError) Erlang error: :nif_panicked
code: assert RustlePoc.Mmdb.country_code_for_ip_address(ip_address, db_path) == {:ok, "SE"}
stacktrace:
(rustle_poc 0.1.0) RustlePoc.Native.country_code_for_ip_address("89.160.20.112", "GeoIP2-Country.mmdb")
test/rustle_poc_test.exs:9: (test)
I did not find this helpful at all to the Elixir calling code
A more Elixir way is to match all error cases in Rust code and return {:ok, _} | {:error, _}
This gives the calling code a nice expectation of what can happen and Ruslter
makes it simple to return
tuples like this.
I think another ELixir way would be to return Elixir Error structs instead of and error tuple
I also think there may be a more in between way that handles the errors at this level
and can return {:ok, _} | {:error, _}
Either way some where has to handle the errors and return something usable back.
I would like find a better way in Rust, I am not fond of the Javascript style Flying V formation
After getting feedback from a community member I have learned a better pattern for the Rust side that made for an easy refactor.
Instead of matching on every Result case .map_err()
and ?
can be used in conjunction to either unwrap the Ok
or Map the Err
to the
appropriate api error. I looked at this function earlier as I was trying to figure out a better pattern but did not understand how this worked.
Installation
If available in Hex, the package can be installed
by adding rustle_poc
to your list of dependencies in mix.exs
:
def deps do
[
{:rustle_poc, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/rustle_poc.