Bläddra i källkod

Add a type class

jherve 1 år sedan
förälder
incheckning
07cd2eb798
1 ändrade filer med 33 tillägg och 11 borttagningar
  1. 33 11
      src/Main.purs

+ 33 - 11
src/Main.purs

@@ -3,14 +3,14 @@ module Main where
 import Prelude
 
 import Data.Either (Either(..))
-import Data.Generic.Rep (class Generic)
-import Data.Show.Generic (genericShow)
 import Data.Tuple (Tuple(..))
 import Effect (Effect)
 import Effect.Aff (Aff, launchAff_)
 import Effect.Class.Console (log, logShow)
 import Node.ChildProcess.Types (Exit(..))
 import Node.Library.Execa (execa)
+import Data.Generic.Rep (class Generic)
+import Data.Show.Generic (genericShow)
 
 
 main :: Effect Unit
@@ -18,9 +18,13 @@ main = do
   log "hello"
   launchAff_ $ unit <$ runCommand "pwd" []
   launchAff_ do
-    res <- runCommand' toResult fromError $ toCommand $ RecSelCommand {filePath: "/tmp/dummy_jobs/jobs.rec", recordType: "job_offer"}
+    res <- runCommand'' $ RecSelCommand {filePath: "/tmp/dummy_jobs/jobs.rec", recordType: "job_offer"}
     logShow res
 
+runCli cmd = launchAff_ do
+  res <- runCommand'' cmd
+  logShow res
+
 runCommand ∷ String → Array String → Aff (Either CommandError String)
 runCommand cmd args = do
   process <- execa cmd args identity
@@ -32,8 +36,6 @@ runCommand cmd args = do
 
   pure ret
 
-data CommandWithOpts = CommandWithOpts String (Array String)
-type CommandError = Tuple Exit String
 
 runCommand' ∷ forall e a. (String -> a) -> (CommandError -> e) -> CommandWithOpts → Aff (Either e a)
 runCommand' handleRes handleErr (CommandWithOpts cmd args) = do
@@ -46,6 +48,28 @@ runCommand' handleRes handleErr (CommandWithOpts cmd args) = do
 
   pure ret
 
+runCommand'' ∷ forall cmd res err. HighLevelCommand cmd res err => cmd → Aff (Either err res)
+runCommand'' hlCommand = do
+  let
+    (CommandWithOpts cmd args) = toCommand hlCommand
+  res <- runCommand cmd args
+
+  let
+    ret = case res of
+      Right s -> Right $ toResult @cmd s
+      Left err -> Left $ fromError @cmd err
+
+  pure ret
+
+
+data CommandWithOpts = CommandWithOpts String (Array String)
+type CommandError = Tuple Exit String
+
+class HighLevelCommand cmd res err | cmd -> res, cmd -> err where
+  toCommand :: cmd -> CommandWithOpts
+  toResult :: String -> res
+  fromError :: CommandError -> err
+
 data RecSelCommand = RecSelCommand {filePath :: String, recordType :: String}
 data RecSelCommandResult = RecSelCommandResult String
 
@@ -57,11 +81,9 @@ data RecSelCommandError = RecSelCommandError CommandError
 derive instance Generic RecSelCommandError _
 instance Show RecSelCommandError where show = genericShow
 
-toCommand ∷ RecSelCommand → CommandWithOpts
-toCommand (RecSelCommand {filePath, recordType}) = CommandWithOpts "recsel" [filePath, "-t", recordType]
+instance HighLevelCommand RecSelCommand RecSelCommandResult RecSelCommandError where
+  toCommand (RecSelCommand {filePath, recordType}) = CommandWithOpts "recsel" [filePath, "-t", recordType]
 
-toResult :: String -> RecSelCommandResult
-toResult s = RecSelCommandResult s
+  toResult s = RecSelCommandResult s
 
-fromError :: CommandError -> RecSelCommandError
-fromError t = RecSelCommandError t
+  fromError t = RecSelCommandError t