summaryrefslogtreecommitdiff
path: root/dhall-to-json/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dhall-to-json/Main.hs')
-rw-r--r--dhall-to-json/Main.hs81
1 files changed, 60 insertions, 21 deletions
diff --git a/dhall-to-json/Main.hs b/dhall-to-json/Main.hs
index db0c9a4..e7191ed 100644
--- a/dhall-to-json/Main.hs
+++ b/dhall-to-json/Main.hs
@@ -1,14 +1,13 @@
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE DeriveAnyClass #-}
-{-# LANGUAGE DeriveGeneric #-}
-{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Exception (SomeException)
-import Options.Generic (Generic, ParseRecord, type (<?>))
+import Data.Monoid ((<>))
+import Dhall.JSON (Conversion)
+import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.Aeson
@@ -19,32 +18,72 @@ import qualified Data.Text.IO
import qualified Dhall
import qualified Dhall.JSON
import qualified GHC.IO.Encoding
-import qualified Options.Generic
+import qualified Options.Applicative
import qualified System.Exit
import qualified System.IO
data Options = Options
- { explain :: Bool <?> "Explain error messages in detail"
- , pretty :: Bool <?> "Pretty print generated JSON"
- , omitNull :: Bool <?> "Omit record fields that are null"
- } deriving (Generic, ParseRecord)
+ { explain :: Bool
+ , pretty :: Bool
+ , omitNull :: Bool
+ , conversion :: Conversion
+ }
+
+parseOptions :: Parser Options
+parseOptions = Options.Applicative.helper <*> do
+ explain <- parseExplain
+ pretty <- parsePretty
+ omitNull <- parseOmitNull
+ conversion <- Dhall.JSON.parseConversion
+ return (Options {..})
+ where
+ parseExplain =
+ Options.Applicative.switch
+ ( Options.Applicative.long "explain"
+ <> Options.Applicative.help "Explain error messages in detail"
+ )
+
+ parsePretty =
+ Options.Applicative.switch
+ ( Options.Applicative.long "pretty"
+ <> Options.Applicative.help "Pretty print generated JSON"
+ )
+
+ parseOmitNull =
+ Options.Applicative.switch
+ ( Options.Applicative.long "omitNull"
+ <> Options.Applicative.help "Omit record fields that are null"
+ )
+
+parserInfo :: ParserInfo Options
+parserInfo =
+ Options.Applicative.info
+ parseOptions
+ ( Options.Applicative.fullDesc
+ <> Options.Applicative.progDesc "Compile Dhall to JSON"
+ )
main :: IO ()
-main = handle $ do
+main = do
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
- Options {..} <- Options.Generic.getRecord "Compile Dhall to JSON"
- let encode = if Options.Generic.unHelpful pretty
- then Data.Aeson.Encode.Pretty.encodePretty
- else Data.Aeson.encode
- explaining = if Options.Generic.unHelpful explain then Dhall.detailed else id
- omittingNull = if Options.Generic.unHelpful omitNull then Dhall.JSON.omitNull else id
+ Options {..} <- Options.Applicative.execParser parserInfo
+
+ handle $ do
+ let encode =
+ if pretty
+ then Data.Aeson.Encode.Pretty.encodePretty
+ else Data.Aeson.encode
+
+ let explaining = if explain then Dhall.detailed else id
+
+ let omittingNull = if omitNull then Dhall.JSON.omitNull else id
- stdin <- Data.Text.IO.getContents
+ stdin <- Data.Text.IO.getContents
- json <- omittingNull <$> explaining (Dhall.JSON.codeToValue "(stdin)" stdin)
+ json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)
- Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
+ Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
handle :: IO a -> IO a
handle = Control.Exception.handle handler