{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
{-# OPTIONS_GHC -Wno-orphans #-}
#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.SegNode
(
rtdir
, brdir
, rtfile
, brfile
, rtdirE
, brdirE
, rtfileE
, brfileE
, append
)
where
import Language.Haskell.TH.Syntax (lift)
import Streamly.Internal.FileSystem.Path.Common (mkQ)
import Streamly.Internal.FileSystem.OS_PATH (OS_PATH(..))
import Streamly.Internal.FileSystem.OS_PATH.Seg (Rooted(..), Branch(..))
import Streamly.Internal.FileSystem.OS_PATH.Node (File(..), Dir(..))
import qualified Streamly.Internal.FileSystem.OS_PATH as OsPath
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Streamly.Internal.Data.Path
instance IsPath OS_PATH (Rooted (File OS_PATH)) where
unsafeFromPath :: PosixPath -> Rooted (File PosixPath)
unsafeFromPath PosixPath
p = File PosixPath -> Rooted (File PosixPath)
forall a. a -> Rooted a
Rooted (PosixPath -> File PosixPath
forall a. a -> File a
File PosixPath
p)
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Rooted (File PosixPath))
fromPath PosixPath
p = do
File PosixPath
_ :: File OS_PATH <- fromPath p
Rooted PosixPath
_ :: Rooted OS_PATH <- fromPath p
Rooted (File PosixPath) -> m (Rooted (File PosixPath))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Rooted (File PosixPath) -> m (Rooted (File PosixPath)))
-> Rooted (File PosixPath) -> m (Rooted (File PosixPath))
forall a b. (a -> b) -> a -> b
$ File PosixPath -> Rooted (File PosixPath)
forall a. a -> Rooted a
Rooted (PosixPath -> File PosixPath
forall a. a -> File a
File PosixPath
p)
toPath :: Rooted (File PosixPath) -> PosixPath
toPath (Rooted (File PosixPath
p)) = PosixPath
p
instance IsPath OS_PATH (Rooted (Dir OS_PATH)) where
unsafeFromPath :: PosixPath -> Rooted (Dir PosixPath)
unsafeFromPath PosixPath
p = Dir PosixPath -> Rooted (Dir PosixPath)
forall a. a -> Rooted a
Rooted (PosixPath -> Dir PosixPath
forall a. a -> Dir a
Dir PosixPath
p)
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Rooted (Dir PosixPath))
fromPath PosixPath
p = do
Dir PosixPath
_ :: Dir OS_PATH <- fromPath p
Rooted PosixPath
_ :: Rooted OS_PATH <- fromPath p
Rooted (Dir PosixPath) -> m (Rooted (Dir PosixPath))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Rooted (Dir PosixPath) -> m (Rooted (Dir PosixPath)))
-> Rooted (Dir PosixPath) -> m (Rooted (Dir PosixPath))
forall a b. (a -> b) -> a -> b
$ Dir PosixPath -> Rooted (Dir PosixPath)
forall a. a -> Rooted a
Rooted (PosixPath -> Dir PosixPath
forall a. a -> Dir a
Dir PosixPath
p)
toPath :: Rooted (Dir PosixPath) -> PosixPath
toPath (Rooted (Dir PosixPath
p)) = PosixPath
p
instance IsPath OS_PATH (Branch (File OS_PATH)) where
unsafeFromPath :: PosixPath -> Branch (File PosixPath)
unsafeFromPath PosixPath
p = File PosixPath -> Branch (File PosixPath)
forall a. a -> Branch a
Branch (PosixPath -> File PosixPath
forall a. a -> File a
File PosixPath
p)
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Branch (File PosixPath))
fromPath PosixPath
p = do
File PosixPath
_ :: File OS_PATH <- fromPath p
Branch PosixPath
_ :: Branch OS_PATH <- fromPath p
Branch (File PosixPath) -> m (Branch (File PosixPath))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Branch (File PosixPath) -> m (Branch (File PosixPath)))
-> Branch (File PosixPath) -> m (Branch (File PosixPath))
forall a b. (a -> b) -> a -> b
$ File PosixPath -> Branch (File PosixPath)
forall a. a -> Branch a
Branch (PosixPath -> File PosixPath
forall a. a -> File a
File PosixPath
p)
toPath :: Branch (File PosixPath) -> PosixPath
toPath (Branch (File PosixPath
p)) = PosixPath
p
instance IsPath OS_PATH (Branch (Dir OS_PATH)) where
unsafeFromPath :: PosixPath -> Branch (Dir PosixPath)
unsafeFromPath PosixPath
p = Dir PosixPath -> Branch (Dir PosixPath)
forall a. a -> Branch a
Branch (PosixPath -> Dir PosixPath
forall a. a -> Dir a
Dir PosixPath
p)
fromPath :: forall (m :: * -> *).
MonadThrow m =>
PosixPath -> m (Branch (Dir PosixPath))
fromPath PosixPath
p = do
Dir PosixPath
_ :: Dir OS_PATH <- fromPath p
Branch PosixPath
_ :: Branch OS_PATH <- fromPath p
Branch (Dir PosixPath) -> m (Branch (Dir PosixPath))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Branch (Dir PosixPath) -> m (Branch (Dir PosixPath)))
-> Branch (Dir PosixPath) -> m (Branch (Dir PosixPath))
forall a b. (a -> b) -> a -> b
$ Dir PosixPath -> Branch (Dir PosixPath)
forall a. a -> Branch a
Branch (PosixPath -> Dir PosixPath
forall a. a -> Dir a
Dir PosixPath
p)
toPath :: Branch (Dir PosixPath) -> PosixPath
toPath (Branch (Dir PosixPath
p)) = PosixPath
p
liftRootedDir :: Rooted (Dir OS_PATH) -> Q Exp
liftRootedDir :: Rooted (Dir PosixPath) -> Q Exp
liftRootedDir (Rooted (Dir 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 (Dir OS_PATH)|]
liftBranchDir :: Branch (Dir OS_PATH) -> Q Exp
liftBranchDir :: Branch (Dir PosixPath) -> Q Exp
liftBranchDir (Branch (Dir 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 (Dir OS_PATH) |]
liftRootedFile :: Rooted (File OS_PATH) -> Q Exp
liftRootedFile :: Rooted (File PosixPath) -> Q Exp
liftRootedFile (Rooted (File 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 (File OS_PATH)|]
liftBranchFile :: Branch (File OS_PATH) -> Q Exp
liftBranchFile :: Branch (File PosixPath) -> Q Exp
liftBranchFile (Branch (File 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 (File OS_PATH)|]
rtdirE :: String -> Q Exp
rtdirE :: [Char] -> Q Exp
rtdirE = (SomeException -> Q Exp)
-> (Rooted (Dir PosixPath) -> Q Exp)
-> Either SomeException (Rooted (Dir 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 (Dir PosixPath) -> Q Exp
liftRootedDir (Either SomeException (Rooted (Dir PosixPath)) -> Q Exp)
-> ([Char] -> Either SomeException (Rooted (Dir PosixPath)))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Rooted (Dir PosixPath))
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
brdirE :: String -> Q Exp
brdirE :: [Char] -> Q Exp
brdirE = (SomeException -> Q Exp)
-> (Branch (Dir PosixPath) -> Q Exp)
-> Either SomeException (Branch (Dir 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 (Dir PosixPath) -> Q Exp
liftBranchDir (Either SomeException (Branch (Dir PosixPath)) -> Q Exp)
-> ([Char] -> Either SomeException (Branch (Dir PosixPath)))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Branch (Dir PosixPath))
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
rtfileE :: String -> Q Exp
rtfileE :: [Char] -> Q Exp
rtfileE = (SomeException -> Q Exp)
-> (Rooted (File PosixPath) -> Q Exp)
-> Either SomeException (Rooted (File 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 (File PosixPath) -> Q Exp
liftRootedFile (Either SomeException (Rooted (File PosixPath)) -> Q Exp)
-> ([Char] -> Either SomeException (Rooted (File PosixPath)))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Rooted (File PosixPath))
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
brfileE :: String -> Q Exp
brfileE :: [Char] -> Q Exp
brfileE = (SomeException -> Q Exp)
-> (Branch (File PosixPath) -> Q Exp)
-> Either SomeException (Branch (File 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 (File PosixPath) -> Q Exp
liftBranchFile (Either SomeException (Branch (File PosixPath)) -> Q Exp)
-> ([Char] -> Either SomeException (Branch (File PosixPath)))
-> [Char]
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Either SomeException (Branch (File PosixPath))
forall (m :: * -> *) a.
(MonadThrow m, IsPath PosixPath a) =>
[Char] -> m a
OsPath.fromString
rtdir :: QuasiQuoter
rtdir :: QuasiQuoter
rtdir = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
rtdirE
brdir :: QuasiQuoter
brdir :: QuasiQuoter
brdir = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
brdirE
rtfile :: QuasiQuoter
rtfile :: QuasiQuoter
rtfile = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
rtfileE
brfile :: QuasiQuoter
brfile :: QuasiQuoter
brfile = ([Char] -> Q Exp) -> QuasiQuoter
mkQ [Char] -> Q Exp
brfileE
{-# INLINE append #-}
append ::
(
IsPath OS_PATH (a (Dir OS_PATH))
, IsPath OS_PATH (b OS_PATH)
, IsPath OS_PATH (a (b OS_PATH))
) => a (Dir OS_PATH) -> Branch (b OS_PATH) -> a (b OS_PATH)
append :: forall (a :: * -> *) (b :: * -> *).
(IsPath PosixPath (a (Dir PosixPath)),
IsPath PosixPath (b PosixPath),
IsPath PosixPath (a (b PosixPath))) =>
a (Dir PosixPath) -> Branch (b PosixPath) -> a (b PosixPath)
append a (Dir PosixPath)
p1 (Branch b PosixPath
p2) =
PosixPath -> a (b PosixPath)
forall a b. IsPath a b => a -> b
unsafeFromPath (PosixPath -> a (b PosixPath)) -> PosixPath -> a (b PosixPath)
forall a b. (a -> b) -> a -> b
$ PosixPath -> PosixPath -> PosixPath
OsPath.unsafeAppend (a (Dir PosixPath) -> PosixPath
forall a b. IsPath a b => b -> a
toPath a (Dir PosixPath)
p1) (b PosixPath -> PosixPath
forall a b. IsPath a b => b -> a
toPath b PosixPath
p2)