{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Hcompta.CLI.Format.Ledger where import Control.Exception (tryJust) import Control.Monad (Monad(..), guard) import Data.Either (Either(..)) import qualified Data.List as List import qualified Data.Text.Lazy as TL import Prelude (($), (.), FilePath, IO) import System.Environment as Env (getEnv) import System.IO.Error (isDoesNotExistError) import Text.Show (Show(..)) import qualified Hcompta.Balance as Balance import qualified Hcompta.CLI.Context as C import qualified Hcompta.CLI.Lang as Lang import qualified Hcompta.CLI.Write as Write import qualified Hcompta.Format.Ledger as Ledger import qualified Hcompta.Format.Ledger.Amount as Ledger.Amount import qualified Hcompta.Format.Ledger.Read as Ledger.Read import qualified Hcompta.Format.Ledger.Write as Ledger.Write import Hcompta.Lib.Leijen (ToDoc(..)) import qualified Hcompta.Lib.Leijen as W import qualified Hcompta.Polarize as Polarize -- | Return 'FilePath's containing 'Hcompta.Format.Ledger.Journal's: -- -- * either those given if any, -- * or the one in LEDGER_FILE environment variable if any, -- * or the one in LEDGER environment variable if any. paths :: C.Context -> [FilePath] -> IO [FilePath] paths c [] = do tryJust (guard . isDoesNotExistError) $ Env.getEnv "LEDGER_FILE" >>= \x -> case x of Right ok -> return [ok] Left _ko -> do tryJust (guard . isDoesNotExistError) $ Env.getEnv "LEDGER" >>= \xx -> case xx of Right ok -> return [ok] Left _ko -> Write.fatal c Lang.Error_No_input_file_given paths _c ps = return ps instance Lang.Translate Ledger.Read.Error W.Doc where translate lang err = case err of Ledger.Read.Error_date date -> toDoc lang date Ledger.Read.Error_transaction_not_equilibrated styles tr unit_sums -> i18n_transaction_not_equilibrated styles tr unit_sums Lang.Error_Transaction_The_following_transaction_is_not_equilibrated_because Ledger.Read.Error_virtual_transaction_not_equilibrated styles tr unit_sums -> i18n_transaction_not_equilibrated styles tr unit_sums Lang.Error_Transaction_The_following_virtual_transaction_is_not_equilibrated_because Ledger.Read.Error_reading_file file_path exn -> W.vsep $ [ Lang.translate lang $ Lang.Error_Failed_to_read_file file_path , W.text $ TL.pack $ show exn ] Ledger.Read.Error_including_file file_path errs -> W.vsep $ [ Lang.translate lang $ Lang.Error_Failed_to_include_file file_path , Lang.translate lang errs ] where i18n_transaction_not_equilibrated styles tr unit_sums msg = W.vsep $ [ Lang.translate lang msg , W.vsep $ List.map (\(unit, Balance.Unit_Sum{Balance.unit_sum_quantity}) -> Lang.translate lang $ Lang.Error_Transaction_Unit_sums_up_to_the_non_null_amount unit $ Ledger.Amount.style styles $ Ledger.Amount unit $ Polarize.depolarize unit_sum_quantity ) unit_sums , W.space , Ledger.Write.transaction styles tr ]