convert: in-field, out-field -> amount-in-field, amount-out-field

This commit is contained in:
Simon Michael 2011-09-28 00:13:42 +00:00
parent fe6f37753f
commit 7d3b1f4a55

View File

@ -37,8 +37,8 @@ data CsvRules = CsvRules {
codeField :: Maybe FieldPosition, codeField :: Maybe FieldPosition,
descriptionField :: [FormatString], descriptionField :: [FormatString],
amountField :: Maybe FieldPosition, amountField :: Maybe FieldPosition,
inField :: Maybe FieldPosition, amountInField :: Maybe FieldPosition,
outField :: Maybe FieldPosition, amountOutField :: Maybe FieldPosition,
currencyField :: Maybe FieldPosition, currencyField :: Maybe FieldPosition,
baseCurrency :: Maybe String, baseCurrency :: Maybe String,
accountField :: Maybe FieldPosition, accountField :: Maybe FieldPosition,
@ -55,8 +55,8 @@ nullrules = CsvRules {
codeField=Nothing, codeField=Nothing,
descriptionField=[], descriptionField=[],
amountField=Nothing, amountField=Nothing,
inField=Nothing, amountInField=Nothing,
outField=Nothing, amountOutField=Nothing,
currencyField=Nothing, currencyField=Nothing,
baseCurrency=Nothing, baseCurrency=Nothing,
accountField=Nothing, accountField=Nothing,
@ -127,8 +127,8 @@ maxFieldIndex r = maximumDef (-1) $ catMaybes [
,statusField r ,statusField r
,codeField r ,codeField r
,amountField r ,amountField r
,inField r ,amountInField r
,outField r ,amountOutField r
,currencyField r ,currencyField r
,accountField r ,accountField r
,account2Field r ,account2Field r
@ -166,14 +166,14 @@ initialRulesFileContent =
validateRules :: CsvRules -> Maybe String validateRules :: CsvRules -> Maybe String
validateRules rules = let validateRules rules = let
hasAmount = isJust $ amountField rules hasAmount = isJust $ amountField rules
hasIn = isJust $ inField rules hasIn = isJust $ amountInField rules
hasOut = isJust $ outField rules hasOut = isJust $ amountOutField rules
in case (hasAmount, hasIn, hasOut) of in case (hasAmount, hasIn, hasOut) of
(True, True, _) -> Just "Don't specify in-field when specifying amount-field" (True, True, _) -> Just "Don't specify amount-in-field when specifying amount-field"
(True, _, True) -> Just "Don't specify out-field when specifying amount-field" (True, _, True) -> Just "Don't specify amount-out-field when specifying amount-field"
(_, False, True) -> Just "Please specify in-field when specifying out-field" (_, False, True) -> Just "Please specify amount-in-field when specifying amount-out-field"
(_, True, False) -> Just "Please specify out-field when specifying in-field" (_, True, False) -> Just "Please specify amount-out-field when specifying amount-in-field"
(False, False, False) -> Just "Please specify either amount-field, or in-field and out-field" (False, False, False) -> Just "Please specify either amount-field, or amount-in-field and amount-out-field"
_ -> Nothing _ -> Nothing
-- rules file parser -- rules file parser
@ -205,8 +205,8 @@ definitions = do
,codefield ,codefield
,descriptionfield ,descriptionfield
,amountfield ,amountfield
,infield ,amountinfield
,outfield ,amountoutfield
,currencyfield ,currencyfield
,accountfield ,accountfield
,account2field ,account2field
@ -269,17 +269,17 @@ amountfield = do
x <- updateState (\r -> r{amountField=readMay v}) x <- updateState (\r -> r{amountField=readMay v})
return x return x
infield = do amountinfield = do
string "in-field" choice [string "amount-in-field", string "in-field"]
many1 spacenonewline many1 spacenonewline
v <- restofline v <- restofline
updateState (\r -> r{inField=readMay v}) updateState (\r -> r{amountInField=readMay v})
outfield = do amountoutfield = do
string "out-field" choice [string "amount-out-field", string "out-field"]
many1 spacenonewline many1 spacenonewline
v <- restofline v <- restofline
updateState (\r -> r{outField=readMay v}) updateState (\r -> r{amountOutField=readMay v})
currencyfield = do currencyfield = do
string "currency-field" string "currency-field"
@ -478,13 +478,13 @@ getAmount :: CsvRules -> CsvRecord -> String
getAmount rules fields = case amountField rules of getAmount rules fields = case amountField rules of
Just f -> maybe "" (atDef "" fields) $ Just f Just f -> maybe "" (atDef "" fields) $ Just f
Nothing -> Nothing ->
case (c, d) of case (i, o) of
(x, "") -> x (x, "") -> x
("", x) -> "-"++x ("", x) -> "-"++x
p -> error' $ "using amount-in-field and amount-out-field, found a value in both fields: "++show p p -> error' $ "using amount-in-field and amount-out-field, found a value in both fields: "++show p
where where
c = maybe "" (atDef "" fields) (inField rules) i = maybe "" (atDef "" fields) (amountInField rules)
d = maybe "" (atDef "" fields) (outField rules) o = maybe "" (atDef "" fields) (amountOutField rules)
tests_Hledger_Cli_Convert = TestList (test_parser ++ test_description_parsing) tests_Hledger_Cli_Convert = TestList (test_parser ++ test_description_parsing)