<commit_before>{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Servant.Utils.Text where

import Data.String.Conversions
import Data.Text
import Text.Read

class FromText a where
  fromText :: Text -> Maybe a

class ToText a where
  toText :: a -> Text


instance FromText Text where
  fromText = Just

instance ToText Text where
  toText = id


instance FromText String where
  fromText = Just . cs

instance ToText String where
  toText = cs


instance FromText Bool where
  fromText "true"  = Just True
  fromText "false" = Just False
  fromText _       = Nothing

instance ToText Bool where
  toText True  = "true"
  toText False = "false"


instance FromText Integer where
  fromText = readMaybe . cs

instance ToText Integer where
  toText = cs . show<commit_msg>add a lot of FromText/ToText instances<commit_after>{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Servant.Utils.Text where

import Data.String.Conversions
import Data.Int
import Data.Text
import Data.Text.Read
import Data.Word

-- | For getting values from url captures, get parameters
class FromText a where
  fromText :: Text -> Maybe a

-- | For putting values in paths, get parameters
class ToText a where
  toText :: a -> Text

instance FromText Text where
  fromText = Just

instance ToText Text where
  toText = id

instance FromText String where
  fromText = Just . cs

instance ToText String where
  toText = cs

instance FromText Bool where
  fromText "true"  = Just True
  fromText "false" = Just False
  fromText _       = Nothing

instance ToText Bool where
  toText True  = "true"
  toText False = "false"

instance FromText Int where
  fromText = runReader (signed decimal)

instance ToText Int where
  toText = cs . show

instance FromText Int8 where
  fromText = runReader (signed decimal)

instance ToText Int8 where
  toText = cs . show

instance FromText Int16 where
  fromText = runReader (signed decimal)

instance ToText Int16 where
  toText = cs . show

instance FromText Int32 where
  fromText = runReader (signed decimal)

instance ToText Int32 where
  toText = cs . show

instance FromText Int64 where
  fromText = runReader (signed decimal)

instance ToText Int64 where
  toText = cs . show

instance FromText Word where
  fromText = runReader decimal

instance ToText Word where
  toText = cs . show

instance FromText Word8 where
  fromText = runReader decimal

instance ToText Word8 where
  toText = cs . show

instance FromText Word16 where
  fromText = runReader decimal

instance ToText Word16 where
  toText = cs . show

instance FromText Word32 where
  fromText = runReader decimal

instance ToText Word32 where
  toText = cs . show

instance FromText Word64 where
  fromText = runReader decimal

instance ToText Word64 where
  toText = cs . show

instance FromText Integer where
  fromText = runReader decimal

instance ToText Integer where
  toText = cs . show

instance FromText Double where
  fromText = runReader rational

instance ToText Double where
  toText = cs . show

instance FromText Float where
  fromText = runReader rational

instance ToText Float where
  toText = cs . show

runReader :: Reader a -> Text -> Maybe a
runReader reader t = either (const Nothing) (Just . fst) $ reader t
{'fraction_non_alphanumeric': 0.055089820359281436, 'fraction_numerical': 0.008383233532934131, 'mean_word_length': 3.666201117318436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6927907', 'n_tokens_mistral': 1003, 'n_tokens_neox': 976, 'n_words': 400}