module Text.Sass.Compilation
(
compileFile
, compileString
, SassError (errorStatus)
, errorJson
, errorText
, errorMessage
, errorFile
, errorSource
, errorLine
, errorColumn
) where
import qualified Bindings.Libsass as Lib
import Control.Monad ((>=>))
import Foreign
import Foreign.C
import Text.Sass.Internal
import Text.Sass.Options
data SassError = SassError {
errorStatus :: Int,
errorContext :: ForeignPtr Lib.SassContext
}
instance Show SassError where
show (SassError s _) =
"SassError: cannot compile provided source, error status: " ++ show s
instance Eq SassError where
(SassError s1 _) == (SassError s2 _) = s1 == s2
loadFromError :: (Ptr Lib.SassContext -> IO a)
-> (a -> IO b)
-> SassError
-> IO b
loadFromError get conv err = withForeignPtr ptr $ get >=> conv
where ptr = errorContext err
loadStringFromError :: (Ptr Lib.SassContext -> IO CString)
-> SassError
-> IO String
loadStringFromError get = loadFromError get peekCString
loadIntFromError :: (Integral a)
=> (Ptr Lib.SassContext -> IO a)
-> SassError
-> IO Int
loadIntFromError get = loadFromError get (return.fromIntegral)
errorJson :: SassError -> IO String
errorJson = loadStringFromError Lib.sass_context_get_error_json
errorText :: SassError -> IO String
errorText = loadStringFromError Lib.sass_context_get_error_text
errorMessage :: SassError -> IO String
errorMessage = loadStringFromError Lib.sass_context_get_error_message
errorFile :: SassError -> IO String
errorFile = loadStringFromError Lib.sass_context_get_error_file
errorSource :: SassError -> IO String
errorSource = loadStringFromError Lib.sass_context_get_error_src
errorLine :: SassError -> IO Int
errorLine = loadIntFromError Lib.sass_context_get_error_line
errorColumn :: SassError -> IO Int
errorColumn = loadIntFromError Lib.sass_context_get_error_column
compileInternal :: CString
-> SassOptions
-> (CString -> IO (Ptr a))
-> (Ptr a -> IO CInt)
-> (Ptr a -> IO ())
-> FinalizerPtr a
-> IO (Either SassError String)
compileInternal str opts make compile delete finalizer = do
context <- make str
let context' = castPtr context
let opts' = castPtr context
copyOptionsToNative opts opts'
status <- withFunctions opts opts' $ compile context
if status /= 0
then do
fptr <- newForeignPtr finalizer context
return $ Left $
SassError (fromIntegral status) (castForeignPtr fptr)
else do
result <- Lib.sass_context_get_output_string context'
!result' <- peekCString result
delete context
return $ Right result'
compileFile :: FilePath
-> SassOptions
-> IO (Either SassError String)
compileFile path opts = withCString path $ \cpath ->
compileInternal cpath opts
Lib.sass_make_file_context
Lib.sass_compile_file_context
Lib.sass_delete_file_context
Lib.p_sass_delete_file_context
compileString :: String
-> SassOptions
-> IO (Either SassError String)
compileString str opts = do
cdata <- newCString str
compileInternal cdata opts
Lib.sass_make_data_context
Lib.sass_compile_data_context
Lib.sass_delete_data_context
Lib.p_sass_delete_data_context