coco-1.0.1: Coco.hs
module Coco where
import Data.Either.HT (maybeRight)
import Data.Word (Word8, Word16, Word32, Word64)
import Data.Int (Int8, Int16, Int32, Int64)
import qualified Data.Vector as Vector
import Data.Vector (Vector)
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.UTF8 as B
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BS
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.Encoding as T
import qualified Data.Text as TS
import qualified Data.Text.Encoding as TS
import Data.UUID (UUID)
import qualified Data.UUID as UUID
import Text.Read (readMaybe)
-- | convert a to b
class To b a where
to :: a -> b
-- | swapped type arguments for convenience
from :: forall a b. To b a => a -> b
from = to
instance To a a where
to = id
instance To String Word where to = show
instance To String Word8 where to = show
instance To String Word16 where to = show
instance To String Word32 where to = show
instance To String Word64 where to = show
instance To String Int where to = show
instance To String Int8 where to = show
instance To String Int16 where to = show
instance To String Int32 where to = show
instance To String Int64 where to = show
instance To String B.ByteString where to = B.toString
instance To String BS.ByteString where to = BS.toString
instance To String T.Text where to = T.unpack
instance To String TS.Text where to = TS.unpack
instance To String UUID where to = UUID.toString
instance To B.ByteString String where to = B.fromString
instance To B.ByteString BS.ByteString where to = B.fromStrict
instance To B.ByteString T.Text where to = T.encodeUtf8
instance To B.ByteString TS.Text where to = to . to @T.Text
instance To B.ByteString UUID where to = UUID.toLazyASCIIBytes
instance To BS.ByteString String where to = BS.fromString
instance To BS.ByteString B.ByteString where to = B.toStrict
instance To BS.ByteString T.Text where to = to . to @B.ByteString
instance To BS.ByteString TS.Text where to = TS.encodeUtf8
instance To BS.ByteString UUID where to = UUID.toASCIIBytes
instance To T.Text Word where to = to . to @String
instance To T.Text Word8 where to = to . to @String
instance To T.Text Word16 where to = to . to @String
instance To T.Text Word32 where to = to . to @String
instance To T.Text Word64 where to = to . to @String
instance To T.Text Int where to = to . to @String
instance To T.Text Int8 where to = to . to @String
instance To T.Text Int16 where to = to . to @String
instance To T.Text Int32 where to = to . to @String
instance To T.Text Int64 where to = to . to @String
instance To T.Text String where to = T.pack
instance To T.Text B.ByteString where to = T.decodeUtf8Lenient
instance To T.Text BS.ByteString where to = to . to @B.ByteString
instance To T.Text TS.Text where to = T.fromStrict
instance To T.Text UUID where to = to . to @TS.Text
instance To TS.Text Word where to = to . to @String
instance To TS.Text Word8 where to = to . to @String
instance To TS.Text Word16 where to = to . to @String
instance To TS.Text Word32 where to = to . to @String
instance To TS.Text Word64 where to = to . to @String
instance To TS.Text Int where to = to . to @String
instance To TS.Text Int8 where to = to . to @String
instance To TS.Text Int16 where to = to . to @String
instance To TS.Text Int32 where to = to . to @String
instance To TS.Text Int64 where to = to . to @String
instance To TS.Text String where to = TS.pack
instance To TS.Text B.ByteString where to = to . to @T.Text
instance To TS.Text BS.ByteString where to = TS.decodeUtf8Lenient
instance To TS.Text T.Text where to = T.toStrict
instance To TS.Text UUID where to = UUID.toText
instance To (Maybe Word) String where to = readMaybe -- TODO: overflow protection
instance To (Maybe Word) T.Text where to = to . to @String
instance To (Maybe Word) TS.Text where to = to . to @String
instance To (Maybe T.Text) B.ByteString where to = maybeRight . T.decodeUtf8'
instance To (Maybe T.Text) BS.ByteString where to = to . to @B.ByteString
instance To (Maybe TS.Text) B.ByteString where to = fmap to . to @(Maybe T.Text)
instance To (Maybe TS.Text) BS.ByteString where to = maybeRight . TS.decodeUtf8'
instance To (Maybe UUID) String where to = UUID.fromString
instance To (Maybe UUID) B.ByteString where to = UUID.fromLazyASCIIBytes
instance To (Maybe UUID) BS.ByteString where to = UUID.fromASCIIBytes
instance To (Maybe UUID) T.Text where to = to . to @B.ByteString
instance To (Maybe UUID) TS.Text where to = UUID.fromText
instance To b a => To [b] (Vector a) where
to = fmap to . Vector.toList
instance To b a => To (Vector b) [a] where
to = Vector.fromList . fmap to