git.iko.soy/typeable/cabal2nix
on 560fb2b help sign in
47 lines 1.6 kB history raw
1module Language.Nix.Path ( Path, path ) where
3import Internal.Lens
4import Internal.PrettyPrinting ( Pretty (..), char, hcat, punctuate )
5import Language.Nix.Identifier
7-- | Paths are non-empty lists of identifiers in Nix.
8--
9-- >>> set path [set ident "yo" undefined] undefined
10-- Path {_segments = [Identifier "yo"]}
11--
12-- Any attempt to construct the empty path throws an 'error':
13--
14-- >>> set path [] undefined
15-- Path {_segments = *** Exception: Nix paths cannot be empty
16--
17-- 'Identifier' is an instance of 'IsString':
18--
19-- >>> :set -XOverloadedStrings
20-- >>> pPrint $ set path ["yo","bar"] undefined
21-- yo.bar
22--
23-- Freaky quoted identifiers are fine except in the first segment:
24--
25-- >>> pPrint $ set path ["yo","b\"ar"] undefined
26-- yo."b\"ar"
27-- >>> pPrint $ set path ["5ident"] undefined
28-- *** Exception: invalid Nix path: [Identifier "5ident"]
29-- >>> pPrint $ set path ["5ident","foo","bar"] undefined
30-- *** Exception: invalid Nix path: [Identifier "5ident",Identifier "foo",Identifier "bar"]
32newtype Path = Path { _segments :: [Identifier] }
33 deriving (Show, Eq, Ord)
35instance Default Path where def = Path (error "undefined Nix.Path")
37path :: Lens' Path [Identifier]
38path f (Path ids) = mkPath `fmap` f ids
39 where
40 mkPath :: [Identifier] -> Path
41 mkPath [] = error "Nix paths cannot be empty"
42 mkPath p@(s0:_)
43 | s0^.ident.to needsQuoting = error ("invalid Nix path: " ++ show p)
44 | otherwise = Path p
46instance Pretty Path where
47 pPrint p = hcat $ punctuate (char '.') $ pPrint <$> p^.path