module Language.PureScript.Bridge (
bridgeSumType
, defaultBridge
, module Bridge
, writePSTypes
) where
import qualified Data.Text as T
import Language.PureScript.Bridge.SumType as Bridge
import Language.PureScript.Bridge.TypeInfo as Bridge
import Language.PureScript.Bridge.Tuple as Bridge
import Language.PureScript.Bridge.Primitives as Bridge
import Language.PureScript.Bridge.Printer as Bridge
import Control.Applicative
import qualified Data.Map as M
import Data.Maybe
writePSTypes :: TypeBridge -> FilePath -> [SumType] -> IO ()
writePSTypes br root sts = do
let bridged = map (bridgeSumType br) sts
let modules = M.elems $ sumTypesToModules M.empty bridged
mapM_ (printModule root) modules
bridgeSumType :: TypeBridge -> SumType -> SumType
bridgeSumType br (SumType t cs) = SumType t $ map (bridgeConstructor br) cs
doBridge :: TypeBridge -> TypeInfo -> TypeInfo
doBridge br info = let
translated = info { typePackage = "" }
res = fixTypeParameters $ fromMaybe translated (br info)
in
res {
typeParameters = map (doBridge br) . typeParameters $ res
}
defaultBridge :: TypeBridge
defaultBridge t = stringBridge t
<|> listBridge t
<|> maybeBridge t
<|> eitherBridge t
<|> boolBridge t
<|> intBridge t
<|> tupleBridge t
bridgeConstructor :: TypeBridge -> DataConstructor -> DataConstructor
bridgeConstructor br (DataConstructor name (Left infos)) =
DataConstructor name . Left $ map (doBridge br) infos
bridgeConstructor br (DataConstructor name (Right record)) =
DataConstructor name . Right $ map (bridgeRecordEntry br) record
bridgeRecordEntry :: TypeBridge -> RecordEntry -> RecordEntry
bridgeRecordEntry br (RecordEntry label value) = RecordEntry label $ doBridge br value
fixTypeParameters :: TypeInfo -> TypeInfo
fixTypeParameters t
| T.isSuffixOf "TypeParameters" (typeModule t) = t {
typePackage = ""
, typeModule = ""
, typeName = stripNum . T.toLower $ typeName t
}
| otherwise = t
where
stripNum v = fromMaybe v (T.stripSuffix "1" v)