{-# LANGUAGE CPP #-}
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module FileSystem.OsPath
(
OsPath,
encode,
encodeLenient,
encodeThrowM,
encodeFail,
unsafeEncode,
osp,
ospPathSep,
encodeValid,
encodeValidLenient,
encodeValidThrowM,
encodeValidFail,
unsafeEncodeValid,
decode,
decodeLenient,
decodeDisplayEx,
decodeShow,
decodeThrowM,
decodeFail,
unsafeDecode,
toOsString,
fromOsString,
fromOsStringThrowM,
fromOsStringFail,
unsafeFromOsString,
reallyUnsafeFromOsString,
(</>),
(<.>),
(-<.>),
(</>!),
(!</>),
combineFilePaths,
EncodingException (..),
toTildeState,
TildeState (..),
OsPathNE (..),
TildeException (..),
)
where
import Control.Category ((>>>))
import Control.DeepSeq (NFData)
import Control.Exception (Exception (displayException))
import Control.Monad.Catch (MonadThrow, throwM)
import FileSystem.Internal (TildePrefixes)
import FileSystem.Internal qualified as Internal
import GHC.Generics (Generic)
import GHC.Stack (HasCallStack)
import Language.Haskell.TH.Quote
( QuasiQuoter
( QuasiQuoter,
quoteDec,
quoteExp,
quotePat,
quoteType
),
)
import System.FilePath qualified as FP
import System.OsPath (OsPath, osp, (-<.>), (<.>), (</>))
import System.OsPath qualified as OsP
import System.OsPath.Encoding (EncodingException (EncodingError))
import System.OsString (OsString)
ospPathSep :: QuasiQuoter
ospPathSep :: QuasiQuoter
ospPathSep =
QuasiQuoter
{ quoteExp :: String -> Q Exp
quoteExp = QuasiQuoter
osp.quoteExp (String -> Q Exp) -> (String -> String) -> String -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
Internal.replaceSlashes,
quotePat :: String -> Q Pat
quotePat = QuasiQuoter
osp.quotePat (String -> Q Pat) -> (String -> String) -> String -> Q Pat
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
Internal.replaceSlashes,
quoteType :: String -> Q Type
quoteType = QuasiQuoter
osp.quoteType (String -> Q Type) -> (String -> String) -> String -> Q Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
Internal.replaceSlashes,
quoteDec :: String -> Q [Dec]
quoteDec = QuasiQuoter
osp.quoteDec (String -> Q [Dec]) -> (String -> String) -> String -> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
Internal.replaceSlashes
}
encode :: FilePath -> Either EncodingException OsPath
encode :: String -> Either EncodingException OsString
encode = TextEncoding
-> TextEncoding -> String -> Either EncodingException OsString
OsP.encodeWith TextEncoding
utf8Encoder TextEncoding
utf16Encoder
where
(TextEncoding
utf8Encoder, TextEncoding
utf16Encoder) = (TextEncoding, TextEncoding)
Internal.utfEncodings
encodeLenient :: FilePath -> OsPath
encodeLenient :: String -> OsString
encodeLenient = Either EncodingException OsString -> OsString
forall {a}. Either EncodingException a -> a
elimEx (Either EncodingException OsString -> OsString)
-> (String -> Either EncodingException OsString)
-> String
-> OsString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding
-> TextEncoding -> String -> Either EncodingException OsString
OsP.encodeWith TextEncoding
uft8Encoding TextEncoding
utf16Encoding
where
(TextEncoding
uft8Encoding, TextEncoding
utf16Encoding, Either EncodingException a -> a
elimEx) = (TextEncoding, TextEncoding, Either EncodingException a -> a)
forall a.
(TextEncoding, TextEncoding, Either EncodingException a -> a)
Internal.utfEncodingsLenient
encodeValid :: FilePath -> Either EncodingException OsPath
encodeValid :: String -> Either EncodingException OsString
encodeValid String
fp = case String -> Either EncodingException OsString
encode String
fp of
Left EncodingException
ex -> EncodingException -> Either EncodingException OsString
forall a b. a -> Either a b
Left EncodingException
ex
Right OsString
op ->
if OsString -> Bool
OsP.isValid OsString
op
then OsString -> Either EncodingException OsString
forall a b. b -> Either a b
Right OsString
op
else EncodingException -> Either EncodingException OsString
forall a b. a -> Either a b
Left (EncodingException -> Either EncodingException OsString)
-> EncodingException -> Either EncodingException OsString
forall a b. (a -> b) -> a -> b
$ String -> Maybe Word8 -> EncodingException
EncodingError (String -> OsString -> String
validFpErr String
fp OsString
op) Maybe Word8
forall a. Maybe a
Nothing
encodeValidLenient :: FilePath -> OsPath
encodeValidLenient :: String -> OsString
encodeValidLenient = OsString -> OsString
OsP.makeValid (OsString -> OsString)
-> (String -> OsString) -> String -> OsString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> OsString
encodeLenient
encodeThrowM :: (HasCallStack, MonadThrow m) => FilePath -> m OsPath
encodeThrowM :: forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
String -> m OsString
encodeThrowM =
String -> Either EncodingException OsString
encode (String -> Either EncodingException OsString)
-> (Either EncodingException OsString -> m OsString)
-> String
-> m OsString
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> \case
Right OsString
txt -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
txt
Left EncodingException
ex -> EncodingException -> m OsString
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM EncodingException
ex
{-# INLINEABLE encodeThrowM #-}
encodeValidThrowM :: (HasCallStack, MonadThrow m) => FilePath -> m OsPath
encodeValidThrowM :: forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
String -> m OsString
encodeValidThrowM =
String -> Either EncodingException OsString
encodeValid (String -> Either EncodingException OsString)
-> (Either EncodingException OsString -> m OsString)
-> String
-> m OsString
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> \case
Left EncodingException
ex -> EncodingException -> m OsString
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM EncodingException
ex
Right OsString
op -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
op
{-# INLINEABLE encodeValidThrowM #-}
encodeFail :: (HasCallStack, MonadFail m) => FilePath -> m OsPath
encodeFail :: forall (m :: * -> *).
(HasCallStack, MonadFail m) =>
String -> m OsString
encodeFail String
fp = case String -> Either EncodingException OsString
encode String
fp of
Right OsString
txt -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
txt
Left EncodingException
ex -> String -> m OsString
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
{-# INLINEABLE encodeFail #-}
encodeValidFail :: (HasCallStack, MonadFail m) => FilePath -> m OsPath
encodeValidFail :: forall (m :: * -> *).
(HasCallStack, MonadFail m) =>
String -> m OsString
encodeValidFail String
fp = case String -> Either EncodingException OsString
encodeValid String
fp of
Left EncodingException
ex -> String -> m OsString
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
Right OsString
op -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
op
{-# INLINEABLE encodeValidFail #-}
unsafeEncode :: (HasCallStack) => FilePath -> OsPath
unsafeEncode :: HasCallStack => String -> OsString
unsafeEncode String
fp = case String -> Either EncodingException OsString
encode String
fp of
Left EncodingException
ex -> String -> OsString
forall a. HasCallStack => String -> a
error (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
Right OsString
p -> OsString
p
unsafeEncodeValid :: (HasCallStack) => FilePath -> OsPath
unsafeEncodeValid :: HasCallStack => String -> OsString
unsafeEncodeValid String
fp = case String -> Either EncodingException OsString
encodeValid String
fp of
Left EncodingException
ex -> String -> OsString
forall a. HasCallStack => String -> a
error (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
Right OsString
op -> OsString
op
decode :: OsPath -> Either EncodingException FilePath
decode :: OsString -> Either EncodingException String
decode = TextEncoding
-> TextEncoding -> OsString -> Either EncodingException String
OsP.decodeWith TextEncoding
utf8Encoder TextEncoding
utf16Encoder
where
(TextEncoding
utf8Encoder, TextEncoding
utf16Encoder) = (TextEncoding, TextEncoding)
Internal.utfEncodings
decodeLenient :: OsPath -> FilePath
decodeLenient :: OsString -> String
decodeLenient = Either EncodingException String -> String
forall {a}. Either EncodingException a -> a
elimEx (Either EncodingException String -> String)
-> (OsString -> Either EncodingException String)
-> OsString
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding
-> TextEncoding -> OsString -> Either EncodingException String
OsP.decodeWith TextEncoding
uft8Encoding TextEncoding
utf16Encoding
where
(TextEncoding
uft8Encoding, TextEncoding
utf16Encoding, Either EncodingException a -> a
elimEx) = (TextEncoding, TextEncoding, Either EncodingException a -> a)
forall a.
(TextEncoding, TextEncoding, Either EncodingException a -> a)
Internal.utfEncodingsLenient
decodeThrowM :: (HasCallStack, MonadThrow m) => OsPath -> m FilePath
decodeThrowM :: forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
OsString -> m String
decodeThrowM =
OsString -> Either EncodingException String
decode (OsString -> Either EncodingException String)
-> (Either EncodingException String -> m String)
-> OsString
-> m String
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> \case
Right String
txt -> String -> m String
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
txt
Left EncodingException
ex -> EncodingException -> m String
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM EncodingException
ex
{-# INLINEABLE decodeThrowM #-}
decodeFail :: (HasCallStack, MonadFail m) => OsPath -> m FilePath
decodeFail :: forall (m :: * -> *).
(HasCallStack, MonadFail m) =>
OsString -> m String
decodeFail OsString
p = case OsString -> Either EncodingException String
decode OsString
p of
Right String
txt -> String -> m String
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
txt
Left EncodingException
ex -> String -> m String
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
{-# INLINEABLE decodeFail #-}
decodeDisplayEx :: OsPath -> String
decodeDisplayEx :: OsString -> String
decodeDisplayEx OsString
p = case OsString -> Either EncodingException String
decode OsString
p of
Left EncodingException
ex -> EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex
Right String
s -> String
s
decodeShow :: OsPath -> String
decodeShow :: OsString -> String
decodeShow OsString
p = case OsString -> Either EncodingException String
decode OsString
p of
Left EncodingException
_ -> OsString -> String
forall a. Show a => a -> String
show OsString
p
Right String
s -> String
s
unsafeDecode :: (HasCallStack) => OsPath -> FilePath
unsafeDecode :: HasCallStack => OsString -> String
unsafeDecode OsString
p = case OsString -> Either EncodingException String
decode OsString
p of
Left EncodingException
ex -> String -> String
forall a. HasCallStack => String -> a
error (EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex)
Right String
fp -> String
fp
validFpErr :: String -> OsPath -> String
validFpErr :: String -> OsString -> String
validFpErr String
fp OsString
x =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"FilePath '",
String
fp,
String
"' encoded as OsPath '",
OsString -> String
decodeLenient OsString
x,
String
"' failed isValid"
]
validOsStrErr :: OsString -> String
validOsStrErr :: OsString -> String
validOsStrErr OsString
str =
[String] -> String
forall a. Monoid a => [a] -> a
mconcat
[ String
"OsString '",
OsString -> String
decodeLenient OsString
str,
String
"' failed isValid"
]
(</>!) :: (HasCallStack) => OsPath -> FilePath -> OsPath
OsString
p </>! :: HasCallStack => OsString -> String -> OsString
</>! String
fp = OsString
p OsString -> OsString -> OsString
</> HasCallStack => String -> OsString
String -> OsString
unsafeEncode String
fp
infixl 9 </>!
(!</>) :: (HasCallStack) => FilePath -> OsPath -> OsPath
!</> :: HasCallStack => String -> OsString -> OsString
(!</>) = (OsString -> String -> OsString) -> String -> OsString -> OsString
forall a b c. (a -> b -> c) -> b -> a -> c
flip HasCallStack => OsString -> String -> OsString
OsString -> String -> OsString
(</>!)
infixl 9 !</>
combineFilePaths :: FilePath -> FilePath -> FilePath
combineFilePaths :: String -> String -> String
combineFilePaths = String -> String -> String
(FP.</>)
newtype TildeException = MkTildeException OsPath
deriving stock
(
TildeException -> TildeException -> Bool
(TildeException -> TildeException -> Bool)
-> (TildeException -> TildeException -> Bool) -> Eq TildeException
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TildeException -> TildeException -> Bool
== :: TildeException -> TildeException -> Bool
$c/= :: TildeException -> TildeException -> Bool
/= :: TildeException -> TildeException -> Bool
Eq,
(forall x. TildeException -> Rep TildeException x)
-> (forall x. Rep TildeException x -> TildeException)
-> Generic TildeException
forall x. Rep TildeException x -> TildeException
forall x. TildeException -> Rep TildeException x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TildeException -> Rep TildeException x
from :: forall x. TildeException -> Rep TildeException x
$cto :: forall x. Rep TildeException x -> TildeException
to :: forall x. Rep TildeException x -> TildeException
Generic,
Int -> TildeException -> String -> String
[TildeException] -> String -> String
TildeException -> String
(Int -> TildeException -> String -> String)
-> (TildeException -> String)
-> ([TildeException] -> String -> String)
-> Show TildeException
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> TildeException -> String -> String
showsPrec :: Int -> TildeException -> String -> String
$cshow :: TildeException -> String
show :: TildeException -> String
$cshowList :: [TildeException] -> String -> String
showList :: [TildeException] -> String -> String
Show
)
deriving anyclass
(
TildeException -> ()
(TildeException -> ()) -> NFData TildeException
forall a. (a -> ()) -> NFData a
$crnf :: TildeException -> ()
rnf :: TildeException -> ()
NFData
)
instance Exception TildeException where
displayException :: TildeException -> String
displayException (MkTildeException OsString
p) =
String
"Unexpected tilde in OsPath: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> OsString -> String
decodeLenient OsString
p
data TildeState
=
TildeStateNone OsPath
|
TildeStatePrefix OsPathNE
|
TildeStateNonPrefix OsPath
deriving stock
(
TildeState -> TildeState -> Bool
(TildeState -> TildeState -> Bool)
-> (TildeState -> TildeState -> Bool) -> Eq TildeState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TildeState -> TildeState -> Bool
== :: TildeState -> TildeState -> Bool
$c/= :: TildeState -> TildeState -> Bool
/= :: TildeState -> TildeState -> Bool
Eq,
(forall x. TildeState -> Rep TildeState x)
-> (forall x. Rep TildeState x -> TildeState) -> Generic TildeState
forall x. Rep TildeState x -> TildeState
forall x. TildeState -> Rep TildeState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TildeState -> Rep TildeState x
from :: forall x. TildeState -> Rep TildeState x
$cto :: forall x. Rep TildeState x -> TildeState
to :: forall x. Rep TildeState x -> TildeState
Generic,
Int -> TildeState -> String -> String
[TildeState] -> String -> String
TildeState -> String
(Int -> TildeState -> String -> String)
-> (TildeState -> String)
-> ([TildeState] -> String -> String)
-> Show TildeState
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> TildeState -> String -> String
showsPrec :: Int -> TildeState -> String -> String
$cshow :: TildeState -> String
show :: TildeState -> String
$cshowList :: [TildeState] -> String -> String
showList :: [TildeState] -> String -> String
Show
)
deriving anyclass
(
TildeState -> ()
(TildeState -> ()) -> NFData TildeState
forall a. (a -> ()) -> NFData a
$crnf :: TildeState -> ()
rnf :: TildeState -> ()
NFData
)
toTildeState :: OsPath -> TildeState
toTildeState :: OsString -> TildeState
toTildeState OsString
p =
case OsString -> Maybe OsPathNE
stripTildePrefix OsString
p of
Maybe OsPathNE
Nothing -> (OsString -> TildeState) -> OsString -> TildeState
f OsString -> TildeState
TildeStateNone OsString
p
Just OsPathNE
OsPathEmpty -> OsPathNE -> TildeState
TildeStatePrefix OsPathNE
OsPathEmpty
Just (OsPathNonEmpty OsString
p') ->
(OsString -> TildeState) -> OsString -> TildeState
f (OsPathNE -> TildeState
TildeStatePrefix (OsPathNE -> TildeState)
-> (OsString -> OsPathNE) -> OsString -> TildeState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OsString -> OsPathNE
OsPathNonEmpty) OsString
p'
where
f :: (OsPath -> TildeState) -> OsPath -> TildeState
f :: (OsString -> TildeState) -> OsString -> TildeState
f OsString -> TildeState
toState OsString
q =
if OsString -> Bool
Internal.containsTilde (OsString -> OsString
toOsString OsString
q)
then OsString -> TildeState
TildeStateNonPrefix OsString
q
else OsString -> TildeState
toState OsString
q
data OsPathNE
=
OsPathEmpty
|
OsPathNonEmpty OsPath
deriving stock
(
OsPathNE -> OsPathNE -> Bool
(OsPathNE -> OsPathNE -> Bool)
-> (OsPathNE -> OsPathNE -> Bool) -> Eq OsPathNE
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OsPathNE -> OsPathNE -> Bool
== :: OsPathNE -> OsPathNE -> Bool
$c/= :: OsPathNE -> OsPathNE -> Bool
/= :: OsPathNE -> OsPathNE -> Bool
Eq,
(forall x. OsPathNE -> Rep OsPathNE x)
-> (forall x. Rep OsPathNE x -> OsPathNE) -> Generic OsPathNE
forall x. Rep OsPathNE x -> OsPathNE
forall x. OsPathNE -> Rep OsPathNE x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OsPathNE -> Rep OsPathNE x
from :: forall x. OsPathNE -> Rep OsPathNE x
$cto :: forall x. Rep OsPathNE x -> OsPathNE
to :: forall x. Rep OsPathNE x -> OsPathNE
Generic,
Int -> OsPathNE -> String -> String
[OsPathNE] -> String -> String
OsPathNE -> String
(Int -> OsPathNE -> String -> String)
-> (OsPathNE -> String)
-> ([OsPathNE] -> String -> String)
-> Show OsPathNE
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> OsPathNE -> String -> String
showsPrec :: Int -> OsPathNE -> String -> String
$cshow :: OsPathNE -> String
show :: OsPathNE -> String
$cshowList :: [OsPathNE] -> String -> String
showList :: [OsPathNE] -> String -> String
Show
)
deriving anyclass
(
OsPathNE -> ()
(OsPathNE -> ()) -> NFData OsPathNE
forall a. (a -> ()) -> NFData a
$crnf :: OsPathNE -> ()
rnf :: OsPathNE -> ()
NFData
)
stripTildePrefix :: OsPath -> Maybe OsPathNE
stripTildePrefix :: OsString -> Maybe OsPathNE
stripTildePrefix =
(OsString -> OsPathNE) -> Maybe OsString -> Maybe OsPathNE
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap OsString -> OsPathNE
toStripped
(Maybe OsString -> Maybe OsPathNE)
-> (OsString -> Maybe OsString) -> OsString -> Maybe OsPathNE
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TildePrefixes -> OsString -> Maybe OsString
Internal.stripTildePrefix TildePrefixes
tildePrefixes
(OsString -> Maybe OsString)
-> (OsString -> OsString) -> OsString -> Maybe OsString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OsString -> OsString
toOsString
where
toStripped :: OsString -> OsPathNE
toStripped :: OsString -> OsPathNE
toStripped OsString
s =
if OsString
s OsString -> OsString -> Bool
forall a. Eq a => a -> a -> Bool
== OsString
forall a. Monoid a => a
mempty
then OsPathNE
OsPathEmpty
else OsString -> OsPathNE
OsPathNonEmpty (OsString -> OsString
reallyUnsafeFromOsString OsString
s)
tildePrefixes :: TildePrefixes
tildePrefixes :: TildePrefixes
tildePrefixes = (OsString -> OsString
toOsString [osp|~/|], OsString -> OsString
toOsString [osp|~\|])
toOsString :: OsPath -> OsString
toOsString :: OsString -> OsString
toOsString = OsString -> OsString
forall a. a -> a
id
fromOsString :: OsString -> Either EncodingException OsPath
fromOsString :: OsString -> Either EncodingException OsString
fromOsString OsString
s =
if OsString -> Bool
OsP.isValid OsString
s
then OsString -> Either EncodingException OsString
forall a b. b -> Either a b
Right OsString
s
else EncodingException -> Either EncodingException OsString
forall a b. a -> Either a b
Left (String -> Maybe Word8 -> EncodingException
EncodingError (OsString -> String
validOsStrErr OsString
s) Maybe Word8
forall a. Maybe a
Nothing)
fromOsStringThrowM :: (HasCallStack, MonadThrow m) => OsString -> m OsPath
fromOsStringThrowM :: forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
OsString -> m OsString
fromOsStringThrowM =
OsString -> Either EncodingException OsString
fromOsString (OsString -> Either EncodingException OsString)
-> (Either EncodingException OsString -> m OsString)
-> OsString
-> m OsString
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> \case
Left EncodingException
ex -> EncodingException -> m OsString
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM EncodingException
ex
Right OsString
p -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
p
fromOsStringFail :: (HasCallStack, MonadFail m) => OsString -> m OsPath
fromOsStringFail :: forall (m :: * -> *).
(HasCallStack, MonadFail m) =>
OsString -> m OsString
fromOsStringFail OsString
s = case OsString -> Either EncodingException OsString
fromOsString OsString
s of
Left EncodingException
ex -> String -> m OsString
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m OsString) -> String -> m OsString
forall a b. (a -> b) -> a -> b
$ EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex
Right OsString
p -> OsString -> m OsString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure OsString
p
unsafeFromOsString :: (HasCallStack) => OsString -> OsPath
unsafeFromOsString :: HasCallStack => OsString -> OsString
unsafeFromOsString OsString
s = case OsString -> Either EncodingException OsString
fromOsString OsString
s of
Left EncodingException
ex -> String -> OsString
forall a. HasCallStack => String -> a
error (String -> OsString) -> String -> OsString
forall a b. (a -> b) -> a -> b
$ EncodingException -> String
forall e. Exception e => e -> String
displayException EncodingException
ex
Right OsString
p -> OsString
p
reallyUnsafeFromOsString :: OsString -> OsPath
reallyUnsafeFromOsString :: OsString -> OsString
reallyUnsafeFromOsString = OsString -> OsString
forall a. a -> a
id