{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Web.Scim.AttrName where
import Data.Aeson.Types (FromJSONKey, ToJSONKey)
import Data.Attoparsec.ByteString.Char8
import qualified Data.CaseInsensitive as CI
import Data.Hashable
import Data.String (IsString, fromString)
import Data.Text (Text, cons)
import Data.Text.Encoding (decodeUtf8)
import Prelude hiding (takeWhile)
newtype AttrName
= AttrName Text
deriving (Int -> AttrName -> ShowS
[AttrName] -> ShowS
AttrName -> String
(Int -> AttrName -> ShowS)
-> (AttrName -> String) -> ([AttrName] -> ShowS) -> Show AttrName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AttrName -> ShowS
showsPrec :: Int -> AttrName -> ShowS
$cshow :: AttrName -> String
show :: AttrName -> String
$cshowList :: [AttrName] -> ShowS
showList :: [AttrName] -> ShowS
Show, FromJSONKeyFunction [AttrName]
FromJSONKeyFunction AttrName
FromJSONKeyFunction AttrName
-> FromJSONKeyFunction [AttrName] -> FromJSONKey AttrName
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
$cfromJSONKey :: FromJSONKeyFunction AttrName
fromJSONKey :: FromJSONKeyFunction AttrName
$cfromJSONKeyList :: FromJSONKeyFunction [AttrName]
fromJSONKeyList :: FromJSONKeyFunction [AttrName]
FromJSONKey, ToJSONKeyFunction [AttrName]
ToJSONKeyFunction AttrName
ToJSONKeyFunction AttrName
-> ToJSONKeyFunction [AttrName] -> ToJSONKey AttrName
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
$ctoJSONKey :: ToJSONKeyFunction AttrName
toJSONKey :: ToJSONKeyFunction AttrName
$ctoJSONKeyList :: ToJSONKeyFunction [AttrName]
toJSONKeyList :: ToJSONKeyFunction [AttrName]
ToJSONKey)
instance Eq AttrName where
AttrName Text
a == :: AttrName -> AttrName -> Bool
== AttrName Text
b = Text -> Text
forall s. FoldCase s => s -> s
CI.foldCase Text
a Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Text
forall s. FoldCase s => s -> s
CI.foldCase Text
b
instance Ord AttrName where
compare :: AttrName -> AttrName -> Ordering
compare (AttrName Text
a) (AttrName Text
b) = Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Text -> Text
forall s. FoldCase s => s -> s
CI.foldCase Text
a) (Text -> Text
forall s. FoldCase s => s -> s
CI.foldCase Text
b)
instance Hashable AttrName where
hashWithSalt :: Int -> AttrName -> Int
hashWithSalt Int
x (AttrName Text
a) = Int -> Text -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
x (Text -> Text
forall s. FoldCase s => s -> s
CI.foldCase Text
a)
instance IsString AttrName where
fromString :: String -> AttrName
fromString = Text -> AttrName
AttrName (Text -> AttrName) -> (String -> Text) -> String -> AttrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. IsString a => String -> a
fromString
pAttrName :: Parser AttrName
pAttrName :: Parser AttrName
pAttrName =
(\Char
c ByteString
str -> Text -> AttrName
AttrName (Char -> Text -> Text
cons Char
c (ByteString -> Text
decodeUtf8 ByteString
str)))
(Char -> ByteString -> AttrName)
-> Parser ByteString Char
-> Parser ByteString (ByteString -> AttrName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Char
letter_ascii
Parser ByteString (ByteString -> AttrName)
-> Parser ByteString ByteString -> Parser AttrName
forall a b.
Parser ByteString (a -> b)
-> Parser ByteString a -> Parser ByteString b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Bool) -> Parser ByteString ByteString
takeWhile (\Char
x -> Char -> Bool
isDigit Char
x Bool -> Bool -> Bool
|| Char -> Bool
isAlpha_ascii Char
x Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_')
rAttrName :: AttrName -> Text
rAttrName :: AttrName -> Text
rAttrName (AttrName Text
x) = Text
x