{-# LANGUAGE OverloadedStrings #-}
module Data.RDF.Encoder.NQuads (
encodeRDFGraph
, encodeRDFGraphs
, encodeTriple
, encodeQuad
) where
import qualified Data.ByteString.Builder as B
import Data.RDF.Types
import Data.RDF.Encoder.Common
encodeTriple :: Triple -> B.Builder
encodeTriple :: Triple -> Builder
encodeTriple (Triple Subject
s Predicate
p Object
o) = Subject -> Builder
encodeSubject Subject
s
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" "
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Predicate -> Builder
encodePredicate Predicate
p
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" "
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Object -> Builder
encodeObject Object
o
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" .\n"
encodeQuad :: Quad -> B.Builder
encodeQuad :: Quad -> Builder
encodeQuad (Quad Triple
t Maybe IRI
Nothing) = Triple -> Builder
encodeTriple Triple
t
encodeQuad (Quad (Triple Subject
s Predicate
p Object
o) (Just IRI
g)) = Subject -> Builder
encodeSubject Subject
s
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" "
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Predicate -> Builder
encodePredicate Predicate
p
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" "
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Object -> Builder
encodeObject Object
o
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" "
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> IRI -> Builder
encodeEscapedIRI IRI
g
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
B.byteString ByteString
" .\n"
encodeRDFGraph :: RDFGraph -> B.Builder
encodeRDFGraph :: RDFGraph -> Builder
encodeRDFGraph (RDFGraph Maybe IRI
Nothing [Triple]
ts) = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ (Triple -> Builder) -> [Triple] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map Triple -> Builder
encodeTriple [Triple]
ts
encodeRDFGraph (RDFGraph (Just IRI
g) [Triple]
ts) = let qs :: [Quad]
qs = (Triple -> Quad) -> [Triple] -> [Quad]
forall a b. (a -> b) -> [a] -> [b]
map (\Triple
t -> Triple -> Maybe IRI -> Quad
Quad Triple
t (IRI -> Maybe IRI
forall a. a -> Maybe a
Just IRI
g)) [Triple]
ts
in [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ (Quad -> Builder) -> [Quad] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map Quad -> Builder
encodeQuad [Quad]
qs
encodeRDFGraphs :: Foldable f => f RDFGraph -> B.Builder
encodeRDFGraphs :: f RDFGraph -> Builder
encodeRDFGraphs = (RDFGraph -> Builder) -> f RDFGraph -> Builder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap RDFGraph -> Builder
encodeRDFGraph