2017-06-16 12:21:12 +00:00
|
|
|
defmodule RDF.Reader do
|
2017-06-16 15:33:05 +00:00
|
|
|
@moduledoc """
|
|
|
|
General serialization-independent functions for reading a `RDF.Graph` or `RDF.Dataset` from a 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
|
|
|
|
generated functions with same name on a `RDF.Serialization`, which implicitly
|
|
|
|
use the proper `RDF.Serialization.Decoder` module.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string(decoder, content, opts \\ []) do
|
|
|
|
decoder.decode(content, opts)
|
|
|
|
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.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string!(decoder, content, opts \\ []) do
|
|
|
|
decoder.decode!(content, opts)
|
|
|
|
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.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_file(decoder, file, opts \\ []) do
|
|
|
|
case File.read(file) do
|
|
|
|
{:ok, content} -> read_string(decoder, content, opts)
|
|
|
|
{: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.
|
|
|
|
"""
|
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
|
|
|
|
read_string!(decoder, content, opts)
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|