4 , GeneralizedNewtypeDeriving
5 , MultiParamTypeClasses
11 module Data.URI.Internal.Scheme
15 import Data.Ascii (AsciiBuilder, CIAscii)
16 import qualified Data.Ascii as A
17 import Data.Attoparsec.Char8
18 import qualified Data.ByteString.Char8 as BS
19 import Data.CaseInsensitive
20 import Data.Convertible.Base
24 import Data.URI.Internal
26 import Prelude hiding (takeWhile)
27 import Prelude.Unicode
29 -- |'Scheme' names consist of a non-empty sequence of characters
30 -- beginning with a letter and followed by any combination of letters,
31 -- digits, plus (\'+\'), period (\'.\'), or hyphen (\'-\'):
32 -- <http://tools.ietf.org/html/rfc3986#section-3.1>
33 newtype Scheme = Scheme CIAscii
42 -- |'isString' is a fast but unsafe way to create 'Scheme' such that
43 -- no validation on the string is performed.
44 deriving instance IsString Scheme
46 instance Default (Parser Scheme) where
47 {-# INLINEABLE def #-}
48 def = do x ← satisfy first
49 xs ← takeWhile nonFirst
50 return ∘ fromBS $ x `BS.cons` xs
56 {-# INLINE nonFirst #-}
64 fromBS = Scheme ∘ A.toCIAscii ∘ A.unsafeFromByteString
66 instance ConvertSuccess Scheme CIAscii where
67 {-# INLINE convertSuccess #-}
68 convertSuccess (Scheme s) = foldCase s
70 instance ConvertSuccess Scheme AsciiBuilder where
71 {-# INLINE convertSuccess #-}
72 convertSuccess = A.toAsciiBuilder ∘ A.fromCIAscii ∘ cs
74 instance ConvertAttempt CIAscii Scheme where
75 {-# INLINE convertAttempt #-}
76 convertAttempt = parseAttempt' def ∘ A.fromCIAscii
78 deriveAttempts [ ([t| Scheme |], [t| AsciiBuilder |])
79 , ([t| Scheme |], [t| CIAscii |])