{-# LANGUAGE OverloadedStrings #-}
module Data.Svfactor.Text.Newline (
Newline (CR, LF, CRLF)
, AsNewline(_Newline, _CR, _LF, _CRLF)
, newlineToString
, parseNewline
) where
import Control.DeepSeq (NFData (rnf))
import Control.Lens (Prism', prism, prism')
import Data.String (IsString (fromString))
import Data.Text (Text)
data Newline =
CR
| LF
| CRLF
deriving (Eq, Ord, Show)
instance NFData Newline where
rnf x = seq x ()
class AsNewline r where
_Newline :: Prism' r Newline
_CR :: Prism' r ()
_LF :: Prism' r ()
_CRLF :: Prism' r ()
_CR = _Newline . _CR
_LF = _Newline . _LF
_CRLF = _Newline . _CRLF
instance AsNewline Newline where
_Newline = id
_CR =
prism (const CR) $ \x -> case x of
CR -> Right ()
_ -> Left x
_LF =
prism (const LF) $ \x -> case x of
LF -> Right ()
_ -> Left x
_CRLF =
prism (const CRLF) $ \x -> case x of
CRLF -> Right ()
_ -> Left x
instance AsNewline Text where
_Newline = prism' newlineToString parseNewline
newlineToString :: IsString s => Newline -> s
newlineToString n = fromString $
case n of
CR -> "\r"
LF -> "\n"
CRLF -> "\r\n"
parseNewline :: Text -> Maybe Newline
parseNewline "" = Nothing
parseNewline "\r" = Just CR
parseNewline "\n" = Just LF
parseNewline "\r\n" = Just CRLF
parseNewline _ = Nothing