Use IO.read/2 :eof option instead of :all in Elixir >= 1.13

This commit is contained in:
Marcel Otto 2021-12-12 23:00:37 +01:00
parent b67db534dd
commit 02e7abc2a2

View file

@ -10,6 +10,7 @@ defmodule RDF.Serialization.Reader do
alias RDF.{Serialization, Dataset, Graph}
@default_file_mode ~w[read utf8]a
@io_read_mode if Version.match?(System.version(), "~> 1.13.0"), do: :eof, else: :all
@spec read_string(module, String.t(), keyword) :: {:ok, Graph.t() | Dataset.t()} | {:error, any}
def read_string(decoder, content, opts \\ []) do
@ -49,9 +50,10 @@ defmodule RDF.Serialization.Reader do
defp do_read_file(false, decoder, file, opts) do
file
|> File.open(file_mode(decoder, opts), &IO.read(&1, :all))
|> File.open(file_mode(decoder, opts), &IO.read(&1, @io_read_mode))
|> case do
{:ok, {:error, error}} -> {:error, error}
{:ok, :eof} -> decoder.decode("", opts)
{:ok, content} -> decoder.decode(content, opts)
{:error, error} -> {:error, error}
end
@ -76,10 +78,11 @@ defmodule RDF.Serialization.Reader do
defp do_read_file!(false, decoder, file, opts) do
file
|> File.open!(file_mode(decoder, opts), &IO.read(&1, :all))
|> File.open!(file_mode(decoder, opts), &IO.read(&1, @io_read_mode))
|> case do
{:error, error} when is_tuple(error) -> error |> inspect() |> raise()
{:error, error} -> raise(error)
:eof -> decoder.decode!("", opts)
content -> decoder.decode!(content, opts)
end
end