module Iri.Parsing.Text
  ( iri,
    httpIri,
    hierarchy,
    scheme,
    host,
    regName,
    domainLabel,
    port,
    path,
    pathSegment,
    query,
    fragment,
  )
where

import qualified Data.Attoparsec.Text as B
import Iri.Data
import qualified Iri.Parsing.Attoparsec.Text as A
import Iri.Prelude

fromParser :: B.Parser a -> Text -> Either Text a
fromParser :: forall a. Parser a -> Text -> Either Text a
fromParser Parser a
parser =
  (String -> Either Text a)
-> (a -> Either Text a) -> Either String a -> Either Text a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> Either Text a
forall a b. a -> Either a b
Left (Text -> Either Text a)
-> (String -> Text) -> String -> Either Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> Text
forall a. IsString a => String -> a
fromString) a -> Either Text a
forall a b. b -> Either a b
Right
    (Either String a -> Either Text a)
-> (Text -> Either String a) -> Text -> Either Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parser a -> Text -> Either String a
forall a. Parser a -> Text -> Either String a
B.parseOnly (Parser a
parser Parser a -> Parser Text () -> Parser a
forall a b. Parser Text a -> Parser Text b -> Parser Text a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
B.endOfInput)

-- |
-- Parser of a well-formed IRI conforming to the RFC3987 standard into 'Iri'.
-- Performs URL-decoding.
iri :: Text -> Either Text Iri
iri :: Text -> Either Text Iri
iri = Parser Iri -> Text -> Either Text Iri
forall a. Parser a -> Text -> Either Text a
fromParser Parser Iri
A.iri

-- |
-- Same as 'iri', but optimized specifially for the case of HTTP IRIs.
httpIri :: Text -> Either Text HttpIri
httpIri :: Text -> Either Text HttpIri
httpIri = Parser HttpIri -> Text -> Either Text HttpIri
forall a. Parser a -> Text -> Either Text a
fromParser Parser HttpIri
A.httpIri

hierarchy :: Text -> Either Text Hierarchy
hierarchy :: Text -> Either Text Hierarchy
hierarchy = Parser Hierarchy -> Text -> Either Text Hierarchy
forall a. Parser a -> Text -> Either Text a
fromParser Parser Hierarchy
A.hierarchy

scheme :: Text -> Either Text Scheme
scheme :: Text -> Either Text Scheme
scheme = Parser Scheme -> Text -> Either Text Scheme
forall a. Parser a -> Text -> Either Text a
fromParser Parser Scheme
A.scheme

host :: Text -> Either Text Host
host :: Text -> Either Text Host
host = Parser Host -> Text -> Either Text Host
forall a. Parser a -> Text -> Either Text a
fromParser Parser Host
A.host

regName :: Text -> Either Text RegName
regName :: Text -> Either Text RegName
regName = Parser RegName -> Text -> Either Text RegName
forall a. Parser a -> Text -> Either Text a
fromParser Parser RegName
A.regName

domainLabel :: Text -> Either Text DomainLabel
domainLabel :: Text -> Either Text DomainLabel
domainLabel = Parser DomainLabel -> Text -> Either Text DomainLabel
forall a. Parser a -> Text -> Either Text a
fromParser Parser DomainLabel
A.domainLabel

port :: Text -> Either Text Word16
port :: Text -> Either Text Word16
port = Parser Word16 -> Text -> Either Text Word16
forall a. Parser a -> Text -> Either Text a
fromParser Parser Word16
A.port

path :: Text -> Either Text Path
path :: Text -> Either Text Path
path = Parser Path -> Text -> Either Text Path
forall a. Parser a -> Text -> Either Text a
fromParser Parser Path
A.path

pathSegment :: Text -> Either Text PathSegment
pathSegment :: Text -> Either Text PathSegment
pathSegment = Parser PathSegment -> Text -> Either Text PathSegment
forall a. Parser a -> Text -> Either Text a
fromParser Parser PathSegment
A.pathSegment

query :: Text -> Either Text Query
query :: Text -> Either Text Query
query = Parser Query -> Text -> Either Text Query
forall a. Parser a -> Text -> Either Text a
fromParser Parser Query
A.query

fragment :: Text -> Either Text Fragment
fragment :: Text -> Either Text Fragment
fragment = Parser Fragment -> Text -> Either Text Fragment
forall a. Parser a -> Text -> Either Text a
fromParser Parser Fragment
A.fragment