{-# LANGUAGE PatternSynonyms #-}-- | C-backed context-bearing strings via FFI.---- Wraps @cbits/nn_ctxstr.c@ - a string with its StringContext stored-- as a contiguous C struct, its text and each store path's hash and name-- and output name a string's bytes ("Nix.Eval.CStr"). Context-free-- strings use thunk tag 4 (a string's bytes alone); this module handles-- tag 8 for strings with non-empty context.---- This module provides raw FFI bindings only. The higher-level-- marshalling between t'Nix.Eval.Types.StringContext' and C is in "Nix.Eval.Types"-- (to avoid circular imports).module Nix.Eval.CCtxStr ( -- * Opaque handle NnCtxStr, CCtxStrPtr, -- * Lifecycle cctxstrNew, -- * Element setters cctxstrSetPlain, cctxstrSetDrvOutput, cctxstrSetAllOutputs, -- * Accessors cctxstrText, cctxstrCtxCount, cctxstrElemTag, cctxstrElemHash, cctxstrElemName, cctxstrElemOutput, -- * Element tags pattern ScePlain, pattern SceDrvOutput, pattern SceAllOutputs, )whereimport Data.Word (Word32, Word8)import Foreign.Ptr (Ptr)import Nix.Eval.CData (MonadCData, cdataIO)import Nix.Eval.CStatus (checkedCPtr)import Nix.Eval.CStr (CStrPtr)-- | Phantom type for C-side @nn_ctxstr_t@.data NnCtxStr-- | Pointer to a C-allocated context string.type CCtxStrPtr = Ptr NnCtxStr-- ----------------------------------------------------------------------------- FFI imports (all unsafe - no callbacks, fast data access)-- ---------------------------------------------------------------------------foreign import ccall unsafe "nn_ctxstr_new" c_nn_ctxstr_new :: CStrPtr -> Word32 -> IO CCtxStrPtrforeign import ccall unsafe "nn_ctxstr_set_plain" c_nn_ctxstr_set_plain :: CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> IO ()foreign import ccall unsafe "nn_ctxstr_set_drv_output" c_nn_ctxstr_set_drv_output :: CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> CStrPtr -> IO ()foreign import ccall unsafe "nn_ctxstr_set_all_outputs" c_nn_ctxstr_set_all_outputs :: CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> IO ()foreign import ccall unsafe "nn_ctxstr_text" c_nn_ctxstr_text :: CCtxStrPtr -> IO CStrPtrforeign import ccall unsafe "nn_ctxstr_ctx_count" c_nn_ctxstr_ctx_count :: CCtxStrPtr -> IO Word32foreign import ccall unsafe "nn_ctxstr_elem_tag" c_nn_ctxstr_elem_tag :: CCtxStrPtr -> Word32 -> IO Word8foreign import ccall unsafe "nn_ctxstr_elem_hash" c_nn_ctxstr_elem_hash :: CCtxStrPtr -> Word32 -> IO CStrPtrforeign import ccall unsafe "nn_ctxstr_elem_name" c_nn_ctxstr_elem_name :: CCtxStrPtr -> Word32 -> IO CStrPtrforeign import ccall unsafe "nn_ctxstr_elem_output" c_nn_ctxstr_elem_output :: CCtxStrPtr -> Word32 -> IO CStrPtr-- ----------------------------------------------------------------------------- Lifecycle-- ----------------------------------------------------------------------------- | Allocate a new context string over its text's bytes with space for-- @ctxCount@ elements, which the caller fills via the set functions.-- The NULL @nn_ctxstr_new@ answers when it refuses-- (@cbits/nn_ctxstr.h@ says when) is raised as-- 'Nix.Eval.CStatus.CStatusError'.cctxstrNew :: (MonadCData m) => CStrPtr -> Word32 -> m CCtxStrPtrcctxstrNew text ctxCount = cdataIO (checkedCPtr "nn_ctxstr_new" =<< c_nn_ctxstr_new text ctxCount)-- ----------------------------------------------------------------------------- Element setters-- ----------------------------------------------------------------------------- | Set context element @i@ to a plain store-path reference (@SCPlain@),-- identified by its hash and name.cctxstrSetPlain :: (MonadCData m) => CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> m ()cctxstrSetPlain ptr idx hash name = cdataIO (c_nn_ctxstr_set_plain ptr idx hash name)-- | Set context element @i@ to a derivation-output reference (@SCDrvOutput@):-- hash and name plus the output name.cctxstrSetDrvOutput :: (MonadCData m) => CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> CStrPtr -> m ()cctxstrSetDrvOutput ptr idx hash name output = cdataIO (c_nn_ctxstr_set_drv_output ptr idx hash name output)-- | Set context element @i@ to an all-outputs reference (@SCAllOutputs@),-- identified by its hash and name.cctxstrSetAllOutputs :: (MonadCData m) => CCtxStrPtr -> Word32 -> CStrPtr -> CStrPtr -> m ()cctxstrSetAllOutputs ptr idx hash name = cdataIO (c_nn_ctxstr_set_all_outputs ptr idx hash name)-- ----------------------------------------------------------------------------- Accessors-- ----------------------------------------------------------------------------- | The context string's text.cctxstrText :: (MonadCData m) => CCtxStrPtr -> m CStrPtrcctxstrText = cdataIO . c_nn_ctxstr_text-- | The number of context elements attached to the string.cctxstrCtxCount :: (MonadCData m) => CCtxStrPtr -> m Word32cctxstrCtxCount = cdataIO . c_nn_ctxstr_ctx_count-- | The tag of context element @i@: 'ScePlain', 'SceDrvOutput' or-- 'SceAllOutputs'.cctxstrElemTag :: (MonadCData m) => CCtxStrPtr -> Word32 -> m Word8cctxstrElemTag ptr idx = cdataIO (c_nn_ctxstr_elem_tag ptr idx)-- | The store path's hash of context element @i@.cctxstrElemHash :: (MonadCData m) => CCtxStrPtr -> Word32 -> m CStrPtrcctxstrElemHash ptr idx = cdataIO (c_nn_ctxstr_elem_hash ptr idx)-- | The store path's name of context element @i@.cctxstrElemName :: (MonadCData m) => CCtxStrPtr -> Word32 -> m CStrPtrcctxstrElemName ptr idx = cdataIO (c_nn_ctxstr_elem_name ptr idx)-- | The output name of context element @i@ (drv-output elements only).cctxstrElemOutput :: (MonadCData m) => CCtxStrPtr -> Word32 -> m CStrPtrcctxstrElemOutput ptr idx = cdataIO (c_nn_ctxstr_elem_output ptr idx)-- ----------------------------------------------------------------------------- Element tags-- ----------------------------------------------------------------------------- | The tags of a string context element, mirroring the @NN_SCE_*@ macros-- in @cbits\/nn_ctxstr.h@, with which they stay in lockstep: a plain-- store path (@SCPlain@), a derivation's output (@SCDrvOutput@) and all-- of a derivation's outputs (@SCAllOutputs@).pattern ScePlain, SceDrvOutput, SceAllOutputs :: Word8pattern ScePlain = 0pattern SceDrvOutput = 1pattern SceAllOutputs = 2