haspara-0.0.0.3: A library providing definitions to work with monetary values.
Safe HaskellNone
LanguageHaskell2010

Haspara.Currency

Description

This module provides definitions for modeling and working with currencies.

Synopsis

Currency

 

newtype Currency Source #

Type encoding for currency symbol values with a syntax of [A-Z]{3}[A-Z]*.

Currency values can be constructed via mkCurrencyError that works in MonadError Text context:

>>> mkCurrencyError "EUR" :: Either T.Text Currency
Right EUR

... or via mkCurrencyFail that works in MonadFail context:

>>> mkCurrencyFail "EUR" :: Maybe Currency
Just EUR

An IsString instance is provided as well which is unsafe but convenient:

>>> "EUR" :: Currency
EUR

Constructors

MkCurrency 

Fields

Instances

Instances details
Eq Currency Source # 
Instance details

Defined in Haspara.Currency

Ord Currency Source # 
Instance details

Defined in Haspara.Currency

Show Currency Source #

Show instance for Currency.

>>> "USD" :: Currency
USD
Instance details

Defined in Haspara.Currency

IsString Currency Source #

IsString instance for Currency.

>>> "USD" :: Currency
USD
Instance details

Defined in Haspara.Currency

Hashable Currency Source # 
Instance details

Defined in Haspara.Currency

Methods

hashWithSalt :: Int -> Currency -> Int #

hash :: Currency -> Int #

ToJSON Currency Source #

ToJSON instance for Currency.

>>> Aeson.encode ("USD" :: Currency)
"\"USD\""
Instance details

Defined in Haspara.Currency

FromJSON Currency Source #

FromJSON instance for Currency.

>>> Aeson.eitherDecode "\"\"" :: Either String Currency
Left "Error in $: Currency code error! Expecting at least 3 uppercase ASCII letters, but received: "
>>> Aeson.eitherDecode "\"A\"" :: Either String Currency
Left "Error in $: Currency code error! Expecting at least 3 uppercase ASCII letters, but received: A"
>>> Aeson.eitherDecode "\"AB\"" :: Either String Currency
Left "Error in $: Currency code error! Expecting at least 3 uppercase ASCII letters, but received: AB"
>>> Aeson.eitherDecode "\"ABC\"" :: Either String Currency
Right ABC
>>> Aeson.eitherDecode "\"ABCD\"" :: Either String Currency
Right ABCD
Instance details

Defined in Haspara.Currency

Lift Currency Source # 
Instance details

Defined in Haspara.Currency

mkCurrencyError :: MonadError Text m => Text -> m Currency Source #

Smart constructor for Currency values within MonadError context.

>>> mkCurrencyError "" :: Either T.Text Currency
Left "Currency code error! Expecting at least 3 uppercase ASCII letters, but received: "
>>> mkCurrencyError " " :: Either T.Text Currency
Left "Currency code error! Expecting at least 3 uppercase ASCII letters, but received:  "
>>> mkCurrencyError "AB" :: Either T.Text Currency
Left "Currency code error! Expecting at least 3 uppercase ASCII letters, but received: AB"
>>> mkCurrencyError " ABC " :: Either T.Text Currency
Left "Currency code error! Expecting at least 3 uppercase ASCII letters, but received:  ABC "
>>> mkCurrencyError "ABC" :: Either T.Text Currency
Right ABC

mkCurrencyFail :: MonadFail m => Text -> m Currency Source #

Smart constructor for Currency values within MonadFail context.

>>> mkCurrencyFail "" :: Maybe Currency
Nothing
>>> mkCurrencyFail "US" :: Maybe Currency
Nothing
>>> mkCurrencyFail "usd" :: Maybe Currency
Nothing
>>> mkCurrencyFail "USD" :: Maybe Currency
Just USD

currencyCodeParser :: Parsec Void Text Text Source #

Parser that parses currency codes.

>>> MP.runParser currencyCodeParser "Example" ""
Left (ParseErrorBundle {bundleErrors = TrivialError 0 (Just EndOfInput) (fromList []) :| [], bundlePosState = PosState {pstateInput = "", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})
>>> MP.runParser currencyCodeParser "Example" " "
Left (ParseErrorBundle {bundleErrors = TrivialError 0 (Just (Tokens (' ' :| ""))) (fromList []) :| [], bundlePosState = PosState {pstateInput = " ", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})
>>> MP.runParser currencyCodeParser "Example" "a"
Left (ParseErrorBundle {bundleErrors = TrivialError 0 (Just (Tokens ('a' :| ""))) (fromList []) :| [], bundlePosState = PosState {pstateInput = "a", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})
>>> MP.runParser currencyCodeParser "Example" "A"
Left (ParseErrorBundle {bundleErrors = TrivialError 1 (Just EndOfInput) (fromList []) :| [], bundlePosState = PosState {pstateInput = "A", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})
>>> MP.runParser currencyCodeParser "Example" "AB"
Left (ParseErrorBundle {bundleErrors = TrivialError 2 (Just EndOfInput) (fromList []) :| [], bundlePosState = PosState {pstateInput = "AB", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})
>>> MP.runParser currencyCodeParser "Example" "ABC"
Right "ABC"
>>> MP.runParser currencyCodeParser "Example" "ABCD"
Right "ABCD"
>>> MP.runParser currencyCodeParser "Example" " ABCD "
Left (ParseErrorBundle {bundleErrors = TrivialError 0 (Just (Tokens (' ' :| ""))) (fromList []) :| [], bundlePosState = PosState {pstateInput = " ABCD ", pstateOffset = 0, pstateSourcePos = SourcePos {sourceName = "Example", sourceLine = Pos 1, sourceColumn = Pos 1}, pstateTabWidth = Pos 8, pstateLinePrefix = ""}})

Currency Pair

 

data CurrencyPair Source #

Type encoding of a currency pair.

CurrencyPair values are constructed via the data constructor:

>>> CurrencyPair "EUR" "USD"
EUR/USD

FromJSON and ToJSON instances are provided as well:

>>> Aeson.decode "{\"base\": \"EUR\", \"quote\": \"EUR\"}" :: Maybe CurrencyPair
Just EUR/EUR
>>> Aeson.encode (CurrencyPair "EUR" "USD")
"{\"base\":\"EUR\",\"quote\":\"USD\"}"

Constructors

CurrencyPair 

Fields

Instances

Instances details
Eq CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

Ord CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

Show CurrencyPair Source #

Show instance for CurrencyPair.

>>> CurrencyPair "EUR" "USD"
EUR/USD
Instance details

Defined in Haspara.Currency

Generic CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

Associated Types

type Rep CurrencyPair :: Type -> Type #

ToJSON CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

FromJSON CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

Lift CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

type Rep CurrencyPair Source # 
Instance details

Defined in Haspara.Currency

type Rep CurrencyPair = D1 ('MetaData "CurrencyPair" "Haspara.Currency" "haspara-0.0.0.3-4zv1bokQoxCFAyBHZ5p8oz" 'False) (C1 ('MetaCons "CurrencyPair" 'PrefixI 'True) (S1 ('MetaSel ('Just "currencyPairBase") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Currency) :*: S1 ('MetaSel ('Just "currencyPairQuote") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Currency)))

toCurrencyTuple :: CurrencyPair -> (Currency, Currency) Source #

Converts a CurrencyPair to a 2-tuple of Currency values.

>>> toCurrencyTuple (CurrencyPair "EUR" "USD")
(EUR,USD)

fromCurrencyTuple :: (Currency, Currency) -> CurrencyPair Source #

Converts a 2-tuple of Currency values to a CurrencyPair.

>>> fromCurrencyTuple ("EUR", "USD")
EUR/USD