-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.ex
More file actions
38 lines (32 loc) · 847 Bytes
/
decoder.ex
File metadata and controls
38 lines (32 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule Msgpack.Decoder do
@moduledoc """
Handles the logic of decoding a MessagePack binary into an Elixir term.
"""
alias Msgpack.Decoder.Internal
@spec decode(binary(), keyword()) :: {:ok, term()} | {:error, term()}
def decode(binary, opts \\ []) do
merged_opts = Keyword.merge(default_opts(), opts)
try do
case Internal.decode(binary, merged_opts) do
{:ok, {term, <<>>}} ->
{:ok, term}
{:ok, {_term, rest}} ->
{:error, {:trailing_bytes, rest}}
{:error, reason} ->
{:error, reason}
end
catch
{:error, reason} ->
{:error, reason}
end
end
@doc """
Returns a keyword list of the default options for the decoder.
"""
def default_opts() do
[
max_depth: 100,
max_byte_size: 10_000_000 # 10MB
]
end
end