{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UnliftedFFITypes #-}
module System.OsPath.Internal where
import {-# SOURCE #-} System.OsPath
( isValid )
import System.OsPath.Types
import qualified System.OsString.Internal as OS
import Control.Monad.Catch
( MonadThrow )
import Data.ByteString
( ByteString )
import Language.Haskell.TH.Quote
( QuasiQuoter (..) )
import Language.Haskell.TH.Syntax
( Lift (..), lift )
import GHC.IO.Encoding.Failure ( CodingFailureMode(..) )
import System.OsString.Internal.Types
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
import qualified System.OsPath.Windows as PF
import System.IO
( TextEncoding, utf16le )
import GHC.IO.Encoding.UTF16 ( mkUTF16le )
import qualified System.OsPath.Data.ByteString.Short.Word16 as BS16
import qualified System.OsPath.Data.ByteString.Short as BS8
#else
import qualified System.OsPath.Posix as PF
import System.OsPath.Encoding
import System.IO
( TextEncoding )
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
import Control.Monad (when)
#endif
encodeUtf :: MonadThrow m => FilePath -> m OsPath
encodeUtf :: FilePath -> m OsPath
encodeUtf = FilePath -> m OsPath
forall (m :: * -> *). MonadThrow m => FilePath -> m OsPath
OS.encodeUtf
encodeWith :: TextEncoding
-> TextEncoding
-> FilePath
-> Either EncodingException OsPath
encodeWith :: TextEncoding
-> TextEncoding -> FilePath -> Either EncodingException OsPath
encodeWith = TextEncoding
-> TextEncoding -> FilePath -> Either EncodingException OsPath
OS.encodeWith
encodeFS :: FilePath -> IO OsPath
encodeFS :: FilePath -> IO OsPath
encodeFS = FilePath -> IO OsPath
OS.encodeFS
decodeUtf :: MonadThrow m => OsPath -> m FilePath
decodeUtf :: OsPath -> m FilePath
decodeUtf = OsPath -> m FilePath
forall (m :: * -> *). MonadThrow m => OsPath -> m FilePath
OS.decodeUtf
decodeWith :: TextEncoding
-> TextEncoding
-> OsPath
-> Either EncodingException FilePath
decodeWith :: TextEncoding
-> TextEncoding -> OsPath -> Either EncodingException FilePath
decodeWith = TextEncoding
-> TextEncoding -> OsPath -> Either EncodingException FilePath
OS.decodeWith
decodeFS :: OsPath -> IO FilePath
decodeFS :: OsPath -> IO FilePath
decodeFS = OsPath -> IO FilePath
OS.decodeFS
fromBytes :: MonadThrow m
=> ByteString
-> m OsPath
fromBytes :: ByteString -> m OsPath
fromBytes = ByteString -> m OsPath
forall (m :: * -> *). MonadThrow m => ByteString -> m OsPath
OS.fromBytes
osp :: QuasiQuoter
osp :: QuasiQuoter
osp = QuasiQuoter :: (FilePath -> Q Exp)
-> (FilePath -> Q Pat)
-> (FilePath -> Q Type)
-> (FilePath -> Q [Dec])
-> QuasiQuoter
QuasiQuoter
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
{ quoteExp = \s -> do
osp' <- either (fail . show) (pure . OsString) . PF.encodeWith (mkUTF16le ErrorOnCodingFailure) $ s
when (not $ isValid osp') $ fail ("filepath now valid: " <> show osp')
lift osp'
, quotePat = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
, quoteType = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
, quoteDec = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
}
#else
{ quoteExp :: FilePath -> Q Exp
quoteExp = \FilePath
s -> do
OsPath
osp' <- (EncodingException -> Q OsPath)
-> (PlatformString -> Q OsPath)
-> Either EncodingException PlatformString
-> Q OsPath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FilePath -> Q OsPath
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail (FilePath -> Q OsPath)
-> (EncodingException -> FilePath) -> EncodingException -> Q OsPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodingException -> FilePath
forall a. Show a => a -> FilePath
show) (OsPath -> Q OsPath
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OsPath -> Q OsPath)
-> (PlatformString -> OsPath) -> PlatformString -> Q OsPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlatformString -> OsPath
OsString) (Either EncodingException PlatformString -> Q OsPath)
-> (FilePath -> Either EncodingException PlatformString)
-> FilePath
-> Q OsPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding -> FilePath -> Either EncodingException PlatformString
PF.encodeWith (CodingFailureMode -> TextEncoding
mkUTF8 CodingFailureMode
ErrorOnCodingFailure) (FilePath -> Q OsPath) -> FilePath -> Q OsPath
forall a b. (a -> b) -> a -> b
$ FilePath
s
Bool -> Q () -> Q ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ OsPath -> Bool
isValid OsPath
osp') (Q () -> Q ()) -> Q () -> Q ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Q ()
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail (FilePath
"filepath now valid: " FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> OsPath -> FilePath
forall a. Show a => a -> FilePath
show OsPath
osp')
OsPath -> Q Exp
forall t. Lift t => t -> Q Exp
lift OsPath
osp'
, quotePat :: FilePath -> Q Pat
quotePat = \FilePath
_ ->
FilePath -> Q Pat
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"illegal QuasiQuote (allowed as expression only, used as a pattern)"
, quoteType :: FilePath -> Q Type
quoteType = \FilePath
_ ->
FilePath -> Q Type
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"illegal QuasiQuote (allowed as expression only, used as a type)"
, quoteDec :: FilePath -> Q [Dec]
quoteDec = \FilePath
_ ->
FilePath -> Q [Dec]
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"illegal QuasiQuote (allowed as expression only, used as a declaration)"
}
#endif
unpack :: OsPath -> [OsChar]
unpack :: OsPath -> [OsChar]
unpack = OsPath -> [OsChar]
OS.unpack
pack :: [OsChar] -> OsPath
pack :: [OsChar] -> OsPath
pack = [OsChar] -> OsPath
OS.pack