@@ -38,7 +38,6 @@ module Data.Path.Pathy
3838 , pathName
3939 , identicalPath
4040 , parentDir
41- , parentDir'
4241 , peel
4342 , posixEscaper
4443 , parsePath
@@ -81,7 +80,7 @@ import Data.String as S
8180import Data.String.NonEmpty (NonEmptyString , appendString )
8281import Data.String.NonEmpty (fromString , toString ) as NEString
8382import Data.Traversable (traverse )
84- import Data.Tuple (Tuple (..), fst , snd )
83+ import Data.Tuple (Tuple (..), snd )
8584import Partial.Unsafe (unsafeCrashWith )
8685import Unsafe.Coerce (unsafeCoerce )
8786
@@ -130,14 +129,14 @@ runName (Name name) = NEString.toString name
130129-- |
131130-- | This ADT allows invalid paths (e.g. paths inside files), but there is no
132131-- | possible way for such paths to be constructed by user-land code. The only
133- -- | "invalid path" that may be constructed is using the `parentDir' ` function, e.g.
134- -- | `parentDir' rootDir`, or by parsing an equivalent string such as `/../`,
132+ -- | "invalid path" that may be constructed is using the `parentDir` function, e.g.
133+ -- | `parentDir rootDir`, or by parsing an equivalent string such as `/../`,
135134-- | but such paths are marked as unsandboxed, and may not be rendered to strings
136135-- | until they are first sandboxed to some directory.
137136data Path (a :: RelOrAbs ) (b :: DirOrFile ) (s :: SandboxedOrNot )
138137 = Current
139138 | Root
140- | ParentIn (Path a b s )
139+ | ParentIn (Path a Dir s )
141140 | In (Path a Dir s ) (Name b )
142141
143142-- | A type describing a file whose location is given relative to some other,
@@ -277,18 +276,18 @@ pathName :: forall b s. AnyPath b s -> Either (Maybe (Name Dir)) (Name File)
277276pathName = bimap dirName fileName
278277
279278-- | Given a directory path, appends either a file or directory to the path.
280- appendPath :: forall a b s . Path a Dir s -> Path Rel b s -> Path a b s
279+ appendPath :: forall a b s . SplitDirOrFile b => Path a Dir s -> Path Rel b s -> Path a b s
280+ appendPath _ Root = unsafeCrashWith " Imposible as Root can't be Path Rel"
281281appendPath Current Current = Current
282282appendPath Root Current = Root
283- appendPath (ParentIn p1) Current = ParentIn (p1 </> Current )
284- appendPath (In p1 f1) Current = In (p1 </> Current ) (unsafeCoerce $ f1)
283+ -- TODO this shold be correct?
284+ -- appendPath (ParentIn p) c@Current = ParentIn (p </> c)
285+ appendPath (ParentIn p) Current = ParentIn (p </> Current )
286+ appendPath (In p1 (Name f1)) c@Current = case dirOrFile c of
287+ Left dir -> In (p1 </> dir) (Name f1)
288+ Right _ -> unsafeCrashWith " Imposible"
285289appendPath p1 (ParentIn p2) = ParentIn (p1 </> p2)
286290appendPath p1 (In p2 f2) = In (p1 </> p2) f2
287- -- following cases don't make sense but cannot exist
288- appendPath Current Root = Current
289- appendPath Root Root = Root
290- appendPath (ParentIn p1) Root = ParentIn (p1 </> Current )
291- appendPath (In p1 f1) Root = In (p1 </> Current ) (unsafeCoerce $ f1)
292291
293292infixl 6 appendPath as </>
294293
@@ -307,10 +306,11 @@ infixl 6 setExtension as <.>
307306-- | its previous sandbox.
308307parentAppend
309308 :: forall a b s s'
310- . Path a Dir s
309+ . SplitDirOrFile b
310+ => Path a Dir s
311311 -> Path Rel b s'
312312 -> Path a b Unsandboxed
313- parentAppend d p = parentDir' d </> unsandbox p
313+ parentAppend d p = parentDir d </> unsandbox p
314314
315315infixl 6 parentAppend as <..>
316316
@@ -337,12 +337,6 @@ depth Root = 0
337337depth (ParentIn p) = depth p - 1
338338depth (In p _) = depth p + 1
339339
340- -- | Attempts to extract out the parent directory of the specified path. If the
341- -- | function would have to use a relative path in the return value, the function will
342- -- | instead return `Nothing`.
343- parentDir :: forall a b s . Path a b s -> Maybe (Path a Dir s )
344- parentDir p = fst <$> peel p
345-
346340-- | Unsandboxes any path (whether sandboxed or not).
347341unsandbox :: forall a b s . Path a b s -> Path a b Unsandboxed
348342unsandbox Current = Current
@@ -352,8 +346,8 @@ unsandbox (In p n) = In (unsandbox p) n
352346
353347-- | Creates a path that points to the parent directory of the specified path.
354348-- | This function always unsandboxes the path.
355- parentDir' :: forall a b s . Path a b s -> Path a Dir Unsandboxed
356- parentDir' = ParentIn <<< unsafeCoerceType <<< unsandbox
349+ parentDir :: forall a b s . Path a Dir s -> Path a Dir Unsandboxed
350+ parentDir = ParentIn <<< unsandbox
357351
358352unsafeCoerceType :: forall a b b' s . Path a b s -> Path a b' s
359353unsafeCoerceType = unsafeCoerce
@@ -388,7 +382,7 @@ canonicalize = snd <<< canonicalize'
388382canonicalize' :: forall a b s . Path a b s -> Tuple Boolean (Path a b s )
389383canonicalize' Current = Tuple false Current
390384canonicalize' Root = Tuple false Root
391- canonicalize' (ParentIn (In p f)) = Tuple true (unsafeCoerceType $ snd $ canonicalize' p)
385+ canonicalize' (ParentIn (In p f)) = Tuple true (unsafeCoerceType $ snd $ canonicalize' p)
392386canonicalize' (ParentIn p) = case canonicalize' p of
393387 Tuple changed p' ->
394388 let p'' = ParentIn p'
@@ -511,12 +505,12 @@ parsePath rd ad rf af err p =
511505 false , true -> segsRaw
512506 false , false -> dropEnd 1 segsRaw
513507 last = length segsDropped - 1
514- folder :: forall a b s . Int -> Path a b s -> NonEmptyString -> Path a b s
508+ folder :: forall a b s . SplitDirOrFile b => Int -> Path a b s -> NonEmptyString -> Path a b s
515509 folder idx base seg =
516510 if NEString .toString seg == " ." then
517511 base
518512 else if NEString .toString seg == " .." then
519- ParentIn base
513+ ParentIn $ unsafeCoerceType base
520514 else In (unsafeCoerceType base) (Name seg)
521515 in
522516 case traverse NEString .fromString segsDropped of
@@ -546,7 +540,7 @@ parseAbsDir = parsePath (const Nothing) Just (const Nothing) (const Nothing) (co
546540instance showPath :: SplitDirOrFile b => Show (Path a b s ) where
547541 show Current = " currentDir"
548542 show Root = " rootDir"
549- show (ParentIn p) = " (parentDir' " <> show p <> " )"
543+ show (ParentIn p) = " (parentDir " <> show p <> " )"
550544 show (In p n ) = case dirOrFileName n of
551545 Left dirN ->
552546 " (" <> show p <> " </> dir " <> show dirN <> " )"
0 commit comments