Current section
Files
Jump to
Current section
Files
src/plushie@binary.erl
-module(plushie@binary).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/plushie/binary.gleam").
-export([error_to_string/1, find/0]).
-export_type([binary_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Resolve the path to the plushie Rust binary.\n"
"\n"
" Resolution order:\n"
" 1. PLUSHIE_BINARY_PATH env var (error if set but file missing)\n"
" 2. priv/bin/plushie (installed by `gleam run -m plushie/download` or `gleam run -m plushie/build`)\n"
" 3. Precompiled at priv/bin/{platform}-{arch}/plushie\n"
" 4. Custom build at _build/{env}/plushie/target/release/plushie\n"
" 5. Common local paths (./plushie, ../plushie/target/release/plushie)\n"
"\n"
" Returns Result(String, BinaryError) with the path on success.\n"
).
-type binary_error() :: {not_found, list(binary())} |
{env_var_points_to_missing, binary()}.
-file("src/plushie/binary.gleam", 46).
?DOC(" Format a binary error as a human-readable string.\n").
-spec error_to_string(binary_error()) -> binary().
error_to_string(Err) ->
case Err of
{not_found, Searched} ->
<<<<"not found (searched: "/utf8,
(gleam@string:join(Searched, <<", "/utf8>>))/binary>>/binary,
")"/utf8>>;
{env_var_points_to_missing, Path} ->
<<"PLUSHIE_BINARY_PATH points to missing file: "/utf8, Path/binary>>
end.
-file("src/plushie/binary.gleam", 55).
-spec candidate_paths() -> list(binary()).
candidate_paths() ->
Platform = plushie_ffi:platform_string(),
Arch = plushie_ffi:arch_string(),
Name = <<"plushie"/utf8>>,
[<<"priv/bin/"/utf8, Name/binary>>,
<<<<<<<<<<"priv/bin/"/utf8, Platform/binary>>/binary, "-"/utf8>>/binary,
Arch/binary>>/binary,
"/"/utf8>>/binary,
Name/binary>>,
<<"_build/dev/plushie/target/release/"/utf8, Name/binary>>,
<<"_build/prod/plushie/target/release/"/utf8, Name/binary>>,
<<"./"/utf8, Name/binary>>,
<<"../plushie/target/release/"/utf8, Name/binary>>,
<<"../plushie/target/debug/"/utf8, Name/binary>>].
-file("src/plushie/binary.gleam", 37).
-spec find_in_standard_paths() -> {ok, binary()} | {error, binary_error()}.
find_in_standard_paths() ->
Paths = candidate_paths(),
case gleam@list:find(Paths, fun plushie_ffi:file_exists/1) of
{ok, Path} ->
{ok, Path};
{error, _} ->
{error, {not_found, Paths}}
end.
-file("src/plushie/binary.gleam", 25).
?DOC(" Find the plushie binary, searching in priority order.\n").
-spec find() -> {ok, binary()} | {error, binary_error()}.
find() ->
case plushie_ffi:get_env(<<"PLUSHIE_BINARY_PATH"/utf8>>) of
{ok, Path} ->
case plushie_ffi:file_exists(Path) of
true ->
{ok, Path};
false ->
{error, {env_var_points_to_missing, Path}}
end;
{error, _} ->
find_in_standard_paths()
end.