{- ORMOLU_DISABLE -}
-- Implicit CAD. Copyright (C) 2011, Christopher Olah (chris@colah.ca)
-- Copyright 2016, Julia Longtin (julial@turinglace.com)
-- Released under the GNU AGPLV3+, see LICENSE

-- This module exists to re-export a coherent set of functions to define
-- Data.Text.Lazy builders with.

module Graphics.Implicit.Export.TextBuilderUtils (
     -- From Data.Text.Lazy
     module DTL,
     -- From Data.Text.Lazy.Builder
     module DTLB,
     toLazyText,
     -- some special case Builders.
     bf,
     buildTruncFloat,
     buildℕ,
     buildInt
    ) where

import Prelude (Maybe(Nothing, Just), Int, ($))

import Graphics.Implicit.Definitions (, , fromℝtoFloat)
import Data.Text.Lazy as DTL (Text, pack)

import Data.Text.Internal.Lazy (defaultChunkSize)
import Data.Text.Lazy.Builder as DTLB (Builder, toLazyTextWith, fromLazyText)
import Data.Text.Lazy.Builder.RealFloat (formatRealFloat, FPFormat(Exponent, Fixed))
import Data.Text.Lazy.Builder.Int (decimal)

-- The chunk size for toLazyText is very small (128 bytes), so we export
-- a version with a much larger size (~16 K)
toLazyText :: Builder -> Text
toLazyText :: Builder -> Text
toLazyText = Int -> Builder -> Text
toLazyTextWith Int
defaultChunkSize

-- | Serialize a value as a single precision float with an exponent attached.
bf ::  -> Builder
bf :: ℝ -> Builder
bf value = FPFormat -> Maybe Int -> Float -> Builder
forall a. RealFloat a => FPFormat -> Maybe Int -> a -> Builder
formatRealFloat FPFormat
Exponent Maybe Int
forall a. Maybe a
Nothing (Float -> Builder) -> Float -> Builder
forall a b. (a -> b) -> a -> b
$ ℝ -> Float
fromℝtoFloat value

-- | Serialize a float with four decimal places
buildTruncFloat ::  -> Builder
buildTruncFloat :: ℝ -> Builder
buildTruncFloat = FPFormat -> Maybe Int -> ℝ -> Builder
forall a. RealFloat a => FPFormat -> Maybe Int -> a -> Builder
formatRealFloat FPFormat
Fixed (Maybe Int -> ℝ -> Builder) -> Maybe Int -> ℝ -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int
forall a. a -> Maybe a
Just Int
4

buildℕ ::  -> Builder
buildℕ :: ℕ -> Builder
buildℕ = ℕ -> Builder
forall a. Integral a => a -> Builder
decimal

buildInt :: Int -> Builder
buildInt :: Int -> Builder
buildInt = Int -> Builder
forall a. Integral a => a -> Builder
decimal