Compare commits
11 Commits
0.5.4
...
constructo
| Author | SHA1 | Date | |
|---|---|---|---|
| 29923d1023 | |||
| c3c96ed371 | |||
|
|
e2974d3152 | ||
|
|
dbee82c69b | ||
|
|
05271c94af | ||
|
|
9d357b24c8 | ||
|
|
62c681819c | ||
|
|
c41fa86d17 | ||
|
|
04608e0e53 | ||
|
|
47450b5dd6 | ||
|
|
540c24f3a5 |
@@ -1,3 +1,9 @@
|
||||
0.5.7:
|
||||
* Fix haddock problem.
|
||||
0.5.6:
|
||||
* Reject only .. and .
|
||||
0.5.5:
|
||||
* Use filepath's isValid function for additional sanity checks
|
||||
0.5.4:
|
||||
* Disable parsing of path consisting only of "."
|
||||
* Add NFData instance for Path
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: path
|
||||
version: 0.5.4
|
||||
version: 0.5.7
|
||||
synopsis: Support for well-typed paths
|
||||
description: Support for will-typed paths.
|
||||
license: BSD3
|
||||
|
||||
137
src/Path.hs
137
src/Path.hs
@@ -13,15 +13,11 @@
|
||||
{-# LANGUAGE DeriveDataTypeable #-}
|
||||
{-# LANGUAGE EmptyDataDecls #-}
|
||||
|
||||
-- | A normalizing well-typed path type.
|
||||
|
||||
module Path
|
||||
(-- * Types
|
||||
Path
|
||||
,Abs
|
||||
,Rel
|
||||
,File
|
||||
,Dir
|
||||
-- * Parsing
|
||||
,parseAbsDir
|
||||
,parseRelDir
|
||||
@@ -39,13 +35,8 @@ module Path
|
||||
,isParentOf
|
||||
,parent
|
||||
,filename
|
||||
,dirname
|
||||
-- * Conversion
|
||||
,toFilePath
|
||||
,fromAbsDir
|
||||
,fromRelDir
|
||||
,fromAbsFile
|
||||
,fromRelFile
|
||||
)
|
||||
where
|
||||
|
||||
@@ -67,11 +58,6 @@ data Abs deriving (Typeable)
|
||||
-- | A relative path; one without a root.
|
||||
data Rel deriving (Typeable)
|
||||
|
||||
-- | A file path.
|
||||
data File deriving (Typeable)
|
||||
|
||||
-- | A directory path.
|
||||
data Dir deriving (Typeable)
|
||||
|
||||
-- | Exception when parsing a location.
|
||||
data PathParseException
|
||||
@@ -80,6 +66,7 @@ data PathParseException
|
||||
| InvalidAbsFile FilePath
|
||||
| InvalidRelFile FilePath
|
||||
| Couldn'tStripPrefixDir FilePath FilePath
|
||||
| InvalidTypeCombination
|
||||
deriving (Show,Typeable)
|
||||
instance Exception PathParseException
|
||||
|
||||
@@ -92,13 +79,14 @@ instance Exception PathParseException
|
||||
-- Throws: 'PathParseException'
|
||||
--
|
||||
parseAbsDir :: MonadThrow m
|
||||
=> FilePath -> m (Path Abs Dir)
|
||||
=> FilePath -> m (Path Abs)
|
||||
parseAbsDir filepath =
|
||||
if FilePath.isAbsolute filepath &&
|
||||
not (null (normalizeDir filepath)) &&
|
||||
not ("~/" `isPrefixOf` filepath) &&
|
||||
not (hasParentDir filepath)
|
||||
then return (Path (normalizeDir filepath))
|
||||
not (hasParentDir filepath) &&
|
||||
FilePath.isValid filepath
|
||||
then return (DPath (normalizeDir filepath))
|
||||
else throwM (InvalidAbsDir filepath)
|
||||
|
||||
-- | Get a location for a relative directory. Produces a normalized
|
||||
@@ -109,15 +97,16 @@ parseAbsDir filepath =
|
||||
-- Throws: 'PathParseException'
|
||||
--
|
||||
parseRelDir :: MonadThrow m
|
||||
=> FilePath -> m (Path Rel Dir)
|
||||
=> FilePath -> m (Path Rel)
|
||||
parseRelDir filepath =
|
||||
if not (FilePath.isAbsolute filepath) &&
|
||||
not (null filepath) &&
|
||||
not ("~/" `isPrefixOf` filepath) &&
|
||||
not (hasParentDir filepath) &&
|
||||
not (null (normalizeDir filepath)) &&
|
||||
not (all (=='.') filepath)
|
||||
then return (Path (normalizeDir filepath))
|
||||
filepath /= "." && filepath /= ".." &&
|
||||
FilePath.isValid filepath
|
||||
then return (DPath (normalizeDir filepath))
|
||||
else throwM (InvalidRelDir filepath)
|
||||
|
||||
-- | Get a location for an absolute file.
|
||||
@@ -125,15 +114,15 @@ parseRelDir filepath =
|
||||
-- Throws: 'PathParseException'
|
||||
--
|
||||
parseAbsFile :: MonadThrow m
|
||||
=> FilePath -> m (Path Abs File)
|
||||
=> FilePath -> m (Path Abs)
|
||||
parseAbsFile filepath =
|
||||
if FilePath.isAbsolute filepath &&
|
||||
not (FilePath.hasTrailingPathSeparator filepath) &&
|
||||
not ("~/" `isPrefixOf` filepath) &&
|
||||
not (hasParentDir filepath) &&
|
||||
not (null (normalizeFile filepath)) &&
|
||||
not (all (=='.') filepath)
|
||||
then return (Path (normalizeFile filepath))
|
||||
FilePath.isValid filepath
|
||||
then return (FPath (normalizeFile filepath))
|
||||
else throwM (InvalidAbsFile filepath)
|
||||
|
||||
-- | Get a location for a relative file.
|
||||
@@ -143,7 +132,7 @@ parseAbsFile filepath =
|
||||
-- Throws: 'PathParseException'
|
||||
--
|
||||
parseRelFile :: MonadThrow m
|
||||
=> FilePath -> m (Path Rel File)
|
||||
=> FilePath -> m (Path Rel)
|
||||
parseRelFile filepath =
|
||||
if not (FilePath.isAbsolute filepath ||
|
||||
FilePath.hasTrailingPathSeparator filepath) &&
|
||||
@@ -151,8 +140,9 @@ parseRelFile filepath =
|
||||
not ("~/" `isPrefixOf` filepath) &&
|
||||
not (hasParentDir filepath) &&
|
||||
not (null (normalizeFile filepath)) &&
|
||||
not (all (=='.') filepath)
|
||||
then return (Path (normalizeFile filepath))
|
||||
filepath /= "." && filepath /= ".." &&
|
||||
FilePath.isValid filepath
|
||||
then return (FPath (normalizeFile filepath))
|
||||
else throwM (InvalidRelFile filepath)
|
||||
|
||||
-- | Helper function: check if the filepath has any parent directories in it.
|
||||
@@ -180,16 +170,18 @@ mkAbsDir :: FilePath -> Q Exp
|
||||
mkAbsDir s =
|
||||
case parseAbsDir s of
|
||||
Left err -> error (show err)
|
||||
Right (Path str) ->
|
||||
[|Path $(return (LitE (StringL str))) :: Path Abs Dir|]
|
||||
Right (DPath str) ->
|
||||
[|DPath $(return (LitE (StringL str))) :: Path Abs|]
|
||||
_ -> error "Invalid Type"
|
||||
|
||||
-- | Make a 'Path Rel Dir'.
|
||||
mkRelDir :: FilePath -> Q Exp
|
||||
mkRelDir s =
|
||||
case parseRelDir s of
|
||||
Left err -> error (show err)
|
||||
Right (Path str) ->
|
||||
[|Path $(return (LitE (StringL str))) :: Path Rel Dir|]
|
||||
Right (DPath str) ->
|
||||
[|DPath $(return (LitE (StringL str))) :: Path Rel|]
|
||||
_ -> error "Invalid Type"
|
||||
|
||||
-- | Make a 'Path Abs File'.
|
||||
--
|
||||
@@ -200,16 +192,18 @@ mkAbsFile :: FilePath -> Q Exp
|
||||
mkAbsFile s =
|
||||
case parseAbsFile s of
|
||||
Left err -> error (show err)
|
||||
Right (Path str) ->
|
||||
[|Path $(return (LitE (StringL str))) :: Path Abs File|]
|
||||
Right (FPath str) ->
|
||||
[|FPath $(return (LitE (StringL str))) :: Path Abs|]
|
||||
_ -> error "Invalid Type"
|
||||
|
||||
-- | Make a 'Path Rel File'.
|
||||
mkRelFile :: FilePath -> Q Exp
|
||||
mkRelFile s =
|
||||
case parseRelFile s of
|
||||
Left err -> error (show err)
|
||||
Right (Path str) ->
|
||||
[|Path $(return (LitE (StringL str))) :: Path Rel File|]
|
||||
Right (FPath str) ->
|
||||
[|FPath $(return (LitE (StringL str))) :: Path Rel|]
|
||||
_ -> error "Invalid Type"
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Conversion
|
||||
@@ -219,24 +213,10 @@ mkRelFile s =
|
||||
-- All directories have a trailing slash, so if you want no trailing
|
||||
-- slash, you can use 'System.FilePath.dropTrailingPathSeparator' from
|
||||
-- the filepath package.
|
||||
toFilePath :: Path b t -> FilePath
|
||||
toFilePath (Path l) = l
|
||||
toFilePath :: Path b -> FilePath
|
||||
toFilePath (FPath l) = l
|
||||
toFilePath (DPath l) = l
|
||||
|
||||
-- | Convert absolute path to directory to 'FilePath' type.
|
||||
fromAbsDir :: Path Abs Dir -> FilePath
|
||||
fromAbsDir = toFilePath
|
||||
|
||||
-- | Convert relative path to directory to 'FilePath' type.
|
||||
fromRelDir :: Path Rel Dir -> FilePath
|
||||
fromRelDir = toFilePath
|
||||
|
||||
-- | Convert absolute path to file to 'FilePath' type.
|
||||
fromAbsFile :: Path Abs File -> FilePath
|
||||
fromAbsFile = toFilePath
|
||||
|
||||
-- | Convert relative path to file to 'FilePath' type.
|
||||
fromRelFile :: Path Rel File -> FilePath
|
||||
fromRelFile = toFilePath
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Operations
|
||||
@@ -263,8 +243,10 @@ fromRelFile = toFilePath
|
||||
--
|
||||
-- @x \<\/> $(mkAbsDir …)@
|
||||
--
|
||||
(</>) :: Path b Dir -> Path Rel t -> Path b t
|
||||
(</>) (Path a) (Path b) = Path (a ++ b)
|
||||
(</>) :: MonadThrow m => Path b -> Path Rel -> m (Path b)
|
||||
(</>) (DPath a) (FPath b) = return $ FPath (a ++ b)
|
||||
(</>) (DPath a) (DPath b) = return $ DPath (a ++ b)
|
||||
(</>) _ _ = throwM InvalidTypeCombination
|
||||
|
||||
-- | Strip directory from path, making it relative to that directory.
|
||||
-- Throws 'Couldn'tStripPrefixDir' if directory is not a parent of the path.
|
||||
@@ -282,18 +264,27 @@ fromRelFile = toFilePath
|
||||
-- In other words the bases must match.
|
||||
--
|
||||
stripDir :: MonadThrow m
|
||||
=> Path b Dir -> Path b t -> m (Path Rel t)
|
||||
stripDir (Path p) (Path l) =
|
||||
=> Path b -> Path b -> m (Path Rel)
|
||||
stripDir (DPath p) (FPath l) =
|
||||
case stripPrefix p l of
|
||||
Nothing -> throwM (Couldn'tStripPrefixDir p l)
|
||||
Just "" -> throwM (Couldn'tStripPrefixDir p l)
|
||||
Just ok -> return (Path ok)
|
||||
Just ok -> return (FPath ok)
|
||||
stripDir (DPath p) (DPath l) =
|
||||
case stripPrefix p l of
|
||||
Nothing -> throwM (Couldn'tStripPrefixDir p l)
|
||||
Just "" -> throwM (Couldn'tStripPrefixDir p l)
|
||||
Just ok -> return (DPath ok)
|
||||
stripDir _ _ = throwM InvalidTypeCombination
|
||||
|
||||
|
||||
-- | Is p a parent of the given location? Implemented in terms of
|
||||
-- 'stripDir'. The bases must match.
|
||||
isParentOf :: Path b Dir -> Path b t -> Bool
|
||||
isParentOf p l =
|
||||
isJust (stripDir p l)
|
||||
-- Returns False if the first argument is not a directory path.
|
||||
isParentOf :: Path b -> Path b -> Bool
|
||||
isParentOf p@(DPath _) l@(DPath _) = isJust (stripDir p l)
|
||||
isParentOf p@(DPath _) l@(FPath _) = isJust (stripDir p l)
|
||||
isParentOf _ _ = False
|
||||
|
||||
-- | Take the absolute parent directory from the absolute path.
|
||||
--
|
||||
@@ -305,29 +296,24 @@ isParentOf p l =
|
||||
--
|
||||
-- @parent (parent \"\/\") = \"\/\"@
|
||||
--
|
||||
parent :: Path Abs t -> Path Abs Dir
|
||||
parent (Path fp) =
|
||||
Path (normalizeDir (FilePath.takeDirectory (FilePath.dropTrailingPathSeparator fp)))
|
||||
parent :: Path Abs -> Path Abs
|
||||
parent (DPath fp) =
|
||||
DPath (normalizeDir (FilePath.takeDirectory (FilePath.dropTrailingPathSeparator fp)))
|
||||
parent (FPath fp) =
|
||||
DPath (normalizeDir (FilePath.takeDirectory (FilePath.dropTrailingPathSeparator fp)))
|
||||
|
||||
-- | Extract the file part of a path.
|
||||
-- | Extract the file/directory part of a path.
|
||||
--
|
||||
-- The following properties hold:
|
||||
--
|
||||
-- @filename (p \<\/> a) == filename a@
|
||||
--
|
||||
filename :: Path b File -> Path Rel File
|
||||
filename (Path l) =
|
||||
Path (normalizeFile (FilePath.takeFileName l))
|
||||
filename :: Path b -> Path Rel
|
||||
filename (FPath l) =
|
||||
FPath (normalizeFile (FilePath.takeFileName l))
|
||||
filename (DPath l) =
|
||||
DPath (last (FilePath.splitPath l))
|
||||
|
||||
-- | Extract the last directory name of a path.
|
||||
--
|
||||
-- The following properties hold:
|
||||
--
|
||||
-- @dirname (p \<\/> a) == dirname a@
|
||||
--
|
||||
dirname :: Path b Dir -> Path Rel Dir
|
||||
dirname (Path l) =
|
||||
Path (last (FilePath.splitPath l))
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Internal functions
|
||||
@@ -347,3 +333,4 @@ normalizeFile =
|
||||
where clean "./" = ""
|
||||
clean ('/':'/':xs) = clean ('/':xs)
|
||||
clean x = x
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{-# LANGUAGE DeriveDataTypeable #-}
|
||||
|
||||
-- TODO: - remove undefined in Ord instance
|
||||
-- - use viewpatterns/patternsynonyms so we don't need to
|
||||
-- export the constructors
|
||||
|
||||
-- | Internal types and functions.
|
||||
|
||||
module Path.Internal
|
||||
@@ -18,7 +22,8 @@ import Data.Data
|
||||
--
|
||||
-- All directories end in a trailing separator. There are no duplicate
|
||||
-- path separators @\/\/@, no @..@, no @.\/@, no @~\/@, etc.
|
||||
newtype Path b t = Path FilePath
|
||||
data Path b = FPath FilePath
|
||||
| DPath FilePath
|
||||
deriving (Typeable)
|
||||
|
||||
-- | String equality.
|
||||
@@ -26,24 +31,31 @@ newtype Path b t = Path FilePath
|
||||
-- The following property holds:
|
||||
--
|
||||
-- @show x == show y ≡ x == y@
|
||||
instance Eq (Path b t) where
|
||||
(==) (Path x) (Path y) = x == y
|
||||
instance Eq (Path b) where
|
||||
(==) (FPath x) (FPath y) = x == y
|
||||
(==) (DPath x) (DPath y) = x == y
|
||||
(==) _ _ = False
|
||||
|
||||
-- | String ordering.
|
||||
--
|
||||
-- The following property holds:
|
||||
--
|
||||
-- @show x \`compare\` show y ≡ x \`compare\` y@
|
||||
instance Ord (Path b t) where
|
||||
compare (Path x) (Path y) = compare x y
|
||||
instance Ord (Path b) where
|
||||
compare (FPath x) (FPath y) = compare x y
|
||||
compare (DPath x) (DPath y) = compare x y
|
||||
compare _ _ = undefined
|
||||
|
||||
-- | Same as 'Path.toFilePath'.
|
||||
--
|
||||
-- The following property holds:
|
||||
--
|
||||
-- @x == y ≡ show x == show y@
|
||||
instance Show (Path b t) where
|
||||
show (Path x) = show x
|
||||
instance Show (Path b) where
|
||||
show (FPath x) = show x
|
||||
show (DPath x) = show x
|
||||
|
||||
instance NFData (Path b) where
|
||||
rnf (FPath x) = rnf x
|
||||
rnf (DPath x) = rnf x
|
||||
|
||||
instance NFData (Path b t) where
|
||||
rnf (Path x) = rnf x
|
||||
|
||||
@@ -156,6 +156,7 @@ parseRelDirSpec =
|
||||
failing "///foo//bar//mu/"
|
||||
failing "///foo//bar////mu"
|
||||
failing "///foo//bar/.//mu"
|
||||
succeeding "..." (Path ".../")
|
||||
succeeding "foo.bak" (Path "foo.bak/")
|
||||
succeeding "./foo" (Path "foo/")
|
||||
succeeding "././foo" (Path "foo/")
|
||||
@@ -176,6 +177,7 @@ parseAbsFileSpec =
|
||||
failing "/"
|
||||
failing "//"
|
||||
failing "///foo//bar//mu/"
|
||||
succeeding "/..." (Path "/...")
|
||||
succeeding "/foo.txt" (Path "/foo.txt")
|
||||
succeeding "///foo//bar////mu.txt" (Path "/foo/bar/mu.txt")
|
||||
succeeding "///foo//bar/.//mu.txt" (Path "/foo/bar/mu.txt")
|
||||
@@ -195,6 +197,7 @@ parseRelFileSpec =
|
||||
failing "///foo//bar//mu/"
|
||||
failing "///foo//bar////mu"
|
||||
failing "///foo//bar/.//mu"
|
||||
succeeding "..." (Path "...")
|
||||
succeeding "foo.txt" (Path "foo.txt")
|
||||
succeeding "./foo.txt" (Path "foo.txt")
|
||||
succeeding "././foo.txt" (Path "foo.txt")
|
||||
|
||||
Reference in New Issue
Block a user