-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2015 Dimitri Sabadie
-- License     : BSD3
--
-- Maintainer  : Dimitri Sabadie <dimitri.sabadie@gmail.com>
-- Stability   : experimental
-- Portability : portable
--
-----------------------------------------------------------------------------

module Codec.Wavefront.IO where

import Codec.Wavefront.Lexer ( lexer )
import Codec.Wavefront.Object ( WavefrontOBJ, ctxtToWavefrontOBJ )
import Codec.Wavefront.Token ( tokenize )
import Control.Monad.IO.Class ( MonadIO(..) )
import qualified Data.Text.IO as T ( readFile )

-- |Extract a 'WavefrontOBJ' from a Wavefront OBJ formatted file.
fromFile :: (MonadIO m) => FilePath -> m (Either String WavefrontOBJ)
fromFile :: FilePath -> m (Either FilePath WavefrontOBJ)
fromFile fd :: FilePath
fd = IO (Either FilePath WavefrontOBJ)
-> m (Either FilePath WavefrontOBJ)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either FilePath WavefrontOBJ)
 -> m (Either FilePath WavefrontOBJ))
-> IO (Either FilePath WavefrontOBJ)
-> m (Either FilePath WavefrontOBJ)
forall a b. (a -> b) -> a -> b
$ (Text -> Either FilePath WavefrontOBJ)
-> IO Text -> IO (Either FilePath WavefrontOBJ)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TokenStream -> WavefrontOBJ)
-> Either FilePath TokenStream -> Either FilePath WavefrontOBJ
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Ctxt -> WavefrontOBJ
ctxtToWavefrontOBJ (Ctxt -> WavefrontOBJ)
-> (TokenStream -> Ctxt) -> TokenStream -> WavefrontOBJ
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TokenStream -> Ctxt
lexer) (Either FilePath TokenStream -> Either FilePath WavefrontOBJ)
-> (Text -> Either FilePath TokenStream)
-> Text
-> Either FilePath WavefrontOBJ
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either FilePath TokenStream
tokenize) (FilePath -> IO Text
T.readFile FilePath
fd)