{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE RankNTypes           #-}
{-# LANGUAGE TypeApplications     #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Text.XML.Hexml.Lens
  ( -- * Nodes
    _children
  , XML(..)
  , node
  , multiple
  ) where

import           Control.Lens               hiding (children)
import qualified Data.ByteString            as Strict
import qualified Data.ByteString.Internal   as Strict
import qualified Data.ByteString.Lazy       as Lazy
import           Data.ByteString.Lens
import           Data.Functor.Contravariant
import           Data.Profunctor.Unsafe
import           Data.String
import qualified Data.Text                  as Strict
import qualified Data.Text.Encoding         as Strict
import qualified Data.Text.Lazy             as Lazy
import qualified Data.Text.Lazy.Encoding    as Lazy
import           Data.Text.Lens
import qualified Foundation                 as F
import qualified Foundation.Array.Internal  as F
import qualified Foundation.String          as F
import           Text.XML.Hexml

-- | Getter for the element children
_children :: Getter Node [Node]
_children :: ([Node] -> f [Node]) -> Node -> f Node
_children = (Node -> [Node]) -> ([Node] -> f [Node]) -> Node -> f Node
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Node -> [Node]
children

class XML s where
  -- | A prism for parsing and unparsing XML.
  --
  -- unparsing is provided by 'outer'.
  --
  -- >>> "<?xml version=\"1.0\"?><foo/>" ^? _XML
  -- Just Node "<?xml version=\"1.0\"?><foo/>"
  --
  -- Nameless nodes are inserted for trees with >1 root.
  --
  -- >>> "<?xml version=\"1.0\"?><foo/>" ^? _XML.to name
  -- Just ""
  --
  -- >>> "<?xml version=\"1.0\"?><foo/>" ^? _XML._children.ix(0)
  -- Just Node "<?xml version=\"1.0\"?>"
  --
  -- >>> "<?xml version=\"1.0\"?><foo/>" ^? _XML._children.ix(1)
  -- Just Node "<foo/>"
  --
  -- If the tree has only 1 root, no nameless nodes are inserted.
  --
  -- >>> "<foo/>" ^? _XML.re(_XML @String)._XML.to name
  -- Just "foo"
  --
  --   The law @x ^? re _XML . _XML == x@ doesn't hold for the nameless nodes
  --   injected by 'parse'.
  --
  -- >>> parse "<foo/>" ^? _Right.to name
  -- Just ""
  -- >>> parse "<foo/>" ^? _Right.re(_XML @String)._XML.to name
  -- Just "foo"
  _XML :: Prism' s Node

  -- | Fold over all the children (text and element)
  _contents :: Fold Node (Either s Node)
  -- | Getter for the 'inner' contents of a node
  _inner :: Getter Node s
  -- | Getter for the 'inner' contents of a node
  _outer :: Getter Node s
  -- | Fold for accessing the text contents of a node
  textContents :: Fold Node s
  -- | Fold for accessing attributes by name.
  _Attribute  :: s -> Getter Node (Maybe s)
  -- | Name-Indexed fold over the attribute values
  iattributes :: IndexedFold String Node s
  -- | A getter for accessing named children nodes
  --   This is a more efficient version of
  --
  -- > nodes foo = _children . to (filter (\n -> name n == foo))
  nodes     :: s -> Getter Node [Node]

-- | > node n = nodes n . folded
node :: XML s => s -> Fold Node Node
node :: s -> Fold Node Node
node s
n = s -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes s
n (([Node] -> f [Node]) -> Node -> f Node)
-> ((Node -> f Node) -> [Node] -> f [Node])
-> (Node -> f Node)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Node -> f Node) -> [Node] -> f [Node]
forall (f :: * -> *) a. Foldable f => IndexedFold Int (f a) a
folded

instance XML String where
  _XML :: p Node (f Node) -> p String (f String)
_XML = p ByteString (f ByteString) -> p String (f String)
Iso' String ByteString
strictUtf8 (p ByteString (f ByteString) -> p String (f String))
-> (p Node (f Node) -> p ByteString (f ByteString))
-> p Node (f Node)
-> p String (f String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XML ByteString => Prism' ByteString Node
forall s. XML s => Prism' s Node
_XML @Strict.ByteString
  _contents :: (Either String Node -> f (Either String Node)) -> Node -> f Node
_contents = (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
forall s. XML s => Fold Node (Either s Node)
_contents ((Either ByteString Node -> f (Either ByteString Node))
 -> Node -> f Node)
-> ((Either String Node -> f (Either String Node))
    -> Either ByteString Node -> f (Either ByteString Node))
-> (Either String Node -> f (Either String Node))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso ByteString ByteString String String
-> Iso
     (Either ByteString Node)
     (Either ByteString Node)
     (Either String Node)
     (Either String Node)
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b x y.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting (AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8)
  _inner :: (String -> f String) -> Node -> f Node
_inner = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_inner ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8
  _outer :: (String -> f String) -> Node -> f Node
_outer = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_outer ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8
  textContents :: (String -> f String) -> Node -> f Node
textContents = XML ByteString => Fold Node ByteString
forall s. XML s => Fold Node s
textContents @Strict.ByteString ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8
  _Attribute :: String -> Getter Node (Maybe String)
_Attribute String
n = ByteString -> Getter Node (Maybe ByteString)
forall s. XML s => s -> Getter Node (Maybe s)
_Attribute(String
n String -> Getting ByteString String ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString String ByteString
forall t. IsByteString t => Iso' String t
packedChars)((Maybe ByteString -> f (Maybe ByteString)) -> Node -> f Node)
-> ((Maybe String -> f (Maybe String))
    -> Maybe ByteString -> f (Maybe ByteString))
-> (Maybe String -> f (Maybe String))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.AnIso ByteString ByteString String String
-> Iso
     (Maybe ByteString) (Maybe ByteString) (Maybe String) (Maybe String)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping(AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8)
  iattributes :: p String (f String) -> Node -> f Node
iattributes  = XML ByteString => IndexedFold String Node ByteString
forall s. XML s => IndexedFold String Node s
iattributes @Strict.ByteString (p ByteString (f ByteString) -> Node -> f Node)
-> (p String (f String) -> p ByteString (f ByteString))
-> p String (f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p String (f String) -> p ByteString (f ByteString)
forall t. IsByteString t => Iso' t String
unpackedChars
  nodes :: String -> Getter Node [Node]
nodes String
name_ = ByteString -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes ( String
name_ String -> Getting ByteString String ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString String ByteString
Iso' String ByteString
strictUtf8)

instance XML F.String where
  _XML :: p Node (f Node) -> p String (f String)
_XML = p ByteString (f ByteString) -> p String (f String)
Iso' String ByteString
foundationUtf8 (p ByteString (f ByteString) -> p String (f String))
-> (p Node (f Node) -> p ByteString (f ByteString))
-> p Node (f Node)
-> p String (f String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Node (f Node) -> p ByteString (f ByteString)
forall s. XML s => Prism' s Node
_XML
  _contents :: (Either String Node -> f (Either String Node)) -> Node -> f Node
_contents  = (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
forall s. XML s => Fold Node (Either s Node)
_contents ((Either ByteString Node -> f (Either ByteString Node))
 -> Node -> f Node)
-> ((Either String Node -> f (Either String Node))
    -> Either ByteString Node -> f (Either ByteString Node))
-> (Either String Node -> f (Either String Node))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso ByteString ByteString String String
-> Iso
     (Either ByteString Node)
     (Either ByteString Node)
     (Either String Node)
     (Either String Node)
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b x y.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting (AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
foundationUtf8)
  _inner :: (String -> f String) -> Node -> f Node
_inner = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_inner ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
foundationUtf8
  _outer :: (String -> f String) -> Node -> f Node
_outer = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_outer ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
foundationUtf8
  textContents :: (String -> f String) -> Node -> f Node
textContents = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Fold Node s
textContents ((ByteString -> f ByteString) -> Node -> f Node)
-> ((String -> f String) -> ByteString -> f ByteString)
-> (String -> f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
foundationUtf8
  _Attribute :: String -> Getter Node (Maybe String)
_Attribute String
n = ByteString -> Getter Node (Maybe ByteString)
forall s. XML s => s -> Getter Node (Maybe s)
_Attribute(String
n String -> Getting ByteString String ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString String ByteString
Iso' String ByteString
foundationUtf8)((Maybe ByteString -> f (Maybe ByteString)) -> Node -> f Node)
-> ((Maybe String -> f (Maybe String))
    -> Maybe ByteString -> f (Maybe ByteString))
-> (Maybe String -> f (Maybe String))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.AnIso ByteString ByteString String String
-> Iso
     (Maybe ByteString) (Maybe ByteString) (Maybe String) (Maybe String)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping(AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
foundationUtf8)
  iattributes :: p String (f String) -> Node -> f Node
iattributes  = p String (f String) -> Node -> f Node
forall s. XML s => IndexedFold String Node s
iattributes (p String (f String) -> Node -> f Node)
-> (p String (f String) -> p String (f String))
-> p String (f String)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> p String (f String) -> p String (f String)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to String -> String
forall a. IsString a => String -> a
fromString
  nodes :: String -> Getter Node [Node]
nodes String
name_ = ByteString -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes ( String
name_ String -> Getting ByteString String ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString String ByteString
Iso' String ByteString
foundationUtf8)

instance XML Strict.Text where
  _XML :: p Node (f Node) -> p Text (f Text)
_XML = p ByteString (f ByteString) -> p Text (f Text)
Iso' Text ByteString
strictTextUtf8 (p ByteString (f ByteString) -> p Text (f Text))
-> (p Node (f Node) -> p ByteString (f ByteString))
-> p Node (f Node)
-> p Text (f Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Node (f Node) -> p ByteString (f ByteString)
forall s. XML s => Prism' s Node
_XML
  _contents :: (Either Text Node -> f (Either Text Node)) -> Node -> f Node
_contents  = (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
forall s. XML s => Fold Node (Either s Node)
_contents ((Either ByteString Node -> f (Either ByteString Node))
 -> Node -> f Node)
-> ((Either Text Node -> f (Either Text Node))
    -> Either ByteString Node -> f (Either ByteString Node))
-> (Either Text Node -> f (Either Text Node))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso ByteString ByteString Text Text
-> Iso
     (Either ByteString Node)
     (Either ByteString Node)
     (Either Text Node)
     (Either Text Node)
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b x y.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting (AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
strictTextUtf8)
  _inner :: (Text -> f Text) -> Node -> f Node
_inner = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_inner ((ByteString -> f ByteString) -> Node -> f Node)
-> ((Text -> f Text) -> ByteString -> f ByteString)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
strictTextUtf8
  _outer :: (Text -> f Text) -> Node -> f Node
_outer = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_outer ((ByteString -> f ByteString) -> Node -> f Node)
-> ((Text -> f Text) -> ByteString -> f ByteString)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
strictTextUtf8
  textContents :: (Text -> f Text) -> Node -> f Node
textContents = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Fold Node s
textContents ((ByteString -> f ByteString) -> Node -> f Node)
-> ((Text -> f Text) -> ByteString -> f ByteString)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
strictTextUtf8
  _Attribute :: Text -> Getter Node (Maybe Text)
_Attribute Text
n = ByteString -> Getter Node (Maybe ByteString)
forall s. XML s => s -> Getter Node (Maybe s)
_Attribute(Text
n Text -> Getting ByteString Text ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString Text ByteString
Iso' Text ByteString
strictTextUtf8)((Maybe ByteString -> f (Maybe ByteString)) -> Node -> f Node)
-> ((Maybe Text -> f (Maybe Text))
    -> Maybe ByteString -> f (Maybe ByteString))
-> (Maybe Text -> f (Maybe Text))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.AnIso ByteString ByteString Text Text
-> Iso
     (Maybe ByteString) (Maybe ByteString) (Maybe Text) (Maybe Text)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping(AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
strictTextUtf8)
  iattributes :: p Text (f Text) -> Node -> f Node
iattributes  = p String (f String) -> Node -> f Node
forall s. XML s => IndexedFold String Node s
iattributes (p String (f String) -> Node -> f Node)
-> (p Text (f Text) -> p String (f String))
-> p Text (f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Text (f Text) -> p String (f String)
forall t. IsText t => Iso' String t
packed
  nodes :: Text -> Getter Node [Node]
nodes Text
name_ = ByteString -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes ( Text
name_ Text -> Getting ByteString Text ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString Text ByteString
Iso' Text ByteString
strictTextUtf8 )

instance XML Lazy.Text where
  _XML :: p Node (f Node) -> p Text (f Text)
_XML = p ByteString (f ByteString) -> p Text (f Text)
Iso' Text ByteString
lazyTextUtf8 (p ByteString (f ByteString) -> p Text (f Text))
-> (p Node (f Node) -> p ByteString (f ByteString))
-> p Node (f Node)
-> p Text (f Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Node (f Node) -> p ByteString (f ByteString)
forall s. XML s => Prism' s Node
_XML
  _contents :: (Either Text Node -> f (Either Text Node)) -> Node -> f Node
_contents  = (Either Text Node -> f (Either Text Node)) -> Node -> f Node
forall s. XML s => Fold Node (Either s Node)
_contents ((Either Text Node -> f (Either Text Node)) -> Node -> f Node)
-> ((Either Text Node -> f (Either Text Node))
    -> Either Text Node -> f (Either Text Node))
-> (Either Text Node -> f (Either Text Node))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso Text Text Text Text
-> Iso
     (Either Text Node)
     (Either Text Node)
     (Either Text Node)
     (Either Text Node)
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b x y.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting AnIso Text Text Text Text
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  _inner :: (Text -> f Text) -> Node -> f Node
_inner = (Text -> f Text) -> Node -> f Node
forall s. XML s => Getter Node s
_inner ((Text -> f Text) -> Node -> f Node)
-> ((Text -> f Text) -> Text -> f Text)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> f Text) -> Text -> f Text
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  _outer :: (Text -> f Text) -> Node -> f Node
_outer = (Text -> f Text) -> Node -> f Node
forall s. XML s => Getter Node s
_outer ((Text -> f Text) -> Node -> f Node)
-> ((Text -> f Text) -> Text -> f Text)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> f Text) -> Text -> f Text
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  textContents :: (Text -> f Text) -> Node -> f Node
textContents = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Fold Node s
textContents ((ByteString -> f ByteString) -> Node -> f Node)
-> ((Text -> f Text) -> ByteString -> f ByteString)
-> (Text -> f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
lazyTextUtf8
  _Attribute :: Text -> Getter Node (Maybe Text)
_Attribute Text
n = ByteString -> Getter Node (Maybe ByteString)
forall s. XML s => s -> Getter Node (Maybe s)
_Attribute(Text
n Text -> Getting ByteString Text ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString Text ByteString
Iso' Text ByteString
lazyTextUtf8)((Maybe ByteString -> f (Maybe ByteString)) -> Node -> f Node)
-> ((Maybe Text -> f (Maybe Text))
    -> Maybe ByteString -> f (Maybe ByteString))
-> (Maybe Text -> f (Maybe Text))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.AnIso ByteString ByteString Text Text
-> Iso
     (Maybe ByteString) (Maybe ByteString) (Maybe Text) (Maybe Text)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping(AnIso Text Text ByteString ByteString
-> Iso ByteString ByteString Text Text
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso Text Text ByteString ByteString
Iso' Text ByteString
lazyTextUtf8)
  iattributes :: p Text (f Text) -> Node -> f Node
iattributes  = p String (f String) -> Node -> f Node
forall s. XML s => IndexedFold String Node s
iattributes (p String (f String) -> Node -> f Node)
-> (p Text (f Text) -> p String (f String))
-> p Text (f Text)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Text (f Text) -> p String (f String)
forall t. IsText t => Iso' String t
packed
  nodes :: Text -> Getter Node [Node]
nodes Text
name_ = ByteString -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes ( Text
name_ Text -> Getting ByteString Text ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString Text ByteString
Iso' Text ByteString
lazyTextUtf8 )

instance XML Strict.ByteString where
  _XML :: p Node (f Node) -> p ByteString (f ByteString)
_XML = (Node -> ByteString)
-> (ByteString -> Maybe Node) -> Prism' ByteString Node
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Node -> ByteString
outer ByteString -> Maybe Node
doParse where
    doParse :: ByteString -> Maybe Node
doParse ByteString
x =
      case ByteString -> Either ByteString Node
parse ByteString
x of
        Right Node
n -> Node -> Maybe Node
forall a. a -> Maybe a
Just (Node -> Maybe Node) -> Node -> Maybe Node
forall a b. (a -> b) -> a -> b
$ case Node -> [Node]
children Node
n of [Node
y] -> Node
y ; [Node]
_ -> Node
n
        Left  ByteString
_ -> Maybe Node
forall a. Maybe a
Nothing
  _contents :: (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
_contents  = (Node -> [Either ByteString Node])
-> Fold Node (Either ByteString Node)
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding Node -> [Either ByteString Node]
contents
  _inner :: (ByteString -> f ByteString) -> Node -> f Node
_inner = (Node -> ByteString)
-> (ByteString -> f ByteString) -> Node -> f Node
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Node -> ByteString
inner
  _outer :: (ByteString -> f ByteString) -> Node -> f Node
_outer = (Node -> ByteString)
-> (ByteString -> f ByteString) -> Node -> f Node
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Node -> ByteString
outer
  textContents :: (ByteString -> f ByteString) -> Node -> f Node
textContents = (Node -> [Either ByteString Node])
-> Fold Node (Either ByteString Node)
forall (f :: * -> *) s a. Foldable f => (s -> f a) -> Fold s a
folding Node -> [Either ByteString Node]
contents ((Either ByteString Node -> f (Either ByteString Node))
 -> Node -> f Node)
-> ((ByteString -> f ByteString)
    -> Either ByteString Node -> f (Either ByteString Node))
-> (ByteString -> f ByteString)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> f ByteString)
-> Either ByteString Node -> f (Either ByteString Node)
forall a c b. Prism (Either a c) (Either b c) a b
_Left
  _Attribute :: ByteString -> Getter Node (Maybe ByteString)
_Attribute ByteString
n = Getting (First ByteString) Node ByteString
-> IndexPreservingGetter Node (Maybe ByteString)
forall a s.
Getting (First a) s a -> IndexPreservingGetter s (Maybe a)
pre (Getting (First ByteString) Node ByteString
 -> IndexPreservingGetter Node (Maybe ByteString))
-> Getting (First ByteString) Node ByteString
-> IndexPreservingGetter Node (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ (Node -> Maybe Attribute)
-> Optic' (->) (Const (First ByteString)) Node (Maybe Attribute)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to(Node -> ByteString -> Maybe Attribute
`attributeBy` ByteString
n)Optic' (->) (Const (First ByteString)) Node (Maybe Attribute)
-> ((ByteString -> Const (First ByteString) ByteString)
    -> Maybe Attribute -> Const (First ByteString) (Maybe Attribute))
-> Getting (First ByteString) Node ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Attribute -> Const (First ByteString) Attribute)
-> Maybe Attribute -> Const (First ByteString) (Maybe Attribute)
forall (f :: * -> *) a. Foldable f => IndexedFold Int (f a) a
folded((Attribute -> Const (First ByteString) Attribute)
 -> Maybe Attribute -> Const (First ByteString) (Maybe Attribute))
-> ((ByteString -> Const (First ByteString) ByteString)
    -> Attribute -> Const (First ByteString) Attribute)
-> (ByteString -> Const (First ByteString) ByteString)
-> Maybe Attribute
-> Const (First ByteString) (Maybe Attribute)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Attribute -> ByteString)
-> (ByteString -> Const (First ByteString) ByteString)
-> Attribute
-> Const (First ByteString) Attribute
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to(Attribute -> ByteString
attributeValue)
  iattributes :: p ByteString (f ByteString) -> Node -> f Node
iattributes  = (Node -> [(String, ByteString)])
-> p ByteString (f ByteString) -> Node -> f Node
forall (f :: * -> *) i (p :: * -> * -> *) (g :: * -> *) s a t b.
(Foldable f, Indexable i p, Contravariant g, Applicative g) =>
(s -> f (i, a)) -> Over p g s t a b
ifolding ((Attribute -> (String, ByteString))
-> [Attribute] -> [(String, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (\ (Attribute ByteString
n ByteString
v) -> (ByteString
nByteString -> Getting String ByteString String -> String
forall s a. s -> Getting a s a -> a
^.AnIso String String ByteString ByteString
-> Iso ByteString ByteString String String
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso String String ByteString ByteString
Iso' String ByteString
strictUtf8, ByteString
v)) ([Attribute] -> [(String, ByteString)])
-> (Node -> [Attribute]) -> Node -> [(String, ByteString)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Node -> [Attribute]
attributes )
  nodes :: ByteString -> Getter Node [Node]
nodes ByteString
name_ = (Node -> [Node]) -> Optic' (->) f Node [Node]
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to ((Node -> [Node]) -> Optic' (->) f Node [Node])
-> (Node -> [Node]) -> Optic' (->) f Node [Node]
forall a b. (a -> b) -> a -> b
$ (Node -> ByteString -> [Node]) -> ByteString -> Node -> [Node]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Node -> ByteString -> [Node]
childrenBy ByteString
name_

instance XML Lazy.ByteString where
  _XML :: p Node (f Node) -> p ByteString (f ByteString)
_XML = p ByteString (f ByteString) -> p ByteString (f ByteString)
forall lazy strict. Strict lazy strict => Iso' lazy strict
strict (p ByteString (f ByteString) -> p ByteString (f ByteString))
-> (p Node (f Node) -> p ByteString (f ByteString))
-> p Node (f Node)
-> p ByteString (f ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XML ByteString => Prism' ByteString Node
forall s. XML s => Prism' s Node
_XML @Strict.ByteString
  _contents :: (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
_contents  = (Either ByteString Node -> f (Either ByteString Node))
-> Node -> f Node
forall s. XML s => Fold Node (Either s Node)
_contents ((Either ByteString Node -> f (Either ByteString Node))
 -> Node -> f Node)
-> ((Either ByteString Node -> f (Either ByteString Node))
    -> Either ByteString Node -> f (Either ByteString Node))
-> (Either ByteString Node -> f (Either ByteString Node))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso ByteString ByteString ByteString ByteString
-> Iso
     (Either ByteString Node)
     (Either ByteString Node)
     (Either ByteString Node)
     (Either ByteString Node)
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b x y.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting AnIso ByteString ByteString ByteString ByteString
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  _inner :: (ByteString -> f ByteString) -> Node -> f Node
_inner = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_inner ((ByteString -> f ByteString) -> Node -> f Node)
-> ((ByteString -> f ByteString) -> ByteString -> f ByteString)
-> (ByteString -> f ByteString)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> f ByteString) -> ByteString -> f ByteString
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  _outer :: (ByteString -> f ByteString) -> Node -> f Node
_outer = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Getter Node s
_outer ((ByteString -> f ByteString) -> Node -> f Node)
-> ((ByteString -> f ByteString) -> ByteString -> f ByteString)
-> (ByteString -> f ByteString)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> f ByteString) -> ByteString -> f ByteString
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  textContents :: (ByteString -> f ByteString) -> Node -> f Node
textContents = (ByteString -> f ByteString) -> Node -> f Node
forall s. XML s => Fold Node s
textContents ((ByteString -> f ByteString) -> Node -> f Node)
-> ((ByteString -> f ByteString) -> ByteString -> f ByteString)
-> (ByteString -> f ByteString)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> f ByteString) -> ByteString -> f ByteString
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  _Attribute :: ByteString -> Getter Node (Maybe ByteString)
_Attribute ByteString
n = ByteString -> Getter Node (Maybe ByteString)
forall s. XML s => s -> Getter Node (Maybe s)
_Attribute(ByteString
nByteString
-> Getting ByteString ByteString ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^.Getting ByteString ByteString ByteString
forall lazy strict. Strict lazy strict => Iso' lazy strict
strict)((Maybe ByteString -> f (Maybe ByteString)) -> Node -> f Node)
-> ((Maybe ByteString -> f (Maybe ByteString))
    -> Maybe ByteString -> f (Maybe ByteString))
-> (Maybe ByteString -> f (Maybe ByteString))
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.AnIso ByteString ByteString ByteString ByteString
-> Iso
     (Maybe ByteString)
     (Maybe ByteString)
     (Maybe ByteString)
     (Maybe ByteString)
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping(AnIso ByteString ByteString ByteString ByteString
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy)
  iattributes :: p ByteString (f ByteString) -> Node -> f Node
iattributes = p ByteString (f ByteString) -> Node -> f Node
forall s. XML s => IndexedFold String Node s
iattributes(p ByteString (f ByteString) -> Node -> f Node)
-> (p ByteString (f ByteString) -> p ByteString (f ByteString))
-> p ByteString (f ByteString)
-> Node
-> f Node
forall b c a. (b -> c) -> (a -> b) -> a -> c
.p ByteString (f ByteString) -> p ByteString (f ByteString)
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy
  nodes :: ByteString -> Getter Node [Node]
nodes ByteString
name_ = ByteString -> Getter Node [Node]
forall s. XML s => s -> Getter Node [Node]
nodes (ByteString
name_ ByteString
-> Getting ByteString ByteString ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString ByteString ByteString
forall lazy strict. Strict lazy strict => Iso' lazy strict
strict)

lazyTextUtf8 :: Iso' Lazy.Text Lazy.ByteString
lazyTextUtf8 :: p ByteString (f ByteString) -> p Text (f Text)
lazyTextUtf8 = (Text -> ByteString)
-> (ByteString -> Text) -> Iso' Text ByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> ByteString
Lazy.encodeUtf8 ByteString -> Text
Lazy.decodeUtf8

strictTextUtf8 :: Iso' Strict.Text Strict.ByteString
strictTextUtf8 :: p ByteString (f ByteString) -> p Text (f Text)
strictTextUtf8 = (Text -> ByteString)
-> (ByteString -> Text) -> Iso' Text ByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> ByteString
Strict.encodeUtf8 ByteString -> Text
Strict.decodeUtf8

strictUtf8 :: Iso' String Strict.ByteString
strictUtf8 :: p ByteString (f ByteString) -> p String (f String)
strictUtf8 = p Text (f Text) -> p String (f String)
forall t. IsText t => Iso' String t
packed (p Text (f Text) -> p String (f String))
-> (p ByteString (f ByteString) -> p Text (f Text))
-> p ByteString (f ByteString)
-> p String (f String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p ByteString (f ByteString) -> p Text (f Text)
Iso' Text ByteString
strictTextUtf8

foundationUtf8 :: Iso' F.String Strict.ByteString
foundationUtf8 :: p ByteString (f ByteString) -> p String (f String)
foundationUtf8 = (String -> ByteString)
-> (ByteString -> String) -> Iso' String ByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso String -> ByteString
toByteString ByteString -> String
fromByteString
  where
    toByteString :: String -> ByteString
toByteString = [Word8] -> ByteString
Strict.packBytes ([Word8] -> ByteString)
-> (String -> [Word8]) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UArray Word8 -> [Word8]
forall l. IsList l => l -> [Item l]
F.toList (UArray Word8 -> [Word8])
-> (String -> UArray Word8) -> String -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> String -> UArray Word8
F.toBytes Encoding
F.UTF8
    fromByteString :: ByteString -> String
fromByteString = Getting
  String (String, Maybe ValidationFailure, UArray Word8) String
-> (String, Maybe ValidationFailure, UArray Word8) -> String
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting
  String (String, Maybe ValidationFailure, UArray Word8) String
forall s t a b. Field1 s t a b => Lens s t a b
_1 ((String, Maybe ValidationFailure, UArray Word8) -> String)
-> (ByteString -> (String, Maybe ValidationFailure, UArray Word8))
-> ByteString
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding
-> UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8)
F.fromBytes Encoding
F.UTF8 (UArray Word8 -> (String, Maybe ValidationFailure, UArray Word8))
-> (ByteString -> UArray Word8)
-> ByteString
-> (String, Maybe ValidationFailure, UArray Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ForeignPtr Word8, Int, Int) -> UArray Word8
forall ty. PrimType ty => (ForeignPtr ty, Int, Int) -> UArray ty
F.fromForeignPtr ((ForeignPtr Word8, Int, Int) -> UArray Word8)
-> (ByteString -> (ForeignPtr Word8, Int, Int))
-> ByteString
-> UArray Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> (ForeignPtr Word8, Int, Int)
Strict.toForeignPtr

multiple :: Getting [a] s a -> IndexPreservingGetter s [a]
multiple :: Getting [a] s a -> IndexPreservingGetter s [a]
multiple Getting [a] s a
l = (s -> [a]) -> (f [a] -> f s) -> p [a] (f [a]) -> p s (f s)
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (Const [a] s -> [a]
forall a k (b :: k). Const a b -> a
getConst (Const [a] s -> [a]) -> (s -> Const [a] s) -> s -> [a]
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Getting [a] s a
l ([a] -> Const [a] a
forall k a (b :: k). a -> Const a b
Const ([a] -> Const [a] a) -> (a -> [a]) -> a -> Const [a] a
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[]))) f [a] -> f s
forall (f :: * -> *) a b.
(Functor f, Contravariant f) =>
f a -> f b
phantom

-- Test setup
-- ---------------------------------------------------------------------------------
-- $setup
-- >>> import Test.QuickCheck
-- >>> :set -XTypeApplications
-- >>> :set -XOverloadedStrings