{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Turtle.Line
( Line
, lineToText
, textToLines
, linesToText
, textToLine
, unsafeTextToLine
, NewlineForbidden(..)
) where
import Data.Text (Text)
import qualified Data.Text as Text
#if __GLASGOW_HASKELL__ >= 708
import Data.Coerce
#endif
import Data.List.NonEmpty (NonEmpty(..))
import Data.String
#if __GLASGOW_HASKELL__ >= 710
#else
import Data.Monoid
#endif
import Data.Maybe
import Data.Typeable
import Control.Exception
import qualified Data.List.NonEmpty
data NewlineForbidden = NewlineForbidden
deriving (Int -> NewlineForbidden -> ShowS
[NewlineForbidden] -> ShowS
NewlineForbidden -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NewlineForbidden] -> ShowS
$cshowList :: [NewlineForbidden] -> ShowS
show :: NewlineForbidden -> String
$cshow :: NewlineForbidden -> String
showsPrec :: Int -> NewlineForbidden -> ShowS
$cshowsPrec :: Int -> NewlineForbidden -> ShowS
Show, Typeable)
instance Exception NewlineForbidden
newtype Line = Line Text
deriving (Line -> Line -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Line -> Line -> Bool
$c/= :: Line -> Line -> Bool
== :: Line -> Line -> Bool
$c== :: Line -> Line -> Bool
Eq, Eq Line
Line -> Line -> Bool
Line -> Line -> Ordering
Line -> Line -> Line
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Line -> Line -> Line
$cmin :: Line -> Line -> Line
max :: Line -> Line -> Line
$cmax :: Line -> Line -> Line
>= :: Line -> Line -> Bool
$c>= :: Line -> Line -> Bool
> :: Line -> Line -> Bool
$c> :: Line -> Line -> Bool
<= :: Line -> Line -> Bool
$c<= :: Line -> Line -> Bool
< :: Line -> Line -> Bool
$c< :: Line -> Line -> Bool
compare :: Line -> Line -> Ordering
$ccompare :: Line -> Line -> Ordering
Ord, Int -> Line -> ShowS
[Line] -> ShowS
Line -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Line] -> ShowS
$cshowList :: [Line] -> ShowS
show :: Line -> String
$cshow :: Line -> String
showsPrec :: Int -> Line -> ShowS
$cshowsPrec :: Int -> Line -> ShowS
Show, Semigroup Line
Line
[Line] -> Line
Line -> Line -> Line
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [Line] -> Line
$cmconcat :: [Line] -> Line
mappend :: Line -> Line -> Line
$cmappend :: Line -> Line -> Line
mempty :: Line
$cmempty :: Line
Monoid)
#if __GLASGOW_HASKELL__ >= 804
instance Semigroup Line where
<> :: Line -> Line -> Line
(<>) = forall a. Monoid a => a -> a -> a
mappend
#endif
instance IsString Line where
fromString :: String -> Line
fromString = forall a. a -> Maybe a -> a
fromMaybe (forall a e. Exception e => e -> a
throw NewlineForbidden
NewlineForbidden) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe Line
textToLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
lineToText :: Line -> Text
lineToText :: Line -> Text
lineToText (Line Text
t) = Text
t
textToLines :: Text -> NonEmpty Line
textToLines :: Text -> NonEmpty Line
textToLines =
#if __GLASGOW_HASKELL__ >= 708
forall a. [a] -> NonEmpty a
Data.List.NonEmpty.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce (Text -> Text -> [Text]
Text.splitOn Text
"\n")
#else
Data.List.NonEmpty.fromList . map unsafeTextToLine . Text.splitOn "\n"
#endif
linesToText :: [Line] -> Text
linesToText :: [Line] -> Text
linesToText =
#if __GLASGOW_HASKELL__ >= 708
coerce :: forall a b. Coercible a b => a -> b
coerce [Text] -> Text
Text.unlines
#else
Text.unlines . map lineToText
#endif
textToLine :: Text -> Maybe Line
textToLine :: Text -> Maybe Line
textToLine = forall {a}. NonEmpty a -> Maybe a
fromSingleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NonEmpty Line
textToLines
where
fromSingleton :: NonEmpty a -> Maybe a
fromSingleton (a
a :| []) = forall a. a -> Maybe a
Just a
a
fromSingleton NonEmpty a
_ = forall a. Maybe a
Nothing
unsafeTextToLine :: Text -> Line
unsafeTextToLine :: Text -> Line
unsafeTextToLine = Text -> Line
Line