From 375d8ae0a37a59d3dfe464d76596903b98c3b080 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 20 Jan 2020 19:00:24 +0100 Subject: [PATCH] Redo parseAny --- hpath/src/HPath.hs | 56 +++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/hpath/src/HPath.hs b/hpath/src/HPath.hs index 63127a5..cdbcb21 100644 --- a/hpath/src/HPath.hs +++ b/hpath/src/HPath.hs @@ -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 (Path a) --- Just "/abc" --- >>> parseAny "..." :: Maybe (Path a) --- Just "..." --- >>> parseAny "abc/def" :: Maybe (Path a) --- Just "abc/def" --- >>> parseAny "abc/def/." :: Maybe (Path a) --- Just "abc/def/" --- >>> parseAny "/abc" :: Maybe (Path a) --- Just "/abc" --- >>> parseAny "" :: Maybe (Path a) +-- >>> 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 (Path a) +-- >>> parseAny "abc/../foo" :: Maybe (Either (Path Abs) (Path Rel)) -- Nothing --- >>> parseAny "." :: Maybe (Path a) +-- >>> parseAny "." :: Maybe (Either (Path Abs) (Path Rel)) -- Nothing --- >>> parseAny ".." :: Maybe (Path a) +-- >>> 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 --- | 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