module Data.Yaml.Pretty
( encodePretty
, Config
, getConfCompare
, setConfCompare
, defConfig
) where
import Prelude hiding (null)
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif
import Data.Aeson.Types
import Data.ByteString (ByteString)
import Data.Function (on)
import qualified Data.HashMap.Strict as HM
import Data.List (sortBy)
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid
#endif
import Data.Text (Text)
import qualified Data.Vector as V
import Data.Yaml.Builder
data Config = Config
{ confCompare :: Text -> Text -> Ordering
}
defConfig :: Config
defConfig = Config mempty
getConfCompare :: Config -> Text -> Text -> Ordering
getConfCompare = confCompare
setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
setConfCompare cmp c = c { confCompare = cmp }
pretty :: Config -> Value -> YamlBuilder
pretty cfg = go
where go (Object o) = mapping (sortBy (confCompare cfg `on` fst) $ HM.toList $ HM.map go o)
go (Array a) = array (go <$> V.toList a)
go Null = null
go (String s) = string s
go (Number n) = scientific n
go (Bool b) = bool b
encodePretty :: ToJSON a => Config -> a -> ByteString
encodePretty cfg = toByteString . pretty cfg . toJSON