summaryrefslogtreecommitdiff
path: root/src/Script
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2026-09-05 10:16:20 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2026-09-06 20:23:58 +0200
commit28f8970012faaae85cf730e879cfb6bdcefa9bb7 (patch)
treee5d6d2084113997804ac10beb644342b7210144a /src/Script
parent9f66b6432fd8025f115289e6b2a9db1579385634 (diff)
Refactor shell expansion conversions using template function
Diffstat (limited to 'src/Script')
-rw-r--r--src/Script/Shell.hs31
1 files changed, 17 insertions, 14 deletions
diff --git a/src/Script/Shell.hs b/src/Script/Shell.hs
index e7c8542..c89e5af 100644
--- a/src/Script/Shell.hs
+++ b/src/Script/Shell.hs
@@ -99,24 +99,27 @@ instance ExprType ShellCommand where
instance ExprType ShellArguments where
textExprType _ = T.pack "ShellArguments"
textExprValue _ = "<shell-arguments>"
-
- exprExpansionConvFrom = listToMaybe $ catMaybes
- [ cast (ShellArguments . (: []) . ShellArgument)
- , cast (ShellArguments . (: []) . ShellArgument . T.pack . show @Integer)
- , cast (ShellArguments . (: []) . ShellArgument . T.pack . show @Scientific)
- , cast (ShellArguments . map ShellArgument)
- , cast (ShellArguments . map (ShellArgument . T.pack . show @Integer))
- , cast (ShellArguments . map (ShellArgument . T.pack . show @Scientific))
- ]
+ exprExpansionConvFrom = shellExpansionTemplate
+ (Just (ShellArguments . (: []) . ShellArgument))
+ (Just (ShellArguments . map ShellArgument))
instance ExprType ShellArgument where
textExprType _ = T.pack "ShellArgument"
textExprValue _ = "<shell-argument>"
-
- exprExpansionConvFrom = listToMaybe $ catMaybes
- [ cast (ShellArgument)
- , cast (ShellArgument . T.pack . show @Integer)
- , cast (ShellArgument . T.pack . show @Scientific)
+ exprExpansionConvFrom = shellExpansionTemplate (Just ShellArgument) Nothing
+
+
+shellExpansionTemplate :: forall a b. (Typeable a, ExprType b) => Maybe (Text -> a) -> Maybe ([ Text ] -> a) -> Maybe (b -> a)
+shellExpansionTemplate fromSingle fromList = listToMaybe $ catMaybes
+ [ single id
+ , single (T.pack . show @Integer)
+ , single (T.pack . show @Scientific)
+ ]
+ where
+ single :: forall c. (ExprType c) => (c -> Text) -> Maybe (b -> a)
+ single conv = listToMaybe $ catMaybes
+ [ fromSingle >>= \f -> cast (f . conv)
+ , fromList >>= \f -> cast (f . map conv)
]