@@ -1,13 +1,25 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−
3−module Language.Nix.Path where
1+{-# LANGUAGE PatternGuards #-}
2+module Language.Nix.Path ( Path, mkPath, path ) where
43
54import Control.Lens
5+import Control.Applicative
66import Data.String
7−import Distribution.Nix.Util.PrettyPrinting -- ( Pretty(..), text )
7+import Distribution.Nix.Util.PrettyPrinting ( Pretty(..), text, punctuate, hcat, char )
88import Language.Nix.Identifier
99
1010-- | Paths are non-empty lists of identifiers in Nix.
1111
12−newtype Path = Path [Identifier]
13− deriving (Show, Eq)
12+newtype Path = Path { _segments :: [Identifier] }
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