Redo parseAny

This commit is contained in:
Julian Ospald 2020-01-20 19:00:24 +01:00
parent 3abc68cdd6
commit 375d8ae0a3
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
1 changed files with 23 additions and 33 deletions

View File

@ -39,6 +39,7 @@ module HPath
,fromAbs ,fromAbs
,fromRel ,fromRel
,toFilePath ,toFilePath
,fromAny
-- * Path Operations -- * Path Operations
,(</>) ,(</>)
,basename ,basename
@ -52,7 +53,6 @@ module HPath
-- * Quasiquoters -- * Quasiquoters
,abs ,abs
,rel ,rel
,any
) )
where where
@ -190,30 +190,30 @@ parseRel filepath =
-- --
-- Throws: 'PathParseException' -- Throws: 'PathParseException'
-- --
-- >>> parseAny "/abc" :: Maybe (Path a) -- >>> parseAny "/abc" :: Maybe (Either (Path Abs) (Path Rel))
-- Just "/abc" -- Just (Left "/abc")
-- >>> parseAny "..." :: Maybe (Path a) -- >>> parseAny "..." :: Maybe (Either (Path Abs) (Path Rel))
-- Just "..." -- Just (Right "...")
-- >>> parseAny "abc/def" :: Maybe (Path a) -- >>> parseAny "abc/def" :: Maybe (Either (Path Abs) (Path Rel))
-- Just "abc/def" -- Just (Right "abc/def")
-- >>> parseAny "abc/def/." :: Maybe (Path a) -- >>> parseAny "abc/def/." :: Maybe (Either (Path Abs) (Path Rel))
-- Just "abc/def/" -- Just (Right "abc/def/")
-- >>> parseAny "/abc" :: Maybe (Path a) -- >>> parseAny "/abc" :: Maybe (Either (Path Abs) (Path Rel))
-- Just "/abc" -- Just (Left "/abc")
-- >>> parseAny "" :: Maybe (Path a) -- >>> parseAny "" :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing -- Nothing
-- >>> parseAny "abc/../foo" :: Maybe (Path a) -- >>> parseAny "abc/../foo" :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing -- Nothing
-- >>> parseAny "." :: Maybe (Path a) -- >>> parseAny "." :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing -- Nothing
-- >>> parseAny ".." :: Maybe (Path a) -- >>> parseAny ".." :: Maybe (Either (Path Abs) (Path Rel))
-- Nothing -- Nothing
parseAny :: MonadThrow m => ByteString -> m (Path a) parseAny :: MonadThrow m => ByteString -> m (Either (Path Abs) (Path Rel))
parseAny filepath = case parseAbs filepath of parseAny filepath = case parseAbs filepath of
Just (MkPath p) -> pure $ (MkPath p) Just p -> pure $ Left p
Nothing -> case parseRel filepath of Nothing -> case parseRel filepath of
Just (MkPath p) -> pure $ (MkPath p) Just p -> pure $ Right p
Nothing -> throwM (InvalidRel filepath) Nothing -> throwM (InvalidRel filepath)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -231,6 +231,10 @@ fromAbs = toFilePath
fromRel :: Path Rel -> ByteString fromRel :: Path Rel -> ByteString
fromRel = toFilePath fromRel = toFilePath
fromAny :: Either (Path Abs) (Path Rel) -> ByteString
fromAny = either toFilePath toFilePath
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Path Operations -- Path Operations
@ -395,9 +399,6 @@ mkAbs = either (error . show) lift . parseAbs
mkRel :: ByteString -> Q Exp mkRel :: ByteString -> Q Exp
mkRel = either (error . show) lift . parseRel 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. -- | Quasiquote an absolute Path. This accepts Unicode Chars and will encode as UTF-8.
-- --
-- >>> [abs|/etc/profile|] :: Path Abs -- >>> [abs|/etc/profile|] :: Path Abs
@ -420,14 +421,3 @@ abs = qq mkAbs
rel :: QuasiQuoter rel :: QuasiQuoter
rel = qq mkRel rel = qq mkRel
-- | Quasiquote any path (relative or absolute).
-- This accepts Unicode Chars and will encode as UTF-8.
--
-- >>> [any|/etc/profile|] :: Path a
-- "/etc/profile"
-- >>> [any|etc|] :: Path a
-- "etc"
-- >>> [any||] :: Path a
-- "\239\131\144"
any :: QuasiQuoter
any = qq mkAny