From 28f8970012faaae85cf730e879cfb6bdcefa9bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 5 Sep 2026 10:16:20 +0200 Subject: Refactor shell expansion conversions using template function --- src/Script/Shell.hs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src') 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 _ = "" - - 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 _ = "" - - 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) ] -- cgit v1.2.3