module Waargonaut.Encode.Builder.JNumber
( jNumberBuilder
) where
import Control.Lens (review)
import qualified Data.Digit as D
import Data.List.NonEmpty (NonEmpty)
import Data.Monoid ((<>))
import Waargonaut.Types.JNumber (E (..), Exp (..), Frac (..),
JNumber (..), jIntToDigits)
import Waargonaut.Encode.Builder.Types (Builder (..))
getExpSymbol
:: Monoid b
=> Builder t b
-> Maybe Bool
-> b
getExpSymbol bldr (Just True) = fromChar bldr '-'
getExpSymbol bldr (Just False) = fromChar bldr '+'
getExpSymbol _ _ = mempty
eBuilder
:: Monoid b
=> Builder t b
-> E
-> b
eBuilder bldr Ee = fromChar bldr 'e'
eBuilder bldr EE = fromChar bldr 'E'
fracBuilder :: Monoid b => Builder t b -> Frac -> b
fracBuilder bldr (Frac digs) = digitsBuilder bldr digs
digitsBuilder
:: Monoid b
=> Builder t b
-> NonEmpty D.DecDigit
-> b
digitsBuilder bldr =
foldMap (fromInt bldr . review D.integralDecimal)
expBuilder
:: Monoid b
=> Builder t b
-> Exp
-> b
expBuilder bldr (Exp e sign digs) =
eBuilder bldr e <> getExpSymbol bldr sign <> digitsBuilder bldr digs
jNumberBuilder
:: Monoid b
=> Builder t b
-> JNumber
-> b
jNumberBuilder bldr (JNumber sign digs mfrac mexp) =
s <> digits <> frac' <> expo
where
s = if sign then fromChar bldr '-' else mempty
digits = digitsBuilder bldr . jIntToDigits $ digs
frac' = foldMap (mappend (fromChar bldr '.') . fracBuilder bldr) mfrac
expo = foldMap (expBuilder bldr) mexp