git.iko.soy/typeable/cabal2nix
on master help sign in
Clean up Path type.
3226a8aPeter Simons
src/Language/Nix/Path.hs+18−6
@@ -1,13 +1,25 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1+{-# LANGUAGE PatternGuards #-}
2−
3−module Language.Nix.Path where
2+module Language.Nix.Path ( Path, mkPath, path ) where
4
3
5import Control.Lens
4import Control.Lens
5+import Control.Applicative
6import Data.String
6import Data.String
7−import Distribution.Nix.Util.PrettyPrinting -- ( Pretty(..), text )
7+import Distribution.Nix.Util.PrettyPrinting ( Pretty(..), text, punctuate, hcat, char )
8import Language.Nix.Identifier
8import Language.Nix.Identifier
9
9
10-- | Paths are non-empty lists of identifiers in Nix.
10-- | Paths are non-empty lists of identifiers in Nix.
11
11
12−newtype Path = Path [Identifier]
12+newtype Path = Path { _segments :: [Identifier] }
13− deriving (Show, Eq)
13+ deriving (Show, Eq, Ord)
14+
15+mkPath :: [Identifier] -> Path
16+mkPath [] = error "Nix paths cannot be empty"
17+mkPath p
18+ | [Identifier s] <- p, needsQuoting s = error ("invalid Nix path: " ++ show p)
19+ | otherwise = Path p
20+
21+path :: Lens' Path [Identifier]
22+path f (Path ids) = mkPath `fmap` f ids
23+
24+instance Pretty Path where
25+ pPrint p = hcat $ punctuate (char '.') $ pPrint <$> p^.path