module System.FileSystem.Types
(
InApp
, DirName
, FileName
, FileCnt
, DirPath , toDirPath , fromDirPath
, FPath , toFPath , fromFPath
, FileData (..)
, emptyFD
, File (..)
, FSE
, FileSystem (..)
, emptyFileSystem
, modDirCnt
, Path (..)
, isFilePath
, FSState
, FST (..) , runFST
, FS , runFS
) where
import Data.ByteString (ByteString,empty)
import Data.Maybe (isNothing)
import System.Time (ClockTime (..))
import Control.Monad.Identity (Identity (..))
import Control.Monad.State (StateT,MonadIO,runStateT,MonadTrans)
import System.FilePath (splitPath , splitFileName , joinPath , combine)
import Control.Arrow ( first )
type InApp a = a -> a
type DirName = String
type FileName = String
type FileCnt = ByteString
type DirPath = [DirName]
toDirPath :: FilePath -> DirPath
toDirPath = splitPath
fromDirPath :: DirPath -> FilePath
fromDirPath = joinPath
type FPath = (DirPath,FileName)
toFPath :: FilePath -> FPath
toFPath = first splitPath . splitFileName
fromFPath :: FPath -> FilePath
fromFPath = uncurry combine . first fromDirPath
data FileData = FD
{ getCnt :: FileCnt
, getLmt :: ClockTime
} deriving Eq
emptyFD :: FileData
emptyFD = FD empty (TOD 0 0)
data File = File
{ getFD :: FileData
, getFN :: FileName
} deriving Eq
instance Show File where
show (File _ fn) = "FILE[" ++ fn ++ "]"
type FSE = Either (DirName,FileSystem) File
newtype FileSystem = Directory { dirCnt :: [FSE] } deriving Show
emptyFileSystem :: FileSystem
emptyFileSystem = Directory []
modDirCnt :: InApp [FSE] -> InApp FileSystem
modDirCnt f fs = fs { dirCnt = f $ dirCnt fs }
data Path = Path
{ pathList :: DirPath
, pathFile :: Maybe File
}
isFilePath :: Path -> Bool
isFilePath = not . isNothing . pathFile
type FSState = FileSystem
newtype FST m a = WrapFST { unwrapFST :: StateT FSState m a } deriving (Functor, Monad, MonadIO, MonadTrans)
runFST :: Monad m => FST m a -> FSState -> m (a,FSState)
runFST = runStateT . unwrapFST
type FS = FST Identity
runFS :: FS a -> FSState -> (a,FSState)
runFS c = runIdentity . runFST c