Add exceptions

This commit is contained in:
s464968 2024-05-26 17:47:23 +02:00
parent c6745f5b35
commit 75c0b88d29

View File

@ -4,7 +4,7 @@ module Main(main, parseTask, serializeTask, getPriority, comparePriority, TaskMa
import System.IO
import System.Directory
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip)
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip, splitOn)
import qualified Data.Text.IO as TIO
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
@ -14,6 +14,7 @@ import Data.Ord (comparing)
import Data.Char (isAlpha)
import qualified Data.Map as Map
import Data.Function (on)
import Control.Exception (catch, IOException)
todoFile :: FilePath
todoFile = "todo.txt"
@ -44,12 +45,17 @@ displayTasks = do
exists <- doesFileExist todoFile
if exists
then do
contents <- BSC.readFile todoFile
let taskMap = parseTask (pack (BSC.unpack contents))
contents <- catch (TIO.readFile todoFile) handleReadError
let taskMap = parseTask contents
putStrLn "Tasks:"
mapM_ (putStrLn . showTask) (Map.toList taskMap)
else putStrLn "No tasks found."
main
where
handleReadError :: IOException -> IO Text
handleReadError e = do
putStrLn $ "Error reading file: " ++ show e
return ""
addTask :: IO ()
addTask = do
@ -59,14 +65,22 @@ addTask = do
exists <- doesFileExist todoFile
if exists
then do
contents <- BSC.readFile todoFile
let taskMap = parseTask (pack (BSC.unpack contents))
contents <- catch (TIO.readFile todoFile) handleReadError
let taskMap = parseTask contents
newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1
newTaskMap = Map.insert newId (pack task) taskMap
BSC.writeFile todoFile (BSC.pack (unpack (serializeTask newTaskMap)))
else BSC.writeFile todoFile (BSC.pack (task <> "\n"))
catch (TIO.writeFile todoFile (serializeTask newTaskMap)) handleWriteError
else catch (TIO.writeFile todoFile (pack task <> "\n")) handleWriteError
putStrLn "Task added."
main
where
handleReadError :: IOException -> IO Text
handleReadError e = do
putStrLn $ "Error reading file: " ++ show e
return ""
handleWriteError :: IOException -> IO ()
handleWriteError e = putStrLn $ "Error writing file: " ++ show e
filterTasksByTag :: IO ()
filterTasksByTag = do
@ -76,26 +90,36 @@ filterTasksByTag = do
exists <- doesFileExist todoFile
if exists
then do
contents <- TIO.readFile todoFile
contents <- catch (TIO.readFile todoFile) handleReadError
let taskMap = parseTask contents
filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap
putStrLn "Filtered tasks:"
mapM_ (putStrLn . showTask) (Map.toList filteredTasks)
else putStrLn "No tasks found."
main
where
handleReadError :: IOException -> IO Text
handleReadError e = do
putStrLn $ "Error reading file: " ++ show e
return ""
sortTasksByPriority :: IO ()
sortTasksByPriority = do
exists <- doesFileExist todoFile
if exists
then do
contents <- TIO.readFile todoFile
contents <- catch (TIO.readFile todoFile) handleReadError
let taskMap = parseTask contents
sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap)
putStrLn "Sorted tasks by priority:"
mapM_ (putStrLn . showTask) sortedTasks
else putStrLn "No tasks found."
main
where
handleReadError :: IOException -> IO Text
handleReadError e = do
putStrLn $ "Error reading file: " ++ show e
return ""
comparePriority :: Text -> Text -> Ordering
comparePriority t1 t2 = comparing getPriority t1 t2