{-# LANGUAGE FlexibleContexts #-}
module LDAPv3.StringRepr
( StringRepr ( asParsec
, asBuilder
, renderShortText
)
, renderText
, renderString
, parseShortText
, parseText
, parseString
) where
import Common hiding (Option, many, option, some, (<|>))
import qualified Data.Text.Lazy as T (toStrict)
import Data.Text.Lazy.Builder as B
import qualified Data.Text.Short as TS
import Text.Parsec as P
import LDAPv3.AttributeDescription
import LDAPv3.Message (Filter)
import LDAPv3.SearchFilter
class StringRepr a where
asParsec :: Stream s Identity Char => Parsec s () a
asBuilder :: a -> Builder
asBuilder = fromText . TS.toText . renderShortText
renderShortText :: a -> ShortText
renderShortText = TS.fromText . T.toStrict . B.toLazyText . asBuilder
{-# MINIMAL asParsec, (renderShortText | asBuilder) #-}
renderText :: StringRepr a => a -> Text
renderText = TS.toText . renderShortText
renderString :: StringRepr a => a -> String
renderString = TS.toString . renderShortText
parseText :: StringRepr a => Text -> Maybe a
parseText = either (const Nothing) Just . parse (asParsec <* eof) ""
parseShortText :: StringRepr a => ShortText -> Maybe a
parseShortText = either (const Nothing) Just . parse (asParsec <* eof) "" . TS.toString
parseString :: StringRepr a => String -> Maybe a
parseString = either (const Nothing) Just . parse (asParsec <* eof) ""
instance StringRepr AttributeDescription where
asParsec = p'AttributeDescription
renderShortText = ts'AttributeDescription
instance StringRepr Option where
asParsec = p'Option
renderShortText = ts'Option
instance StringRepr OID where
asBuilder = r'OID
renderShortText = ts'OID
asParsec = p'OID
instance StringRepr KeyString where
asParsec = p'KeyString
renderShortText = ts'KeyString
instance StringRepr MatchingRuleId where
asParsec = p'MatchingRuleId
renderShortText = ts'MatchingRuleId
instance StringRepr Filter where
asBuilder = r'Filter
asParsec = p'Filter