2018-05-13 20:13:30 +02:00
{- # LANGUAGE DeriveGeneric # -}
module Nix.NixString (
2018-09-09 17:01:09 +02:00
NixString
2018-09-16 02:04:54 +02:00
, stringHasContext
2018-09-09 17:01:09 +02:00
, hackyStringIgnoreContextMaybe
, hackyStringIgnoreContext
2018-09-16 02:04:54 +02:00
, hackyMakeNixStringWithoutContext
, hackyModifyNixContents
2018-05-13 20:13:30 +02:00
) where
import qualified Data.HashSet as S
import Data.Hashable
import Data.Text ( Text )
import GHC.Generics
import Data.Semigroup
2018-09-16 02:04:54 +02:00
{- # WARNING hackyStringIgnoreContextMaybe, hackyStringIgnoreContext, hackyMakeNixStringWithoutContext, hackyModifyNixContents "This NixString function needs to be replaced" # -}
2018-09-09 17:01:09 +02:00
2018-05-13 20:13:30 +02:00
-- | A 'ContextFlavor' describes the sum of possible derivations for string contexts
data ContextFlavor =
DirectPath
| DerivationOutput ! Text
deriving ( Show , Eq , Ord , Generic )
instance Hashable ContextFlavor
-- | A 'StringContext' ...
data StringContext =
StringContext { scPath :: ! Text
, scFlavor :: ! ContextFlavor
} deriving ( Eq , Ord , Show , Generic )
instance Hashable StringContext
data NixString = NixString
{ nsContents :: ! Text
, nsContext :: ! ( S . HashSet StringContext )
} deriving ( Eq , Ord , Show , Generic )
instance Hashable NixString
instance Semigroup NixString where
NixString s1 t1 <> NixString s2 t2 = NixString ( s1 <> s2 ) ( t1 <> t2 )
instance Monoid NixString where
mempty = NixString mempty mempty
mappend = ( <> )
2018-09-16 02:04:54 +02:00
-- | Extract the string contents from a NixString that has no context
2018-09-09 17:01:09 +02:00
hackyStringIgnoreContextMaybe :: NixString -> Maybe Text
hackyStringIgnoreContextMaybe ( NixString s c ) | null c = Just s
2018-05-13 20:13:30 +02:00
| otherwise = Nothing
2018-09-16 02:04:54 +02:00
-- | Extract the string contents from a NixString even if the NixString has an associated context
2018-09-09 17:01:09 +02:00
hackyStringIgnoreContext :: NixString -> Text
hackyStringIgnoreContext ( NixString s _ ) = s
2018-05-13 20:13:30 +02:00
2018-09-16 02:04:54 +02:00
-- | Returns True if the NixString has an associated context
stringHasContext :: NixString -> Bool
stringHasContext ( NixString _ c ) = not ( null c )
2018-05-13 20:13:30 +02:00
2018-09-16 02:04:54 +02:00
-- | Constructs a NixString without a context
hackyMakeNixStringWithoutContext :: Text -> NixString
hackyMakeNixStringWithoutContext = flip NixString mempty
2018-05-13 20:13:30 +02:00
2018-09-16 02:04:54 +02:00
-- | Modify the string part of the NixString -- ignores the context
hackyModifyNixContents :: ( Text -> Text ) -> NixString -> NixString
hackyModifyNixContents f ( NixString s c ) = NixString ( f s ) c
2018-05-13 20:13:30 +02:00