Add `printNix` that prints value like nix does

This commit is contained in:
Guillaume Maudoux 2018-02-09 15:27:11 +01:00
parent 5718952e7b
commit 507a1f24a3
1 changed files with 14 additions and 1 deletions

View File

@ -6,7 +6,7 @@ import Data.Fix
import Data.Map (toList)
import Data.Maybe (isJust)
import Data.Text (Text, pack, unpack, replace, strip)
import Data.List (isPrefixOf)
import Data.List (isPrefixOf, intercalate)
import Nix.Atoms
import Nix.Eval (NValue, NValueF (..), atomText)
import Nix.Expr
@ -188,3 +188,16 @@ prettyNixValue = prettyNix . valueToExpr
go (NVBuiltin1 name _) = NSym $ Text.pack $ "builtins." ++ name
go (NVBuiltin2 name _ _) = NSym $ Text.pack $ "builtins." ++ name
printNix :: Functor m => NValue m -> String
printNix = cata phi
where phi :: NValueF m String -> String
phi (NVConstant a) = unpack $ atomText a
phi (NVStr t) = unpack t
phi (NVList l) = "[ " ++ (intercalate " " l) ++ " ]"
phi (NVSet s) = intercalate ", " $ [ unpack k ++ ":" ++ v | (k, v) <- toList s]
phi (NVFunction p _) = error "Cannot print a thunk"
phi (NVLiteralPath fp) = fp
phi (NVEnvPath p) = p
phi (NVBuiltin1 name _) = error "Cannot print a thunk"
phi (NVBuiltin2 name _ _) = error "Cannot print a thunk"