Skip to content

Commit b55ed28

Browse files
Merge #979: Path type boundary, improving Annotated
After `String` got prunned, what is left is `FilePath` everywhere. This PR approaches this (#912) with the first step - differentiating between path information & `String`, which this is doing. The outside layer & optpatse stuff `{Nix, Nix.Options, Shorthands}` externally use `FilePath` to not scare the most non-involved users of library with `Path` type, thou all it requires is coerce. Maybe it would've been a good Haskell practive for libraries to use newtype wrappers & coerce for type boundaries, in that way ecosystem architecture would've be much more flexible & already solving the data types situations, but I digress. Further allowign to use something like `path` package & migrating more processing to `Text`. Further what should be looked into is returning pretty paths. Module Annotated got a big refactor, rework, clean-up.
2 parents dd2d300 + f4c82ba commit b55ed28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+891
-805
lines changed

benchmarks/ParserBench.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module ParserBench (benchmarks) where
22

3+
import Nix.Utils
34
import Nix.Parser
45

56
import Criterion
67

7-
benchFile :: FilePath -> Benchmark
8-
benchFile = bench <*> whnfIO . parseNixFile . ("data/" <>)
8+
benchFile :: Path -> Benchmark
9+
benchFile = bench . coerce <*> whnfIO . parseNixFile . ("data/" <>)
910

1011
benchmarks :: Benchmark
1112
benchmarks = bgroup

hnix.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ library
485485
, TupleSections
486486
, LambdaCase
487487
, BangPatterns
488+
, ViewPatterns
488489
if flag(optimize)
489490
default-extensions:
490491
ApplicativeDo
@@ -551,6 +552,7 @@ executable hnix
551552
, TupleSections
552553
, LambdaCase
553554
, BangPatterns
555+
, ViewPatterns
554556
if flag(optimize)
555557
default-extensions:
556558
ApplicativeDo
@@ -629,6 +631,7 @@ test-suite hnix-tests
629631
, TupleSections
630632
, LambdaCase
631633
, BangPatterns
634+
, ViewPatterns
632635
if flag(optimize)
633636
default-extensions:
634637
ApplicativeDo
@@ -681,6 +684,7 @@ benchmark hnix-benchmarks
681684
, TupleSections
682685
, LambdaCase
683686
, BangPatterns
687+
, ViewPatterns
684688
if flag(optimize)
685689
default-extensions:
686690
ApplicativeDo

main/Main.hs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
{-# LANGUAGE MultiWayIf #-}
2-
{-# LANGUAGE ScopedTypeVariables #-}
3-
{-# LANGUAGE TypeFamilies #-}
4-
{-# LANGUAGE ViewPatterns #-}
5-
{-# LANGUAGE RecordWildCards #-}
1+
{-# language MultiWayIf #-}
2+
{-# language ScopedTypeVariables #-}
3+
{-# language TypeFamilies #-}
4+
{-# language RecordWildCards #-}
65

76
module Main ( main ) where
87

@@ -68,7 +67,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
6867
case filePaths of
6968
[] -> runRepl
7069
["-"] -> readExpressionFromStdin
71-
_paths -> processSeveralFiles _paths
70+
_paths -> processSeveralFiles (coerce _paths)
7271
where
7372
-- | Fall back to running the REPL
7473
runRepl = withEmptyNixContext Repl.main
@@ -87,18 +86,18 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
8786
(\binaryCacheFile ->
8887
do
8988
let file = replaceExtension binaryCacheFile "nixc"
90-
processCLIOptions (Just file) =<< liftIO (readCache binaryCacheFile)
89+
processCLIOptions (Just $ coerce file) =<< liftIO (readCache $ coerce $ binaryCacheFile)
9190
) <$> readFrom
9291

9392
-- | The `--expr` option: read expression from the argument string
9493
loadLiteralExpression = processExpr <$> expression
9594

9695
-- | The `--file` argument: read expressions from the files listed in the argument file
9796
loadExpressionFromFile =
98-
-- We can start use Text as in the base case, requires changing FilePath -> Text
97+
-- We can start use Text as in the base case, requires changing Path -> Text
9998
-- But that is a gradual process:
10099
-- https://github.com/haskell-nix/hnix/issues/912
101-
(processSeveralFiles . String.lines <=< liftIO) .
100+
(processSeveralFiles . (coerce <$>) . String.lines <=< liftIO) .
102101
(\case
103102
"-" -> getContents
104103
_fp -> readFile _fp
@@ -148,25 +147,25 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
148147
bool
149148
Repl.main
150149
(do
151-
val <- nixEvalExprLoc mpath expr
150+
val <- nixEvalExprLoc (coerce mpath) expr
152151
Repl.main' $ pure val
153152
)
154153
evaluate
155154
)
156155

157156
-- 2021-07-15: NOTE: Logic of CLI Option processing is scattered over several functions, needs to be consolicated.
158-
processCLIOptions :: Maybe FilePath -> NExprLoc -> StandardT (StdIdT IO) ()
157+
processCLIOptions :: Maybe Path -> NExprLoc -> StandardT (StdIdT IO) ()
159158
processCLIOptions mpath expr
160159
| evaluate =
161160
if
162161
| tracing -> evaluateExprWithEvaluator nixTracingEvalExprLoc expr
163-
| Just path <- reduce -> evaluateExprWithEvaluator (reduction path) expr
162+
| Just path <- reduce -> evaluateExprWithEvaluator (reduction (coerce path) . coerce) expr
164163
| null arg || null argstr -> evaluateExprWithEvaluator nixEvalExprLoc expr
165-
| otherwise -> processResult printer <=< nixEvalExprLoc mpath $ expr
164+
| otherwise -> processResult printer <=< nixEvalExprLoc (coerce mpath) $ expr
166165
| xml = fail "Rendering expression trees to XML is not yet implemented"
167166
| json = fail "Rendering expression trees to JSON is not implemented"
168167
| verbose >= DebugInfo = liftIO . putStr . ppShow . stripAnnotation $ expr
169-
| cache , Just path <- mpath = liftIO . writeCache (replaceExtension path "nixc") $ expr
168+
| cache , Just path <- mpath = liftIO . writeCache (coerce $ replaceExtension (coerce path) "nixc") $ expr
170169
| parseOnly = void . liftIO . Exception.evaluate . force $ expr
171170
| otherwise =
172171
liftIO .
@@ -177,7 +176,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
177176
. stripAnnotation
178177
$ expr
179178
where
180-
evaluateExprWithEvaluator evaluator = evaluateExpression mpath evaluator printer
179+
evaluateExprWithEvaluator evaluator = evaluateExpression (coerce mpath) evaluator printer
181180

182181
printer
183182
| finder = findAttrs <=< fromValue @(AttrSet (StdValue (StandardT (StdIdT IO))))
@@ -307,10 +306,10 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
307306

308307
handleReduced
309308
:: (MonadThrow m, MonadIO m)
310-
=> FilePath
309+
=> Path
311310
-> (NExprLoc, Either SomeException (NValue t f m))
312311
-> m (NValue t f m)
313-
handleReduced path (expr', eres) =
312+
handleReduced (coerce -> path) (expr', eres) =
314313
do
315314
liftIO $
316315
do

main/Repl.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
directory for more details.
88
-}
99

10-
{-# LANGUAGE MultiWayIf #-}
11-
{-# LANGUAGE ScopedTypeVariables #-}
12-
{-# LANGUAGE ViewPatterns #-}
10+
{-# language MultiWayIf #-}
11+
{-# language ScopedTypeVariables #-}
1312

1413
module Repl
1514
( main

src/Nix.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
{-# LANGUAGE ScopedTypeVariables #-}
2-
{-# LANGUAGE ViewPatterns #-}
1+
{-# language ScopedTypeVariables #-}
32

43
module Nix
54
( module Nix.Cache
65
, module Nix.Exec
7-
, module Nix.Expr
6+
, module Nix.Expr.Types
7+
, module Nix.Expr.Shorthands
8+
, module Nix.Expr.Types.Annotated
89
, module Nix.Frames
910
, module Nix.Render.Frame
1011
, module Nix.Normal
@@ -35,7 +36,9 @@ import Nix.Builtins
3536
import Nix.Cache
3637
import qualified Nix.Eval as Eval
3738
import Nix.Exec
38-
import Nix.Expr
39+
import Nix.Expr.Types
40+
import Nix.Expr.Shorthands
41+
import Nix.Expr.Types.Annotated
3942
import Nix.Frames
4043
import Nix.String
4144
import Nix.Normal
@@ -60,7 +63,7 @@ nixEval
6063
-> Maybe FilePath
6164
-> Fix g
6265
-> m a
63-
nixEval transform alg mpath = withNixContext mpath . adi transform alg
66+
nixEval transform alg mpath = withNixContext (coerce mpath) . adi transform alg
6467

6568
-- | Evaluate a nix expression in the default context
6669
nixEvalExpr
@@ -92,7 +95,7 @@ nixTracingEvalExprLoc
9295
=> Maybe FilePath
9396
-> NExprLoc
9497
-> m (NValue t f m)
95-
nixTracingEvalExprLoc mpath = withNixContext mpath . evalExprLoc
98+
nixTracingEvalExprLoc mpath = withNixContext (coerce mpath) . evalExprLoc
9699

97100
evaluateExpression
98101
:: (MonadNix e t f m, Has e Options)

src/Nix/Atoms.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{-# LANGUAGE CPP #-}
2-
{-# LANGUAGE DeriveAnyClass #-}
1+
{-# language CPP #-}
2+
{-# language DeriveAnyClass #-}
33

44
module Nix.Atoms where
55

0 commit comments

Comments
 (0)