{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
#if defined(IS_WINDOWS)
#define OS_NAME Windows
#define OS_PATH WindowsPath
#else
#define OS_NAME Posix
#define OS_PATH PosixPath
#endif
module Streamly.Internal.FileSystem.OS_PATH.Seg
(
Rooted (..)
, Branch (..)
, IsSeg
, rt
, br
, rtE
, brE
, append
)
where
import Control.Monad.Catch (MonadThrow(..))
import Language.Haskell.TH (Q, Exp)
import Language.Haskell.TH.Syntax (lift)
import Language.Haskell.TH.Quote (QuasiQuoter)
import Streamly.Internal.Data.Path (IsPath(..), PathException(..))
import Streamly.Internal.FileSystem.Path.Common (mkQ)
import Streamly.Internal.FileSystem.OS_PATH (OS_PATH(..))
import qualified Streamly.Internal.FileSystem.OS_PATH as OsPath
newtype Rooted a = Rooted a
newtype Branch a = Branch a
instance IsPath OS_PATH (Rooted OS_PATH) where
unsafeFromPath :: PosixPath -> Rooted PosixPath
unsafeFromPath = PosixPath -> Rooted PosixPath
forall a. a -> Rooted a
Rooted
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Rooted PosixPath)
fromPath PosixPath
p =
if PosixPath -> Bool
OsPath.isRooted PosixPath
p
then Rooted PosixPath -> m (Rooted PosixPath)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PosixPath -> Rooted PosixPath
forall a. a -> Rooted a
Rooted PosixPath
p)
else PathException -> m (Rooted PosixPath)
forall e a. Exception e => e -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (PathException -> m (Rooted PosixPath))
-> PathException -> m (Rooted PosixPath)
forall a b. (a -> b) -> a -> b
$ [Char] -> PathException
InvalidPath
([Char] -> PathException) -> [Char] -> PathException
forall a b. (a -> b) -> a -> b
$ [Char]
"Must be a specific location, not a path segment: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ PosixPath -> [Char]
forall a. IsPath PosixPath a => a -> [Char]
OsPath.toString PosixPath
p
toPath :: Rooted PosixPath -> PosixPath
toPath (Rooted PosixPath
p) = PosixPath
p
instance IsPath OS_PATH (Branch OS_PATH) where
unsafeFromPath :: PosixPath -> Branch PosixPath
unsafeFromPath = PosixPath -> Branch PosixPath
forall a. a -> Branch a
Branch
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Branch PosixPath)
fromPath PosixPath
p =
if PosixPath -> Bool
OsPath.isBranch PosixPath
p
then Branch PosixPath -> m (Branch PosixPath)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PosixPath -> Branch PosixPath
forall a. a -> Branch a
Branch PosixPath
p)
else PathException -> m (Branch PosixPath)
forall e a. Exception e => e -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (PathException -> m (Branch PosixPath))
-> PathException -> m (Branch PosixPath)
forall a b. (a -> b) -> a -> b
$ [Char] -> PathException
InvalidPath
([Char] -> PathException) -> [Char] -> PathException
forall a b. (a -> b) -> a -> b
$ [Char]
"Must be a path segment, not a specific location: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ PosixPath -> [Char]
forall a. IsPath PosixPath a => a -> [Char]
OsPath.toString PosixPath
p
toPath :: Branch PosixPath -> PosixPath
toPath (Branch PosixPath
p) = PosixPath
p
class IsSeg a
instance IsSeg (Rooted a)
instance IsSeg (Branch a)
liftRooted :: Rooted OS_PATH -> Q Exp
liftRooted :: Rooted PosixPath -> Q Exp
liftRooted (Rooted PosixPath
p) =
[| OsPath.unsafeFromString $([Char] -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [Char] -> m Exp
lift ([Char] -> Q Exp) -> [Char] -> Q Exp
forall a b. (a -> b) -> a -> b
$ PosixPath -> [Char]
forall a. IsPath PosixPath a => a -> [Char]
OsPath.toString PosixPath
p) :: Rooted OS_PATH |]
liftBranch :: Branch OS_PATH -> Q Exp
liftBranch :: Branch PosixPath -> Q Exp
liftBranch (Branch PosixPath
p) =
[| OsPath.unsafeFromString $([Char] -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [Char] -> m Exp
lift ([Char] -> Q Exp) -> [Char] -> Q Exp
forall a b. (a -> b) -> a -> b
$ PosixPath -> [Char]
forall a. IsPath PosixPath a => a -> [Char]
OsPath.toString PosixPath
p) :: Branch OS_PATH |]
rtE :: String -> Q Exp
rtE :: [Char] -> Q Exp
rtE = (SomeException -> Q Exp)
-> (Rooted PosixPath -> Q Exp)
-> Either SomeException (Rooted PosixPath)
-> Q Exp
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> Q Exp
forall a. HasCallStack => [Char] -> a
error ([Char] -> Q Exp)
-> (SomeException -> [Char]) -> SomeException -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SomeException -> [Char]
forall a. Show a => a -> [Char]
show) Rooted PosixPath -> Q Exp
liftRooted (Either SomeException (Rooted PosixPath) -> Q Exp)
-> ([Char] -> Either SomeException (Rooted PosixPath))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Rooted PosixPath)
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
brE :: String -> Q Exp
brE :: [Char] -> Q Exp
brE = (SomeException -> Q Exp)
-> (Branch PosixPath -> Q Exp)
-> Either SomeException (Branch PosixPath)
-> Q Exp
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> Q Exp
forall a. HasCallStack => [Char] -> a
error ([Char] -> Q Exp)
-> (SomeException -> [Char]) -> SomeException -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SomeException -> [Char]
forall a. Show a => a -> [Char]
show) Branch PosixPath -> Q Exp
liftBranch (Either SomeException (Branch PosixPath) -> Q Exp)
-> ([Char] -> Either SomeException (Branch PosixPath))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Branch PosixPath)
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
rt :: QuasiQuoter
rt :: QuasiQuoter
rt = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
rtE
br :: QuasiQuoter
br :: QuasiQuoter
br = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
brE
{-# INLINE append #-}
append ::
(
IsSeg (a OS_PATH)
, IsPath OS_PATH (a OS_PATH)
) => a OS_PATH -> Branch OS_PATH -> a OS_PATH
append :: forall (a :: * -> *).
(IsSeg (a PosixPath), IsPath PosixPath (a PosixPath)) =>
a PosixPath -> Branch PosixPath -> a PosixPath
append a PosixPath
a (Branch PosixPath
c) = PosixPath -> a PosixPath
forall a b. IsPath a b => a -> b
unsafeFromPath (PosixPath -> a PosixPath) -> PosixPath -> a PosixPath
forall a b. (a -> b) -> a -> b
$ PosixPath -> PosixPath -> PosixPath
OsPath.unsafeAppend (a PosixPath -> PosixPath
forall a b. IsPath a b => b -> a
toPath a PosixPath
a) (PosixPath -> PosixPath
forall a b. IsPath a b => b -> a
toPath PosixPath
c)