yaml-pretty-extras 0.0.1.3 → 0.0.2.0
raw patch · 3 files changed
+43/−24 lines, 3 filesdep −bytestringdep −errorsdep −mtl
Dependencies removed: bytestring, errors, mtl, text
Files
- ChangeLog.md +5/−0
- src/Data/Yaml/Pretty/Extras.hs +36/−14
- yaml-pretty-extras.cabal +2/−10
ChangeLog.md view
@@ -1,5 +1,10 @@ # Changelog for yaml-pretty-extras +## v0.0.2.0++Add haddock documentation.+Drop PrettyYamlException type.+ ## v0.0.1.3 Add decodeFileThrow, decodeFileThrowLogged and encodeFilePrettyLogged functions
src/Data/Yaml/Pretty/Extras.hs view
@@ -1,56 +1,77 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.Yaml.Pretty.Extras+-- Copyright : (c) Daniel Firth 2018+-- License : BSD3+-- Maintainer : locallycompact@gmail.com+-- Stability : experimental+--+-- This file defines yaml pretty printers with additional MonadThrow helpers+-- and RIO display functionality.+--+----------------------------------------------------------------------------- module Data.Yaml.Pretty.Extras ( module Data.Yaml , module Data.Yaml.Pretty++ -- * Yaml Pretty Printers , ToPrettyYaml(..) , encodeFilePretty- , displayPrettyYaml- , PrettyYamlException(..)++ -- * RIO Helpers (Codecs and Logging) , decodeFileThrow+ , displayPrettyYaml , decodeFileThrowLogged , encodeFilePrettyLogged ) where -import Control.Error.Safe-import Control.Monad.Except import Data.Typeable import Data.Yaml import Data.Yaml.Pretty-import RIO hiding ( tryJust )+import RIO import qualified RIO.ByteString as BS import RIO.List -data PrettyYamlException = FieldNotListed Text [Text]- deriving (Typeable)+listElemCmp as x y =+ fromMaybe LT $ liftA2 compare (elemIndex x as) (elemIndex y as) -instance Show PrettyYamlException where- show (FieldNotListed x as) = "Could not find field " ++ show x ++ "in " ++ show as+{- | Augments ToJSON by allowing specification of a fieldOrder for printing. -exceptElemIndex x as = tryJust (FieldNotListed x as) (elemIndex x as)+ > data Person = { name :: Text, age :: Int, job :: Text }+ > deriving (Eq, FromJSON, Generic, Show, ToJSON)+ >+ > instance ToPrettyYaml Person where+ > fieldOrder = const ["name", "age", "job"] -listElemCmp as x y = either (error . show) id $ runExcept $ liftA2- compare- (exceptElemIndex x as)- (exceptElemIndex y as)+-} class ToJSON a => ToPrettyYaml a where+ -- | The order that detected fields should be printed in, fields that aren't found in this function+ -- will be printed non-deterministically. fieldOrder :: a -> [Text] + -- | Whether to drop null elements on this type. dropNull :: a -> Bool dropNull = const True + -- | Prints a Yaml ByteString according to specified fieldOrder. toPrettyYaml :: a -> BS.ByteString toPrettyYaml = encodePretty =<< liftM2 setConfDropNull dropNull (flip setConfCompare defConfig . listElemCmp . fieldOrder) +-- | A version of Data.Yaml's encodeFile using `toPrettyYaml` instead of `toJSON` encodeFilePretty :: (MonadIO m) => ToPrettyYaml a => FilePath -> a -> m () encodeFilePretty f x = BS.writeFile f (toPrettyYaml x) +-- | Displays a ToPrettyYaml instance as Utf8, for use with RIO log functions displayPrettyYaml :: ToPrettyYaml a => a -> Utf8Builder displayPrettyYaml = displayBytesUtf8 . toPrettyYaml +-- | A version of Data.Yaml's decodeFileEither lifted to MonadThrow decodeFileThrow :: (MonadIO m, FromJSON c, MonadThrow m) => FilePath -> m c decodeFileThrow = liftIO . decodeFileEither >=> either throwM return +-- | decodeFileThrow with info logging, reports what was parsed via RIO's logInfo decodeFileThrowLogged :: ( MonadReader env m , MonadThrow m@@ -75,6 +96,7 @@ ] return t +-- | encodeFilePretty with info logging, reports what was saved to disk via RIO's logInfo encodeFilePrettyLogged :: ( MonadReader env m , MonadThrow m
yaml-pretty-extras.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 02fcaf6c7200aae215dad66d520b577e59eee386c8f339577cae1535d75f6775+-- hash: 010d745442891bcf2faf89ea29834fe82716bfc1ab9555e945b01d2990a228d6 name: yaml-pretty-extras-version: 0.0.1.3+version: 0.0.2.0 synopsis: Extra functionality for pretty printing Yaml documents. description: Extra functionality for pretty printing Yaml documents. Allows precise field ordering. category: Data@@ -34,11 +34,7 @@ default-extensions: AutoDeriveTypeable BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DoAndIfThenElse EmptyDataDecls ExistentialQuantification FlexibleContexts FlexibleInstances FunctionalDependencies GADTs GeneralizedNewtypeDeriving InstanceSigs KindSignatures LambdaCase MonadFailDesugaring MultiParamTypeClasses MultiWayIf NamedFieldPuns NoImplicitPrelude OverloadedStrings PartialTypeSignatures PatternGuards PolyKinds RankNTypes RecordWildCards ScopedTypeVariables StandaloneDeriving TupleSections TypeFamilies TypeSynonymInstances ViewPatterns build-depends: base >=4.7 && <5- , bytestring- , errors- , mtl , rio- , text , yaml default-language: Haskell2010 @@ -53,12 +49,8 @@ ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5- , bytestring- , errors , hspec- , mtl , rio- , text , yaml , yaml-pretty-extras default-language: Haskell2010