@@ -1,13 +1,25 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1+{-# LANGUAGE PatternGuards #-}
3−module Language.Nix.Path where
2+module Language.Nix.Path ( Path, mkPath, path ) where
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
10-- | Paths are non-empty lists of identifiers in Nix.
10-- | Paths are non-empty lists of identifiers in Nix.
12−newtype Path = Path [Identifier]
12+newtype Path = Path { _segments :: [Identifier] }
13− deriving (Show, Eq)
13+ deriving (Show, Eq, Ord)
15+mkPath :: [Identifier] -> Path
16+mkPath [] = error "Nix paths cannot be empty"
18+ | [Identifier s] <- p, needsQuoting s = error ("invalid Nix path: " ++ show p)
21+path :: Lens' Path [Identifier]
22+path f (Path ids) = mkPath `fmap` f ids
24+instance Pretty Path where
25+ pPrint p = hcat $ punctuate (char '.') $ pPrint <$> p^.path