2018-05-09 01:40:56 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2018-04-12 02:59:42 +02:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2018-04-07 21:02:50 +02:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Nix.Atoms where
|
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-04-12 02:59:42 +02:00
|
|
|
import Codec.Serialise
|
2018-05-09 01:40:56 +02:00
|
|
|
#endif
|
2019-03-17 22:47:38 +01:00
|
|
|
import Control.DeepSeq
|
|
|
|
import Data.Data
|
2019-03-22 22:50:29 +01:00
|
|
|
import Data.Fixed (mod')
|
2019-03-17 22:47:38 +01:00
|
|
|
import Data.Hashable
|
|
|
|
import Data.Text ( Text
|
|
|
|
, pack
|
|
|
|
)
|
|
|
|
import GHC.Generics
|
2018-04-07 21:02:50 +02:00
|
|
|
|
|
|
|
-- | Atoms are values that evaluate to themselves. This means that
|
|
|
|
-- they appear in both the parsed AST (in the form of literals) and
|
|
|
|
-- the evaluated form.
|
|
|
|
data NAtom
|
2019-05-16 21:42:49 +02:00
|
|
|
-- | An URI like @https://example.com@.
|
|
|
|
= NURI Text
|
2018-04-07 21:02:50 +02:00
|
|
|
-- | An integer. The c nix implementation currently only supports
|
|
|
|
-- integers that fit in the range of 'Int64'.
|
2019-05-16 21:42:49 +02:00
|
|
|
| NInt Integer
|
2018-04-07 21:02:50 +02:00
|
|
|
-- | A floating point number
|
2018-04-09 11:07:40 +02:00
|
|
|
| NFloat Float
|
2020-07-03 01:35:30 +02:00
|
|
|
-- | Booleans. @false@ or @true@.
|
2018-04-09 11:07:40 +02:00
|
|
|
| NBool Bool
|
2020-07-03 01:35:30 +02:00
|
|
|
-- | Null values. There's only one of this variant: @null@.
|
2018-04-07 21:02:50 +02:00
|
|
|
| NNull
|
2018-04-17 21:53:41 +02:00
|
|
|
deriving (Eq, Ord, Generic, Typeable, Data, Show, Read, NFData,
|
2018-05-09 01:40:56 +02:00
|
|
|
Hashable)
|
|
|
|
|
2018-05-11 21:15:18 +02:00
|
|
|
#ifdef MIN_VERSION_serialise
|
2018-05-09 01:40:56 +02:00
|
|
|
instance Serialise NAtom
|
|
|
|
#endif
|
2018-04-09 11:07:40 +02:00
|
|
|
|
2018-04-07 21:02:50 +02:00
|
|
|
-- | Translate an atom into its nix representation.
|
|
|
|
atomText :: NAtom -> Text
|
2019-05-16 21:42:49 +02:00
|
|
|
atomText (NURI t) = t
|
2019-03-17 22:47:38 +01:00
|
|
|
atomText (NInt i) = pack (show i)
|
2019-03-22 22:50:29 +01:00
|
|
|
atomText (NFloat f) = pack (showNixFloat f)
|
|
|
|
where
|
|
|
|
showNixFloat x
|
|
|
|
| x `mod'` 1 /= 0 = show x
|
|
|
|
| otherwise = show (truncate x :: Int)
|
2019-03-17 22:47:38 +01:00
|
|
|
atomText (NBool b) = if b then "true" else "false"
|
2018-04-07 21:02:50 +02:00
|
|
|
atomText NNull = "null"
|