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, download_dir/0, 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. build/plushie/bin/plushie-{platform}-{arch} (downloaded binary)\n"
" 3. build/plushie/bin/plushie (platform-generic fallback)\n"
" 4. priv/bin/plushie-{platform}-{arch} (legacy location, backward compat)\n"
" 5. priv/bin/plushie (legacy location, backward compat)\n"
" 6. Custom build at _build/{env}/plushie-renderer/target/release/plushie-renderer\n"
" 7. Common local paths (./plushie, ../plushie-renderer/target/release/plushie-renderer)\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", 48).
?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", 60).
?DOC(
" Returns the directory where downloaded binaries are stored.\n"
" Shared across environments (the binary is platform-specific,\n"
" not env-specific).\n"
).
-spec download_dir() -> binary().
download_dir() ->
<<"build/plushie/bin"/utf8>>.
-file("src/plushie/binary.gleam", 64).
-spec candidate_paths() -> list(binary()).
candidate_paths() ->
Platform = plushie_ffi:platform_string(),
Arch = plushie_ffi:arch_string(),
Name = <<"plushie"/utf8>>,
Platform_name = <<<<<<<<Name/binary, "-"/utf8>>/binary, Platform/binary>>/binary,
"-"/utf8>>/binary,
Arch/binary>>,
[<<<<(download_dir())/binary, "/"/utf8>>/binary, Platform_name/binary>>,
<<<<(download_dir())/binary, "/"/utf8>>/binary, Name/binary>>,
<<"priv/bin/"/utf8, Platform_name/binary>>,
<<"priv/bin/"/utf8, Name/binary>>,
<<"_build/dev/plushie-renderer/target/release/plushie-renderer"/utf8>>,
<<"_build/prod/plushie-renderer/target/release/plushie-renderer"/utf8>>,
<<"./"/utf8, Name/binary>>,
<<"../plushie/target/release/"/utf8, Name/binary>>,
<<"../plushie/target/debug/"/utf8, Name/binary>>].
-file("src/plushie/binary.gleam", 39).
-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", 27).
?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.