From 78004194a4d9dfabd5e64d72751f3270d3034217 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Tue, 27 Jun 2017 05:37:41 +0300 Subject: Remove I/O format knowledge from library functions --- cmd/Main.hs | 5 +++-- cmd/Main/Output.hs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 cmd/Main/Output.hs (limited to 'cmd') diff --git a/cmd/Main.hs b/cmd/Main.hs index 69ce114..35a42a6 100644 --- a/cmd/Main.hs +++ b/cmd/Main.hs @@ -18,8 +18,9 @@ import Text.InterpolatedString.Perl6 (qc) import Malodivo.Budget (manyToMany) -import Malodivo.Types.District (di2df) import qualified Main.Input as I +import qualified Main.Output as O +import Malodivo.Types.District (di2df) import Paths_malodivo (version) -- from cabal usageHelp :: String @@ -49,7 +50,7 @@ process = do let allBills = I.bills si suppliedFunds = di2df $ I.districts si in do when (HM.null suppliedFunds) $ die "We needs at least one district" - L.putStr . encode $ manyToMany suppliedFunds allBills + L.putStr . encode . O.encode $ manyToMany suppliedFunds allBills main :: IO () main = do diff --git a/cmd/Main/Output.hs b/cmd/Main/Output.hs new file mode 100644 index 0000000..107e8fd --- /dev/null +++ b/cmd/Main/Output.hs @@ -0,0 +1,29 @@ +{- +This modules describes output data for the command line utlity. +-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} + +module Main.Output + ( Output + , encode + ) where + +import GHC.Generics (Generic) + +import Data.Aeson (ToJSON) + +import Malodivo.Types.Bill (Bill) +import Malodivo.Types.District (DistrictFunds, DistrictInfo, df2di) + +type Output = [BillBudget] + +-- | JSON-friendly type. It describes contribution of each district into a bill. +data BillBudget = BillBudget + { bill :: Bill + , districts :: [DistrictInfo] + } deriving (Generic, ToJSON) + +-- | Translate into JSON-friendly format. +encode :: [(Bill, DistrictFunds)] -> Output +encode = map (\(b, df) -> BillBudget {bill = b, districts = df2di df}) -- cgit v1.2.3