-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adding.hs
22 lines (18 loc) · 865 Bytes
/
Adding.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Adding where
import FileSystem (FileSystem(..))
import ChangingDirections ( changeDir, changeEntity )
import Parser (getNextDir)
add :: String -> FileSystem -> Maybe FileSystem -> Maybe FileSystem
add path toAdd (Just old@(Root n xs)) = case getNextDir path of
Just ("", "") -> Just (Root n (toAdd : xs))
Just (rest, curr) -> case changeDir curr xs of
Nothing -> Nothing
Just dir@(Root path' files) -> case add rest toAdd (Just dir) of
Nothing -> Nothing
Just new@(Root dir xs'') -> Just (changeEntity new old)
Nothing -> Nothing
add _ _ _ = Nothing
addFile :: String -> String -> String -> Maybe FileSystem -> Maybe FileSystem
addFile path name val = add path (File name val)
addFolder :: String -> String -> Maybe FileSystem -> Maybe FileSystem
addFolder path name = add path (Root name [])