Skip to content

Commit d2a7b28

Browse files
committed
Main: refactor
1 parent fea76d9 commit d2a7b28

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

main/Main.hs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE MultiWayIf #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -66,7 +67,7 @@ main = do
6667
(filePaths opts)
6768
)
6869
(\ x ->
69-
-- use Text as in the base case, requires changing FilePath -> Text
70+
-- We can start use Text as in the base case, requires changing FilePath -> Text
7071
traverse_ (processFile opts) . lines =<< liftIO
7172
(case x of
7273
"-" -> getContents -- get user input
@@ -129,57 +130,36 @@ main = do
129130
(evaluate opts)
130131

131132
process opts mpath expr
132-
| evaluate opts
133-
, tracing opts
134-
= evaluateExpression mpath Nix.nixTracingEvalExprLoc printer expr
135-
| evaluate opts
136-
, Just path <- reduce opts
137-
= evaluateExpression mpath (reduction path) printer expr
138-
| evaluate opts
139-
, not (null (arg opts) && null (argstr opts))
140-
= evaluateExpression mpath Nix.nixEvalExprLoc printer expr
141-
| evaluate opts
142-
= processResult printer =<< Nix.nixEvalExprLoc mpath expr
143-
| xml opts
144-
= error "Rendering expression trees to XML is not yet implemented"
145-
| json opts
146-
= error "Rendering expression trees to JSON is not implemented"
147-
| verbose opts >= DebugInfo
148-
= liftIO $ putStr $ PS.ppShow $ stripAnnotation expr
133+
| evaluate opts =
134+
if
135+
| tracing opts -> evaluateExpression mpath Nix.nixTracingEvalExprLoc printer expr
136+
| Just path <- reduce opts -> evaluateExpression mpath (reduction path) printer expr
137+
| not ( null (arg opts)
138+
&& null (argstr opts)
139+
) -> evaluateExpression mpath Nix.nixEvalExprLoc printer expr
140+
| otherwise -> processResult printer =<< Nix.nixEvalExprLoc mpath expr
141+
| xml opts = error "Rendering expression trees to XML is not yet implemented"
142+
| json opts = error "Rendering expression trees to JSON is not implemented"
143+
| verbose opts >= DebugInfo = liftIO $ putStr $ PS.ppShow $ stripAnnotation expr
149144
| cache opts
150-
, Just path <- mpath
151-
= liftIO $ writeCache (addExtension (dropExtension path) "nixc") expr
152-
| parseOnly opts
153-
= void $ liftIO $ Exc.evaluate $ Deep.force expr
154-
| otherwise
155-
= liftIO
156-
$ renderIO stdout
157-
. layoutPretty (LayoutOptions $ AvailablePerLine 80 0.4)
158-
. prettyNix
159-
. stripAnnotation
160-
$ expr
145+
, Just path <- mpath = liftIO $ writeCache (addExtension (dropExtension path) "nixc") expr
146+
| parseOnly opts = void $ liftIO $ Exc.evaluate $ Deep.force expr
147+
| otherwise =
148+
liftIO $
149+
renderIO
150+
stdout
151+
. layoutPretty (LayoutOptions $ AvailablePerLine 80 0.4)
152+
. prettyNix
153+
. stripAnnotation $
154+
expr
161155
where
162156
printer
163-
| finder opts
164-
= fromValue @(AttrSet (StdValue (StandardT (StdIdT IO)))) >=> findAttrs
165-
| xml opts
166-
= liftIO
167-
. putStrLn
168-
. Text.unpack
169-
. stringIgnoreContext
170-
. toXML
171-
<=< normalForm
172-
| json opts
173-
= liftIO
174-
. Text.putStrLn
175-
. stringIgnoreContext
176-
<=< nvalueToJSONNixString
177-
| strict opts
178-
= liftIO . print . prettyNValue <=< normalForm
179-
| values opts
180-
= liftIO . print . prettyNValueProv <=< removeEffects
181-
| otherwise
182-
= liftIO . print . prettyNValue <=< removeEffects
157+
| finder opts = findAttrs <=< fromValue @(AttrSet (StdValue (StandardT (StdIdT IO))))
158+
| xml opts = liftIO . putStrLn . Text.unpack . stringIgnoreContext . toXML <=< normalForm
159+
| json opts = liftIO . Text.putStrLn . stringIgnoreContext <=< nvalueToJSONNixString
160+
| strict opts = liftIO . print . prettyNValue <=< normalForm
161+
| values opts = liftIO . print . prettyNValueProv <=< removeEffects
162+
| otherwise = liftIO . print . prettyNValue <=< removeEffects
183163
where
184164
findAttrs
185165
:: AttrSet (StdValue (StandardT (StdIdT IO)))

0 commit comments

Comments
 (0)