{-# language DeriveDataTypeable #-}
{-# language DeriveFoldable #-}
{-# language DeriveFunctor #-}
{-# language DeriveGeneric #-}
{-# language DeriveTraversable #-}
{-# language LambdaCase #-}
module System.Directory.Contents.Types where
import Data.Data
import Data.Map (Map)
import qualified Data.Map as Map
import GHC.Generics
import System.FilePath
data DirTree a
= DirTree_Dir FilePath (Map FileName (DirTree a))
| DirTree_File FilePath a
| DirTree_Symlink FilePath (Symlink a)
deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable, Generic, Data)
data Symlink a
= Symlink_Internal String FilePath
| Symlink_External String (Map FileName (DirTree a))
deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable, Generic, Data)
filePath :: DirTree a -> FilePath
filePath = \case
DirTree_Dir f _ -> f
DirTree_File f _ -> f
DirTree_Symlink f _ -> f
type FileName = String
fileName :: DirTree a -> FileName
fileName = takeFileName . filePath
fileNameMap :: [DirTree a] -> Map FileName (DirTree a)
fileNameMap xs = Map.fromList $ zip (fileName <$> xs) xs
insertSibling :: DirTree a -> Map FileName (DirTree a) -> Map FileName (DirTree a)
insertSibling a = Map.insert (fileName a) a
removeSibling :: DirTree a -> Map FileName (DirTree a) -> Map FileName (DirTree a)
removeSibling a = Map.delete (fileName a)
withFirstChild
:: Map FileName (DirTree a)
-> (DirTree a -> Map FileName (DirTree a) -> x)
-> Maybe x
withFirstChild m f = case Map.minView m of
Nothing -> Nothing
Just (firstChild, children) -> Just $ f firstChild children