T.P.App.Opt: custom FromJSON instance for LineEnding.

So either CRLF or crlf will work.
This commit is contained in:
John MacFarlane
2019-10-06 23:48:34 -07:00
parent 3ef0cdd8f9
commit 6e537aeda8
+17 -8
View File
@@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
@@ -31,16 +32,29 @@ import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
ObfuscationMethod (NoObfuscation),
CiteMethod (Citeproc))
import Text.Pandoc.Shared (camelCaseToHyphenated)
import qualified Data.Text as T
import Data.Aeson (defaultOptions, Options(..), FromJSON(..), ToJSON(..),
Value(..))
#ifdef DERIVE_JSON_VIA_TH
import Data.Aeson.TH (deriveJSON, defaultOptions, Options(..))
import Data.Aeson.TH (deriveJSON)
#else
import Data.Aeson (FromJSON (..), ToJSON (..), Options(..)
defaultOptions, genericToEncoding)
import Data.Aeson (genericToEncoding)
#endif
-- | The type of line-endings to be used when writing plain-text.
data LineEnding = LF | CRLF | Native deriving (Show, Generic)
instance ToJSON LineEnding
instance FromJSON LineEnding where
parseJSON (String t) =
case T.toLower t of
"lf" -> return LF
"crlf" -> return CRLF
"native" -> return Native
_ -> fail "Expecting LF, CRLF, or Native"
parseJSON _ = fail "Expecting string"
-- | Data structure for command line options.
data Opt = Opt
{ optTabStop :: Int -- ^ Number of spaces per tab
@@ -191,15 +205,10 @@ defaultOpts = Opt
#ifdef DERIVE_JSON_VIA_TH
-- see https://github.com/jgm/pandoc/pull/4083
-- using generic deriving caused long compilation times
$(deriveJSON defaultOptions ''LineEnding)
$(deriveJSON
defaultOptions{ fieldLabelModifier =
camelCaseToHyphenated . drop 3 } ''Opt)
#else
instance ToJSON LineEnding where
toEncoding = genericToEncoding defaultOptions
instance FromJSON LineEnding
instance ToJSON Opt where
toEncoding = genericToEncoding
defaultOptions{ fieldLabelModifier =