From 5d7d39c3fccb587fc6fb8b8194883bff46896a5f Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Sat, 16 Mar 2024 15:54:38 +0000 Subject: [PATCH] Fix all non-exhaustive pattern matches --- tasklite-core/package.yaml | 1 + tasklite-core/source/ImportExport.hs | 36 ++++++++++++++-------------- tasklite-core/source/Utils.hs | 13 ++++++++-- tasklite-core/tasklite-core.cabal | 1 + 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/tasklite-core/package.yaml b/tasklite-core/package.yaml index 65260a3..fef2516 100644 --- a/tasklite-core/package.yaml +++ b/tasklite-core/package.yaml @@ -71,6 +71,7 @@ library: - protolude - QuickCheck - quickcheck-instances + - random - read-editor - simple-sql-parser - sqlite-simple diff --git a/tasklite-core/source/ImportExport.hs b/tasklite-core/source/ImportExport.hs index 6be5dbe..1c1f135 100644 --- a/tasklite-core/source/ImportExport.hs +++ b/tasklite-core/source/ImportExport.hs @@ -43,6 +43,7 @@ import Protolude ( import Protolude qualified as P import Config (Config (dataDir, dbName)) +import Control.Arrow ((>>>)) import Data.Aeson as Aeson ( FromJSON (parseJSON), ToJSON, @@ -126,6 +127,7 @@ import Text.PortableLines.ByteString.Lazy (lines8) import Time.System (timeCurrent) import Utils ( IdText, + emptyUlid, parseUlidText, parseUtc, setDateTime, @@ -156,23 +158,23 @@ instance FromJSON Annotation where annotationToNote :: Annotation -> Note -annotationToNote annot@Annotation{entry = entry, description = description} = +annotationToNote annot@Annotation{entry, description} = do let - utc = fromMaybe (timeFromElapsedP 0 :: DateTime) (parseUtc entry) - Right ulidGenerated = (ulidFromInteger . abs . toInteger . hash) annot - ulidCombined = setDateTime ulidGenerated utc - in - Note - { ulid = (T.toLower . show) ulidCombined - , body = description - } + utc = entry & parseUtc & fromMaybe (timeFromElapsedP 0 :: DateTime) + ulidGeneratedRes = annot & (hash >>> toInteger >>> abs >>> ulidFromInteger) + ulidCombined = (ulidGeneratedRes & P.fromRight emptyUlid) `setDateTime` utc + + Note + { ulid = (T.toLower . show) ulidCombined + , body = description + } textToNote :: DateTime -> Text -> Note textToNote utc body = let - Right ulidGenerated = (ulidFromInteger . abs . toInteger . hash) body - ulidCombined = setDateTime ulidGenerated utc + ulidGeneratedRes = body & (hash >>> toInteger >>> abs >>> ulidFromInteger) + ulidCombined = (ulidGeneratedRes & P.fromRight emptyUlid) `setDateTime` utc in Note { ulid = (T.toLower . show) ulidCombined @@ -428,8 +430,8 @@ instance FromJSON ImportTask where o_ulid <- o .:? "ulid" let - Right ulidGenerated = (ulidFromInteger . abs . toInteger . hash) tempTask - ulidCombined = setDateTime ulidGenerated createdUtc + ulidGeneratedRes = tempTask & (hash >>> toInteger >>> abs >>> ulidFromInteger) + ulidCombined = (ulidGeneratedRes & P.fromRight emptyUlid) `setDateTime` createdUtc ulid = T.toLower $ fromMaybe @@ -503,8 +505,7 @@ emailToImportTask email@(Message headerFields msgBody) = Task.body task <> ( msgBody & lines8 - <&> TL.decodeUtf8 - <&> toStrict + <&> (TL.decodeUtf8 >>> toStrict) & T.unlines & T.dropEnd 1 ) @@ -530,9 +531,8 @@ emailToImportTask email@(Message headerFields msgBody) = Email.Date emailDate -> let utc = zonedTimeToDateTime emailDate - Right ulidGenerated = - (ulidFromInteger . abs . toInteger . hash) $ (show email :: Text) - ulidCombined = setDateTime ulidGenerated utc + ulidGeneratedRes = (email & show :: Text) & (hash >>> toInteger >>> abs >>> ulidFromInteger) + ulidCombined = (ulidGeneratedRes & P.fromRight emptyUlid) `setDateTime` utc in ImportTask task diff --git a/tasklite-core/source/Utils.hs b/tasklite-core/source/Utils.hs index a267c2f..dfb663d 100644 --- a/tasklite-core/source/Utils.hs +++ b/tasklite-core/source/Utils.hs @@ -76,8 +76,8 @@ import Data.Text as T ( ) import Data.Time (UTCTime, ZonedTime, addUTCTime, zonedTimeToUTC) import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds) -import Data.ULID (ULID (ULID, random)) -import Data.ULID.Random (ULIDRandom) +import Data.ULID (ULID (ULID, random, timeStamp)) +import Data.ULID.Random (ULIDRandom, mkULIDRandom) import Data.ULID.TimeStamp (ULIDTimeStamp, mkULIDTimeStamp) import Prettyprinter (Doc, Pretty (pretty), softline) import Prettyprinter.Render.Terminal ( @@ -94,6 +94,7 @@ import Config ( Hook (body, filePath, interpreter), ) import Control.Arrow ((>>>)) +import System.Random (mkStdGen) type IdText = Text @@ -117,6 +118,14 @@ zeroTime :: DateTime zeroTime = timeFromElapsedP 0 +emptyUlid :: ULID +emptyUlid = + ULID + { timeStamp = mkULIDTimeStamp 0 + , random = mkULIDRandom (mkStdGen 0) & P.fst + } + + utcFormatReadable :: TimeFormatString utcFormatReadable = toFormat ("YYYY-MM-DD H:MI:S" :: [Char]) diff --git a/tasklite-core/tasklite-core.cabal b/tasklite-core/tasklite-core.cabal index e6ebea5..a78dbbb 100644 --- a/tasklite-core/tasklite-core.cabal +++ b/tasklite-core/tasklite-core.cabal @@ -85,6 +85,7 @@ library , process , protolude , quickcheck-instances + , random , read-editor , simple-sql-parser , sqlite-simple