2018-03-08 16:33:20 +00:00
|
|
|
defmodule RDF.Serialization.Reader do
|
2017-06-16 15:33:05 +00:00
|
|
|
@moduledoc """
|
2018-03-08 16:33:20 +00:00
|
|
|
General functions for reading a `RDF.Graph` or `RDF.Dataset` from a serialization file or encoded-string.
|
2017-04-10 19:24:43 +00:00
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
You probably won't use these functions directly, but instead use the automatically
|
2018-03-08 16:33:20 +00:00
|
|
|
generated functions with same name on a `RDF.Serialization.Format`, which implicitly
|
2017-06-16 15:33:05 +00:00
|
|
|
use the proper `RDF.Serialization.Decoder` module.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
|
2020-03-10 00:37:53 +00:00
|
|
|
alias RDF.{Dataset, Graph}
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a string.
|
|
|
|
|
|
|
|
It returns an `{:ok, data}` tuple, with `data` being the deserialized graph or
|
|
|
|
dataset, or `{:error, reason}` if an error occurs.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@spec read_string(module, String.t, keyword) :: {:ok, Graph.t | Dataset.t} | {:error, any}
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string(decoder, content, opts \\ []) do
|
2019-08-03 22:13:13 +00:00
|
|
|
decoder.decode(content, opts)
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a string.
|
|
|
|
|
|
|
|
As opposed to `read_string`, it raises an exception if an error occurs.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@spec read_string!(module, String.t, keyword) :: Graph.t | Dataset.t
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string!(decoder, content, opts \\ []) do
|
2019-08-03 22:13:13 +00:00
|
|
|
decoder.decode!(content, opts)
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a file.
|
|
|
|
|
|
|
|
It returns an `{:ok, data}` tuple, with `data` being the deserialized graph or
|
|
|
|
dataset, or `{:error, reason}` if an error occurs.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@spec read_file(module, Path.t, keyword) :: {:ok, Graph.t | Dataset.t} | {:error, any}
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_file(decoder, file, opts \\ []) do
|
|
|
|
case File.read(file) do
|
2019-08-03 22:13:13 +00:00
|
|
|
{:ok, content} -> read_string(decoder, content, opts)
|
2017-04-10 19:24:43 +00:00
|
|
|
{:error, reason} -> {:error, reason}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a file.
|
|
|
|
|
|
|
|
As opposed to `read_file`, it raises an exception if an error occurs.
|
|
|
|
"""
|
2020-03-10 00:37:53 +00:00
|
|
|
@spec read_file!(module, Path.t, keyword) :: Graph.t | Dataset.t
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_file!(decoder, file, opts \\ []) do
|
2017-05-15 20:48:55 +00:00
|
|
|
with content = File.read!(file) do
|
2019-08-03 22:13:13 +00:00
|
|
|
read_string!(decoder, content, opts)
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|