-module(yodel@format). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/yodel/format.gleam"). -export([get_format/4]). -export_type([format_detector/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. -type format_detector() :: {format_detector, binary(), fun((yodel@input:input()) -> yodel@options:format())}. -file("src/yodel/format.gleam", 32). -spec detect_format(yodel@input:input(), list(format_detector())) -> yodel@options:format(). detect_format(Input, Formats) -> gleam@list:fold(Formats, auto, fun(Acc, Format) -> case Acc of auto -> (erlang:element(3, Format))(Input); _ -> Acc end end). -file("src/yodel/format.gleam", 16). ?DOC( " if the user specified a format, use it\n" " otherwise, try to detect the format from the input\n" " if that fails, try to detect the format from the content\n" " and if that fails, return `Auto` because we didn't figure it out\n" ). -spec get_format( binary(), binary(), yodel@options:options(), list(format_detector()) ) -> yodel@options:format(). get_format(Input, Content, Options, Formats) -> case yodel@options:get_format(Options) of auto -> case begin _pipe = Input, _pipe@1 = yodel@input:detect_input(_pipe), detect_format(_pipe@1, Formats) end of auto -> _pipe@2 = Content, _pipe@3 = yodel@input:detect_input(_pipe@2), detect_format(_pipe@3, Formats); Format -> Format end; Format@1 -> Format@1 end.