From a51b943f84426fb83c8f3fd77950168e59bf73a3 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 27 May 2023 17:04:23 +0200 Subject: [PATCH] AlternativeMain, to test stuff (currently sum types JSON encoding). --- spago.dhall | 1 + src/AlternativeMain.purs | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/AlternativeMain.purs diff --git a/spago.dhall b/spago.dhall index 66e1ab5..f1830b8 100644 --- a/spago.dhall +++ b/spago.dhall @@ -1,6 +1,7 @@ { name = "halogen-project" , dependencies = [ "aff" + , "argonaut-codecs" , "argonaut-core" , "arraybuffer" , "arraybuffer-builder" diff --git a/src/AlternativeMain.purs b/src/AlternativeMain.purs new file mode 100644 index 0000000..cf404fc --- /dev/null +++ b/src/AlternativeMain.purs @@ -0,0 +1,41 @@ +-- | To run this alternative main: spago run -m AlternativeMain +module AlternativeMain where + +import Prelude + +import Effect (Effect) +import Effect.Class (class MonadEffect, liftEffect) +import Effect.Class.Console as Console + +import Data.Argonaut.Core as J +import Data.Codec.Argonaut as CA +import Data.Maybe (Maybe) +import Data.Argonaut.Decode as DAD +import Data.Either (Either(..)) +import Data.Codec.Argonaut.Record as CAR +import Data.UInt (fromInt, toInt, UInt) +import Data.Argonaut.Parser as JSONParser +import Data.Bifunctor (lmap) + +import Data.Tuple (Tuple(..)) +import App.PermissionLevel as PermissionLevel + +type SumType = { permission :: PermissionLevel.PermissionLevel } +codecSumType = CA.object "SumType" (CAR.record { permission: PermissionLevel.codec }) + +encode :: SumType -> String +encode = J.stringify <<< CA.encode codecSumType + +decode :: String -> Either String SumType +decode = parseDecodeJSON codecSumType + +parseDecodeJSON :: forall a. CA.JsonCodec a -> String -> Either String a +parseDecodeJSON codec str = do + json <- JSONParser.jsonParser str + lmap CA.printJsonDecodeError (CA.decode codec json) + +main :: Effect Unit +main = do + Console.log ("Hello this is the main") + let origin = { permission: PermissionLevel.Read } + Console.log ("origin: " <> encode origin)