packages feed

cursor-dirforest-0.0.0.0 / src / Cursor / FileOrDir.hs

{-# LANGUAGE DeriveFunctor #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE QuasiQuotes #-}module Cursor.FileOrDir whereimport Control.DeepSeqimport Cursor.Textimport Cursor.Typesimport qualified Data.Text as Timport Data.Validityimport Data.Validity.Path ()import GHC.Generics (Generic)import Pathdata FileOrDirCursor a  = Existent (FileOrDir a)  | InProgress TextCursor  deriving (Show, Eq, Generic, Functor)instance (Validity a) => Validity (FileOrDirCursor a)instance (NFData a) => NFData (FileOrDirCursor a)makeFileOrDirCursor :: FileOrDir a -> FileOrDirCursor amakeFileOrDirCursor = ExistentrebuildFileOrDirCursor :: FileOrDirCursor a -> Maybe (FileOrDir a)rebuildFileOrDirCursor = \case  Existent fod -> Just fod  InProgress _ -> NothingfileOrDirCursorInsertChar :: Char -> FileOrDirCursor a -> Maybe (FileOrDirCursor a)fileOrDirCursorInsertChar c = \case  Existent _ -> Nothing  InProgress tc -> InProgress <$> textCursorInsert c tcfileOrDirCursorAppendChar :: Char -> FileOrDirCursor a -> Maybe (FileOrDirCursor a)fileOrDirCursorAppendChar c = \case  Existent _ -> Nothing  InProgress tc -> InProgress <$> textCursorAppend c tcfileOrDirCursorRemoveChar :: FileOrDirCursor a -> Maybe (DeleteOrUpdate (FileOrDirCursor a))fileOrDirCursorRemoveChar = \case  Existent _ -> Nothing  InProgress tc -> fmap InProgress <$> textCursorRemove tcfileOrDirCursorDeleteChar :: FileOrDirCursor a -> Maybe (DeleteOrUpdate (FileOrDirCursor a))fileOrDirCursorDeleteChar = \case  Existent _ -> Nothing  InProgress tc -> fmap InProgress <$> textCursorDelete tcfileOrDirCursorSelectPrevChar :: FileOrDirCursor a -> Maybe (FileOrDirCursor a)fileOrDirCursorSelectPrevChar = \case  Existent _ -> Nothing  InProgress tc -> InProgress <$> textCursorSelectPrev tcfileOrDirCursorSelectNextChar :: FileOrDirCursor a -> Maybe (FileOrDirCursor a)fileOrDirCursorSelectNextChar = \case  Existent _ -> Nothing  InProgress tc -> InProgress <$> textCursorSelectNext tcfileOrDirCursorCompleteToDir :: FileOrDirCursor a -> Maybe (Path Rel Dir, FileOrDirCursor a)fileOrDirCursorCompleteToDir = fileOrDirCursorCompleteToDir' purefileOrDirCursorCompleteToDir' :: (Path Rel Dir -> Maybe (Path Rel Dir)) -> FileOrDirCursor a -> Maybe (Path Rel Dir, FileOrDirCursor a)fileOrDirCursorCompleteToDir' func = \case  Existent _ -> Nothing  InProgress tc -> case completeTextCursorToDir tc of    Nothing -> Nothing    Just rd ->      if isTopLevel rd        then do          rd' <- func rd          if isTopLevel rd'            then pure (rd', Existent $ FodDir rd')            else Nothing        else NothingcompleteTextCursorToDir :: TextCursor -> Maybe (Path Rel Dir)completeTextCursorToDir = parseRelDir . T.unpack . rebuildTextCursorfileOrDirCursorCompleteToFile :: a -> FileOrDirCursor a -> Maybe (Path Rel File, FileOrDirCursor a)fileOrDirCursorCompleteToFile = fileOrDirCursorCompleteToFile' purefileOrDirCursorCompleteToFile' :: (Path Rel File -> Maybe (Path Rel File)) -> a -> FileOrDirCursor a -> Maybe (Path Rel File, FileOrDirCursor a)fileOrDirCursorCompleteToFile' func a = \case  Existent _ -> Nothing  InProgress tc -> case completeTextCursorToFile tc of    Nothing -> Nothing    Just rf ->      if isTopLevel rf        then do          rf' <- func rf          if isTopLevel rf'            then Just (rf', Existent $ FodFile rf' a)            else Nothing        else NothingcompleteTextCursorToFile :: TextCursor -> Maybe (Path Rel File)completeTextCursorToFile = parseRelFile . T.unpack . rebuildTextCursordata FileOrDir a = FodFile (Path Rel File) a | FodDir (Path Rel Dir)  deriving (Show, Eq, Generic, Functor)instance (Validity a) => Validity (FileOrDir a) where  validate fod =    mconcat      [ genericValidate fod,        declare "The path is toplevel" $ case fod of          FodFile rf _ -> isTopLevel rf          FodDir rd -> isTopLevel rd      ]instance (NFData a) => NFData (FileOrDir a)isTopLevel :: Path Rel t -> BoolisTopLevel p_ = parent p_ == [reldir|./|]