-module(yodel@internal@input). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/yodel/internal/input.gleam"). -export([detect_input/1, get_extension_from_path/1, read_file/1, get_content/1, list_files/1]). -export_type([input/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(false). -type input() :: {file, binary()} | {directory, binary()} | {content, binary()}. -file("src/yodel/internal/input.gleam", 33). ?DOC(false). -spec detect_input(binary()) -> input(). detect_input(Input) -> Input@1 = gleam@string:trim(Input), case {simplifile_erl:is_file(Input@1), simplifile_erl:is_directory(Input@1)} of {{ok, true}, _} -> {file, Input@1}; {_, {ok, true}} -> {directory, Input@1}; {_, _} -> {content, Input@1} end. -file("src/yodel/internal/input.gleam", 45). ?DOC(false). -spec get_extension_from_path(binary()) -> binary(). get_extension_from_path(Path) -> case begin _pipe = Path, _pipe@1 = gleam@string:trim(_pipe), _pipe@2 = string:lowercase(_pipe@1), _pipe@3 = gleam@string:split(_pipe@2, <<"."/utf8>>), gleam@list:last(_pipe@3) end of {ok, Ext} -> string:lowercase(Ext); _ -> <<""/utf8>> end. -file("src/yodel/internal/input.gleam", 79). ?DOC(false). -spec map_simplifile_error(simplifile:file_error()) -> yodel@errors:config_error(). map_simplifile_error(Error) -> {file_error, case Error of eacces -> {file_permission_denied, simplifile:describe_error(Error)}; enoent -> {file_not_found, simplifile:describe_error(Error)}; _ -> {file_read_error, simplifile:describe_error(Error)} end}. -file("src/yodel/internal/input.gleam", 55). ?DOC(false). -spec read_file(binary()) -> {ok, binary()} | {error, yodel@errors:config_error()}. read_file(Path) -> _pipe = simplifile:read(Path), gleam@result:map_error(_pipe, fun(Err) -> map_simplifile_error(Err) end). -file("src/yodel/internal/input.gleam", 24). ?DOC(false). -spec get_content(binary()) -> {ok, binary()} | {error, yodel@errors:config_error()}. get_content(Input) -> case begin _pipe = Input, detect_input(_pipe) end of {file, Path} -> read_file(Path); {content, Content} -> {ok, Content}; {directory, Dir} -> {error, {file_error, {not_a_file, Dir}}} end. -file("src/yodel/internal/input.gleam", 70). ?DOC(false). -spec ls( binary(), fun((list(binary())) -> {ok, list(binary())} | {error, yodel@errors:config_error()}) ) -> {ok, list(binary())} | {error, yodel@errors:config_error()}. ls(Path, Handler) -> _pipe = simplifile_erl:read_directory(Path), _pipe@1 = gleam@result:map_error( _pipe, fun(Err) -> map_simplifile_error(Err) end ), gleam@result:'try'(_pipe@1, Handler). -file("src/yodel/internal/input.gleam", 61). ?DOC(false). -spec list_files(binary()) -> {ok, list(binary())} | {error, yodel@errors:config_error()}. list_files(Directory) -> ls(Directory, fun(Files) -> _pipe = Files, _pipe@1 = gleam@list:map( _pipe, fun(File) -> <<<>/binary, File/binary>> end ), _pipe@2 = gleam@list:filter( _pipe@1, fun(File_path) -> simplifile_erl:is_file(File_path) =:= {ok, true} end ), {ok, _pipe@2} end).