Browse Source

Redo parseAny

travis
Julian Ospald 4 years ago
parent
commit
375d8ae0a3
No known key found for this signature in database GPG Key ID: 511B62C09D50CD28
1 changed files with 23 additions and 11 deletions
  1. +23
    -11
      hpath/src/HPath.hs

+ 23
- 11
hpath/src/HPath.hs View File

@@ -39,6 +39,7 @@ module HPath
,fromAbs
,fromRel
,toFilePath
,fromAny
-- * Path Operations
,(</>)
,basename
@@ -52,7 +53,6 @@ module HPath
-- * Quasiquoters
,abs
,rel
,any
)
where

@@ -190,30 +190,30 @@ parseRel filepath =
--
-- Throws: 'PathParseException'
--
-- >>> parseAny "/abc" :: Maybe (Either (Path Abs) (Path Rel))
-- Just (Left "/abc")
-- >>> parseAny "..." :: Maybe (Either (Path Abs) (Path Rel))
-- Just (Right "...")
-- >>> parseAny "abc/def" :: Maybe (Either (Path Abs) (Path Rel))
-- Just (Right "abc/def")
-- >>> parseAny "abc/def/." :: Maybe (Either (Path Abs) (Path Rel))
-- Just (Right "abc/def/")
-- >>> parseAny "/abc" :: Maybe (Either (Path Abs) (Path Rel))
-- Just (Left "/abc")
-- >>> parseAny "" :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing
-- >>> parseAny "abc/../foo" :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing
-- >>> parseAny "." :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing
-- >>> parseAny ".." :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing
parseAny :: MonadThrow m => ByteString -> m (Path a)
parseAny :: MonadThrow m => ByteString -> m (Either (Path Abs) (Path Rel))
parseAny filepath = case parseAbs filepath of
Just (MkPath p) -> pure $ (MkPath p)
Just p -> pure $ Left p
Nothing -> case parseRel filepath of
Just (MkPath p) -> pure $ (MkPath p)
Nothing -> throwM (InvalidRel filepath)
Just p -> pure $ Right p
Nothing -> throwM (InvalidRel filepath)


--------------------------------------------------------------------------------
@@ -231,6 +231,10 @@ fromAbs = toFilePath
fromRel :: Path Rel -> ByteString
fromRel = toFilePath

fromAny :: Either (Path Abs) (Path Rel) -> ByteString
fromAny = either toFilePath toFilePath


--------------------------------------------------------------------------------
-- Path Operations

@@ -395,9 +399,6 @@ mkAbs = either (error . show) lift . parseAbs
mkRel :: ByteString -> Q Exp
mkRel = either (error . show) lift . parseRel

mkAny :: ByteString -> Q Exp
mkAny = either (error . show) lift . parseAny

-- | Quasiquote an absolute Path. This accepts Unicode Chars and will encode as UTF-8.
--
-- >>> [abs|/etc/profile|] :: Path Abs
@@ -420,14 +421,3 @@ abs = qq mkAbs
rel :: QuasiQuoter
rel = qq mkRel

--
any :: QuasiQuoter
any = qq mkAny


Loading…
Cancel
Save