<commit_before>{-# LANGUAGE FlexibleInstances, OverlappingInstances, TypeSynonymInstances,
    OverloadedStrings #-}

module Database.Redis.Types where

import Control.Applicative
import Data.ByteString.Char8 (ByteString, pack)
import Data.ByteString.Lex.Double (readDouble)

import Database.Redis.Protocol


------------------------------------------------------------------------------
-- Classes of types Redis understands
--
class RedisArg a where
    encode :: a -> ByteString

class RedisResult a where
    decode :: Reply -> Either Reply a

------------------------------------------------------------------------------
-- RedisArg instances
--
instance RedisArg ByteString where
    encode = id

instance RedisArg Integer where
    encode = pack . show

instance RedisArg Double where
    encode = pack . show

------------------------------------------------------------------------------
-- RedisResult instances
--
data Status = Ok | Pong | Status ByteString
    deriving (Show, Eq)

data RedisType = None | String | Hash | List | Set | ZSet
    deriving (Show, Eq)

instance RedisResult Reply where
    decode = Right

instance RedisResult ByteString where
    decode (SingleLine s)  = Right s
    decode (Bulk (Just s)) = Right s
    decode r               = Left r

instance RedisResult Integer where
    decode (Integer n) = Right n
    decode r           = Left r

instance RedisResult Int where
    decode = fmap fromInteger . decode

instance RedisResult Double where
    decode r = maybe (Left r) (Right . fst) . readDouble =<< decode r

instance RedisResult Status where
    decode (SingleLine s) = Right $ case s of
        "OK"     -> Ok
        "PONG"   -> Pong
        _        -> Status s
    decode r = Left r
    <commit_msg>Decode Integer from Bulk replies.<commit_after>{-# LANGUAGE FlexibleInstances, OverlappingInstances, TypeSynonymInstances,
    OverloadedStrings #-}

module Database.Redis.Types where

import Control.Applicative
import Data.ByteString.Char8 (ByteString, pack)
import Data.ByteString.Lex.Double (readDouble)
import Data.ByteString.Lex.Integral (readSigned, readDecimal)

import Database.Redis.Protocol


------------------------------------------------------------------------------
-- Classes of types Redis understands
--
class RedisArg a where
    encode :: a -> ByteString

class RedisResult a where
    decode :: Reply -> Either Reply a

------------------------------------------------------------------------------
-- RedisArg instances
--
instance RedisArg ByteString where
    encode = id

instance RedisArg Integer where
    encode = pack . show

instance RedisArg Double where
    encode = pack . show

------------------------------------------------------------------------------
-- RedisResult instances
--
data Status = Ok | Pong | Status ByteString
    deriving (Show, Eq)

data RedisType = None | String | Hash | List | Set | ZSet
    deriving (Show, Eq)

instance RedisResult Reply where
    decode = Right

instance RedisResult ByteString where
    decode (SingleLine s)  = Right s
    decode (Bulk (Just s)) = Right s
    decode r               = Left r

instance RedisResult Integer where
    decode (Integer n) = Right n
    decode r           =
        maybe (Left r) (Right . fst) . readSigned readDecimal =<< decode r

instance RedisResult Int where
    decode = fmap fromInteger . decode

instance RedisResult Double where
    decode r = maybe (Left r) (Right . fst) . readDouble =<< decode r

instance RedisResult Status where
    decode (SingleLine s) = Right $ case s of
        "OK"     -> Ok
        "PONG"   -> Pong
        _        -> Status s
    decode r = Left r
    
{'fraction_non_alphanumeric': 0.19157088122605365, 'fraction_numerical': 0.0005473453749315818, 'mean_word_length': 1.8007662835249043, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1047793', 'n_tokens_mistral': 946, 'n_tokens_neox': 927, 'n_words': 347}