Skip to content

Commit

Permalink
Revert changes to the public API
Browse files Browse the repository at this point in the history
Applying suggestions from code review.
  • Loading branch information
sliiser committed Jul 2, 2024
1 parent 90daa84 commit 88c8feb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
34 changes: 30 additions & 4 deletions lib/rustler_precompiled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,19 @@ defmodule RustlerPrecompiled do

@native_dir "priv/native"

@doc deprecated: "Use available_nifs/1 instead"
def available_nif_urls(nif_module) when is_atom(nif_module) do
available_nifs(nif_module)
|> Enum.map(fn {_lib_name, {url, _headers}} -> url end)
end

@doc """
Returns tuples of file names and their URLs for NIFs based on its module name.
Returns URLs for NIFs based on its module name as a list of tuples: `[{lib_name, {url, headers}}]`.
The module name is the one that defined the NIF and this information
is stored in a metadata file.
"""
def available_nif_urls(nif_module) when is_atom(nif_module) do
def available_nifs(nif_module) when is_atom(nif_module) do
nif_module
|> metadata_file()
|> read_map_from_file()
Expand All @@ -299,6 +305,17 @@ defmodule RustlerPrecompiled do

@doc false
def nif_urls_from_metadata(metadata) when is_map(metadata) do
case(nifs_from_metadata(metadata)) do
{:ok, nifs} ->
{:ok, Enum.map(nifs, fn {_lib_name, {url, _headers}} -> url end)}

{:error, wrong_meta} ->
{:error, wrong_meta}
end
end

@doc false
def nifs_from_metadata(metadata) when is_map(metadata) do
case metadata do
%{
targets: targets,
Expand Down Expand Up @@ -340,13 +357,19 @@ defmodule RustlerPrecompiled do

defp maybe_variants_tar_gz_urls(_, _, _, _), do: []

@doc deprecated: "Use current_target_nifs/1 instead"
def current_target_nif_urls(nif_module) when is_atom(nif_module) do
current_target_nifs(nif_module)
|> Enum.map(fn {_lib_name, {url, _headers}} -> url end)
end

@doc """
Returns tuples of file names and their URLs to be downloaded for current target.
Returns the file URLs to be downloaded for current target as a list of tuples: `[{lib_name, {url, headers}}]`.
It is in the plural because a target may have some variants for it.
It receives the NIF module.
"""
def current_target_nif_urls(nif_module) when is_atom(nif_module) do
def current_target_nifs(nif_module) when is_atom(nif_module) do
metadata =
nif_module
|> metadata_file()
Expand Down Expand Up @@ -920,6 +943,9 @@ defmodule RustlerPrecompiled do

options = [body_format: :binary]

request_headers =
Enum.map(request_headers, fn {k, v} when is_binary(k) -> {String.to_charlist(k), v} end)

case :httpc.request(:get, {url, request_headers}, http_options, options) do
{:ok, {{_, 200, _}, _headers, body}} ->
{:ok, body}
Expand Down
8 changes: 4 additions & 4 deletions lib/rustler_precompiled/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ defmodule RustlerPrecompiled.Config do

defp validate_base_url!(nil), do: raise_for_nil_field_value(:base_url)

defp validate_base_url!(base_url) when is_binary(base_url) do
validate_base_url!({base_url, []})
end

defp validate_base_url!({base_url, headers}) when is_binary(base_url) and is_list(headers) do
case :uri_string.parse(base_url) do
%{} ->
Expand All @@ -107,10 +111,6 @@ defmodule RustlerPrecompiled.Config do
end
end

defp validate_base_url!(base_url) when is_binary(base_url) do
validate_base_url!({base_url, []})
end

defp validate_list!(nil, option, _valid_values), do: raise_for_nil_field_value(option)

defp validate_list!([_ | _] = values, option, valid_values) do
Expand Down
6 changes: 3 additions & 3 deletions test/rustler_precompiled_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ defmodule RustlerPrecompiledTest do
module: RustlerPrecompilationExample.Native,
base_cache_dir: tmp_dir,
base_url:
{"http://localhost:#{bypass.port}/download", [{'authorization', "Token 123"}]},
{"http://localhost:#{bypass.port}/download", [{"authorization", "Token 123"}]},
version: "0.2.0",
crate: "example",
targets: @available_targets,
Expand Down Expand Up @@ -1015,7 +1015,7 @@ defmodule RustlerPrecompiledTest do
"libexample-v0.2.0-nif-2.17-x86_64-unknown-linux-gnu.so.tar.gz",
"libexample-v0.2.0-nif-2.17-x86_64-unknown-linux-musl.so.tar.gz"
]
|> Enum.map(fn file_name -> {file_name, {"#{base_url}/#{file_name}", []}} end)
|> Enum.map(fn file_name -> "#{base_url}/#{file_name}" end)
end

test "does not build list of tar gz urls due to missing metadata field" do
Expand Down Expand Up @@ -1063,6 +1063,6 @@ defmodule RustlerPrecompiledTest do

def url_with_headers(file_name) do
{"http://localhost:1234/download?file_name=#{file_name}&foo=bar",
[{'authorization', "Token 123"}]}
[{"authorization", "Token 123"}]}
end
end

0 comments on commit 88c8feb

Please sign in to comment.