aboutsummaryrefslogtreecommitdiff
path: root/cmd/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/Main.hs')
-rw-r--r--cmd/Main.hs52
1 files changed, 19 insertions, 33 deletions
diff --git a/cmd/Main.hs b/cmd/Main.hs
index bbc45cb..da28a98 100644
--- a/cmd/Main.hs
+++ b/cmd/Main.hs
@@ -1,32 +1,30 @@
-{-# LANGUAGE DeriveAnyClass #-}
-{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE QuasiQuotes #-}
module Main
( main
) where
-import Control.Arrow ((&&&))
import Control.Monad (when)
import Data.Version (showVersion)
-import GHC.Generics (Generic)
import System.Environment (getArgs)
import System.Exit (die)
-import Data.Aeson (FromJSON, eitherDecode, encode)
+import Data.Aeson (eitherDecode, encode)
import qualified Data.ByteString.Lazy as L
import qualified Data.HashMap.Strict as HM
-import qualified System.Console.Docopt.NoTH as O
+import System.Console.Docopt.NoTH
+ (isPresent, longOption, parseArgsOrExit, parseUsageOrExit, usage)
import Text.InterpolatedString.Perl6 (qc)
-import Malodivo.Budget (manyToOne)
-import Malodivo.Types.Bill (Bill)
-import Malodivo.Types.District (District)
+import Malodivo.Budget (manyToMany)
+import Main.DistrictInfo (di2df)
+import qualified Main.Input as I
+import qualified Main.Output as O
import Paths_malodivo (version) -- from cabal
-usage :: String
-usage =
+usageHelp :: String
+usageHelp =
"malodivo " ++
showVersion version ++
" - budget planning tool for the Kingdom of Malodivo" ++
@@ -41,36 +39,24 @@ Options:
-h, --help Show this message and exit
-
|]
-data DistrictInfo = DistrictInfo
- { name :: District
- , availableFunds :: Integer
- } deriving (Generic, FromJSON)
-
-data SimpleInput = SimpleInput
- { bills :: [Bill]
- , districts :: [DistrictInfo]
- } deriving (Generic, FromJSON)
-
process :: IO ()
process = do
input <- L.getContents
case eitherDecode input of
Left err -> die err
- Right si -> do
- let nbills = length . bills $ si
- funds =
- HM.fromListWith (+) . map (name &&& availableFunds) $ districts si
- when (nbills /= 1) $ die "We needs exactly one bill in input"
- when (HM.null funds) $ die "We needs at least one district"
- L.putStr . encode $ manyToOne funds (head . bills $ si)
+ Right si ->
+ let allBills = I.bills si
+ billFunds = manyToMany suppliedFunds allBills
+ suppliedFunds = di2df $ I.districts si
+ in do when (HM.null suppliedFunds) $ die "We needs at least one district"
+ L.putStr . encode $ O.zipBills allBills billFunds
main :: IO ()
main = do
- doco <- O.parseUsageOrExit usage
- args <- O.parseArgsOrExit doco =<< getArgs
- if args `O.isPresent` O.longOption "help"
- then putStrLn $ O.usage doco
+ doco <- parseUsageOrExit usageHelp
+ args <- parseArgsOrExit doco =<< getArgs
+ if args `isPresent` longOption "help"
+ then putStrLn $ usage doco
else process