module Penny.Lincoln.Builders (
crashy
, account
, commodity
) where
import Control.Monad.Exception.Synchronous as Ex
import qualified Data.List.Split as S
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Penny.Lincoln.Bits as B
import Penny.Lincoln.TextNonEmpty (TextNonEmpty(TextNonEmpty))
import Data.Text (pack)
import qualified Data.Traversable as T
crashy :: Show e => Ex.Exceptional e a -> a
crashy = Ex.resolve (error . show)
account :: String -> Ex.Exceptional String B.Account
account input = do
subStrs <- case S.splitOn ":" input of
[] -> error "splitOn returned an empty list; should never happen"
[]:[] -> Ex.throw "account name is null"
(s:ss) -> return $ s :| ss
let makeSub s = case s of
[] -> Ex.throw $
"sub account name is null from account: " ++ input
(c:cs) -> return $ B.SubAccountName (TextNonEmpty c (pack cs))
subs <- T.traverse makeSub subStrs
return $ B.Account subs
commodity :: String -> Ex.Exceptional String B.Commodity
commodity input = do
subStrs <- case S.splitOn ":" input of
[] -> error "splitOn returned an empty list; should never happen"
[]:[] -> Ex.throw "account name is null"
(s:ss) -> return $ s :| ss
let makeSub s = case s of
[] -> Ex.throw $
"sub account name is null from account: " ++ input
(c:cs) -> return $ B.SubCommodity (TextNonEmpty c (pack cs))
subs <- T.traverse makeSub subStrs
return $ B.Commodity subs