@@ -49,6 +49,8 @@ defmodule Msgpack do
4949
5050 alias Msgpack.Encoder
5151 alias Msgpack.Decoder
52+ alias Msgpack.StreamEncoder
53+ alias Msgpack.StreamDecoder
5254 alias Msgpack.EncodeError
5355 alias Msgpack.DecodeError
5456
@@ -314,4 +316,63 @@ defmodule Msgpack do
314316 raise DecodeError , reason: reason
315317 end
316318 end
319+
320+ @ doc """
321+ Encodes a stream of Elixir terms into a stream of MessagePack binaries.
322+
323+ Each term in the input enumerable is encoded individually. The output stream
324+ will contain `{:ok, binary}` tuples for successful encodings or `{:error,
325+ reason}` tuples for failures.
326+
327+ This function delegates to `Msgpack.StreamEncoder.encode/2`.
328+
329+ ## Options
330+
331+ Accepts the same options as `Msgpack.encode/2`.
332+
333+ ## Examples
334+
335+ ```elixir
336+ iex> terms = [1, "elixir", :world]
337+ iex> Msgpack.encode_stream(terms, atoms: :string) |> Enum.to_list()
338+ [
339+ {:ok, <<1>>},
340+ {:ok, <<166, 101, 108, 105, 120, 105, 114>>},
341+ {:ok, <<165, 119, 111, 114, 108, 100>>}
342+ ]
343+ ```
344+ """
345+ @ spec encode_stream ( Enumerable . t ( ) , StreamEncoder . opts_t ( ) ) :: StreamEncoder . t ( )
346+ def encode_stream ( enumerable , opts \\ [ ] ) do
347+ StreamEncoder . encode ( enumerable , opts )
348+ end
349+
350+ @ doc """
351+ Decodes a stream of MessagePack binaries into a stream of Elixir terms.
352+
353+ This function provides a streaming, lazy interface for decoding, making it
354+ suitable for handling large datasets that do not fit into memory.
355+
356+ It delegates to `Msgpack.StreamDecoder.decode/2`.
357+
358+ For more detailed information on behavior, see the `Msgpack.StreamDecoder`
359+ module documentation.
360+
361+ ## Options
362+
363+ Accepts the same options as `Msgpack.decode/2`.
364+
365+ ## Examples
366+
367+ ```elixir
368+ iex> objects = [1, "elixir", true]
369+ iex> stream = Enum.map(objects, &Msgpack.encode!/1)
370+ iex> Msgpack.decode_stream(stream) |> Enum.to_list()
371+ [1, "elixir", true]
372+ ```
373+ """
374+ @ spec decode_stream ( Enumerable . t ( binary ( ) ) , StreamDecoder . opts_t ( ) ) :: StreamDecoder . t ( )
375+ def decode_stream ( enumerable , opts \\ [ ] ) do
376+ StreamDecoder . decode ( enumerable , opts )
377+ end
317378end
0 commit comments