module Data.Convertible.Instances.Ascii
( Ascii
, CIAscii
, AsciiBuilder
) where
import Blaze.ByteString.Builder (Builder)
import Control.Failure
import Data.Ascii (Ascii, CIAscii, AsciiBuilder)
import qualified Data.Ascii as A
import Data.ByteString (ByteString)
import Data.Convertible.Base
import Data.Convertible.Utils
import Data.Text (Text)
import Prelude.Unicode
instance ConvertSuccess Ascii AsciiBuilder where
convertSuccess = A.toAsciiBuilder
instance ConvertSuccess Ascii ByteString where
convertSuccess = A.toByteString
instance ConvertSuccess Ascii CIAscii where
convertSuccess = A.toCIAscii
instance ConvertSuccess Ascii String where
convertSuccess = A.toString
instance ConvertSuccess Ascii Text where
convertSuccess = A.toText
instance ConvertSuccess AsciiBuilder Ascii where
convertSuccess = A.fromAsciiBuilder
instance ConvertSuccess AsciiBuilder Builder where
convertSuccess = A.toBuilder
instance ConvertSuccess AsciiBuilder ByteString where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess AsciiBuilder CIAscii where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess AsciiBuilder String where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess AsciiBuilder Text where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertAttempt ByteString Ascii where
convertAttempt bs
= case A.fromByteString bs of
Just a → return a
Nothing → nonAscii bs
instance ConvertAttempt ByteString AsciiBuilder where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
instance ConvertAttempt ByteString CIAscii where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
instance ConvertSuccess CIAscii Ascii where
convertSuccess = A.fromCIAscii
instance ConvertSuccess CIAscii AsciiBuilder where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess CIAscii ByteString where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess CIAscii String where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertSuccess CIAscii Text where
convertSuccess = convertSuccessVia ((⊥) ∷ Ascii)
instance ConvertAttempt String Ascii where
convertAttempt s
= case A.fromChars s of
Just a → return a
Nothing → nonAscii s
instance ConvertAttempt String AsciiBuilder where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
instance ConvertAttempt String CIAscii where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
instance ConvertAttempt Text Ascii where
convertAttempt t
= case A.fromText t of
Just a → return a
Nothing → nonAscii t
instance ConvertAttempt Text AsciiBuilder where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
instance ConvertAttempt Text CIAscii where
convertAttempt = convertAttemptVia ((⊥) ∷ Ascii)
nonAscii ∷ Failure (ConvertBoundsException Char α) f ⇒ α → f β
nonAscii = failure ∘ ConvertBoundsException '\0' '\x7F'
deriveAttempts [ ([t| Ascii |], [t| AsciiBuilder |])
, ([t| Ascii |], [t| ByteString |])
, ([t| Ascii |], [t| CIAscii |])
, ([t| Ascii |], [t| String |])
, ([t| Ascii |], [t| Text |])
, ([t| AsciiBuilder |], [t| Ascii |])
, ([t| AsciiBuilder |], [t| Builder |])
, ([t| AsciiBuilder |], [t| ByteString |])
, ([t| AsciiBuilder |], [t| CIAscii |])
, ([t| AsciiBuilder |], [t| String |])
, ([t| AsciiBuilder |], [t| Text |])
, ([t| CIAscii |], [t| Ascii |])
, ([t| CIAscii |], [t| AsciiBuilder |])
, ([t| CIAscii |], [t| ByteString |])
, ([t| CIAscii |], [t| String |])
, ([t| CIAscii |], [t| Text |])
]