within 0.0.1.0 → 0.0.1.1
raw patch · 3 files changed
+22/−3 lines, 3 files
Files
- ChangeLog.md +5/−1
- src/Within.hs +15/−0
- within.cabal +2/−2
ChangeLog.md view
@@ -1,6 +1,10 @@ # Changelog for within -## v0.0.1.o+## v0.0.1.1++Add docs++## v0.0.1.0 * Add Within Type based on [path](https://hackage.haskell.org/package/path). Within is a path within another path.
src/Within.hs view
@@ -20,44 +20,59 @@ import GHC.Generics import Path +-- | The `Within` type represents a relative `Path` inside a directory `Path`.+-- The two halves can be manipulated independently. newtype Within a t = Within (Path a Dir, Path Rel t) deriving (Typeable, Generic, Eq, Show) +-- | Convert a `Within` to a `Path` by joining it with a path separator. fromWithin :: Within a t -> Path a t fromWithin (Within (x,y)) = x </> y +-- | Convert a directory `Path` and a relative `Path` to a `Within` toWithin :: Path a Dir -> Path Rel t -> Within a t toWithin = flip within +-- | Infix version of `toWithin`, e.g "file.txt \`within\` myDir" within :: Path Rel t -> Path a Dir -> Within a t within y x = Within (x,y) +-- | Attempts to convert a `Path` to a `Within` by treating it as if it were+-- within the second argument. Used infix as "myParentDir\/foo\/file.txt \`asWithin\` myParentDir" asWithin :: MonadThrow m => Path a t -> Path a Dir -> m (Within a t) asWithin x y = stripProperPrefix y x >>= \z -> return (Within (y, z)) +-- | Extracts the inner path. whatLiesWithin :: Within a t -> Path Rel t whatLiesWithin (Within (_,y)) = y +-- | Map the inner part of the `Within` value to a new `Path`. mapWithin :: (Path Rel s -> Path Rel t) -> Within a s -> Within a t mapWithin f (Within (x,y)) = Within (x, f y) +-- | Map the inner part of the `Within` value to a new `Path` with an operation that may throw. mapWithinT :: MonadThrow m => (Path Rel s -> m (Path Rel t)) -> Within a s -> m (Within a t) mapWithinT f (Within (x,y)) = f y >>= \z -> return (Within (x, z)) +-- | Switch the outer part of a `Within` value to a new directory immediately. blinkWithin :: Path b Dir -> Within a t -> Within b t blinkWithin = moveWithin . const +-- | Map the outer part of a `Within` value via a function that changes the directory. moveWithin :: (Path a Dir -> Path b Dir) -> Within a t -> Within b t moveWithin f (Within (x,y)) = Within (f x, y) +-- | Map the outer part of a `Within` value via a function that changes the directory with an operation that may throw. moveWithinT :: MonadThrow m => (Path a Dir -> m (Path b Dir)) -> Within a t -> m (Within b t) moveWithinT f (Within (x,y)) = f x >>= \z -> return (Within (z,y)) +-- | `blinkWithin` and `mapWithinT` simultaneously. blinkAndMapT :: MonadThrow m => Path b Dir -> (Path Rel s -> m (Path Rel t)) -> Within a s -> m (Within b t) blinkAndMapT k g (Within (_,y)) = do y' <- g y return $ Within (k, y') +-- | `moveWithinT` and `mapWithinT` simultaneously. moveAndMapT :: MonadThrow m => (Path a Dir -> m (Path b Dir)) -> (Path Rel s -> m (Path Rel t)) -> Within a s -> m (Within b t) moveAndMapT f g (Within (x,y)) = do x' <- f x
within.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: b387258ad76af8d3a2c66d14e6c7814a0c5b23ceabe75719dbbb8aae3e8a6bde+-- hash: d64000b16dcb0fb670b8181322d54239181b081453413aabeeddfc44536d945f name: within-version: 0.0.1.0+version: 0.0.1.1 synopsis: A path within another path. description: Simple type for representing a well-typed path within another path. Useful for when you need to jump between directories and change filenames independently. Uses the path library. category: Filesystem