module Nix.Atoms where
import Prelude
import Data.Text (Text, pack)
import GHC.Generics
import Data.Data
import Data.HashMap.Strict (HashMap)
data NAtom
= NInt !Integer
| NBool !Bool
| NNull
| NUri !Text
deriving (Eq, Ord, Generic, Typeable, Data, Show)
class ToAtom t where
toAtom :: t -> NAtom
instance ToAtom Bool where toAtom = NBool
instance ToAtom Int where toAtom = NInt . fromIntegral
instance ToAtom Integer where toAtom = NInt
class FromAtom t where
fromAtom :: NAtom -> t
fromAtoms :: [NAtom] -> t
fromAtomSet :: HashMap Text NAtom -> t
convert :: (ToAtom prim, FromAtom t) => prim -> t
convert = fromAtom . toAtom
atomToEnvString :: NAtom -> Text
atomToEnvString = \case
NInt i -> pack $ show i
NBool True -> "1"
NBool False -> ""
NNull -> ""
NUri uri -> uri