From 2c57d1dffca6e2ed6264277792459521d47ff222 Mon Sep 17 00:00:00 2001 From: Sean D Gillespie Date: Thu, 25 Jan 2024 22:40:38 -0500 Subject: [PATCH] refactor(lib): Extract Errors into its own module --- gibberish.cabal | 1 + src/Data/Gibberish/Errors.hs | 28 ++++++++++++++++++++++++++++ src/Data/Gibberish/Types.hs | 18 +++--------------- test/Data/Gibberish/TrigraphSpec.hs | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 src/Data/Gibberish/Errors.hs diff --git a/gibberish.cabal b/gibberish.cabal index 622fd89..fc1b2b6 100644 --- a/gibberish.cabal +++ b/gibberish.cabal @@ -97,6 +97,7 @@ library Data.Elocrypt, Data.Elocrypt.Trigraph, Data.Elocrypt.Utils, + Data.Gibberish.Errors, Data.Gibberish.Format, Data.Gibberish.MonadPass, Data.Gibberish.Trigraph, diff --git a/src/Data/Gibberish/Errors.hs b/src/Data/Gibberish/Errors.hs new file mode 100644 index 0000000..1f390c0 --- /dev/null +++ b/src/Data/Gibberish/Errors.hs @@ -0,0 +1,28 @@ +module Data.Gibberish.Errors + ( GibberishErr (..), + isTrigraphNotFound, + isImpossibleError, + ) where + +import Control.Exception (Exception ()) +import Data.Typeable (Typeable ()) + +-- | Exceptions that can occur at runtime +data GibberishErr + = TrigraphNotFound FilePath + | ImpossibleError + deriving stock (Eq, Typeable) + +instance Exception GibberishErr + +instance Show GibberishErr where + show (TrigraphNotFound path) = "Trigraph file " <> show path <> " does not exist!" + show ImpossibleError = "The impossible happened! Please file a bug report." + +isTrigraphNotFound :: GibberishErr -> Bool +isTrigraphNotFound (TrigraphNotFound _) = True +isTrigraphNotFound _ = False + +isImpossibleError :: GibberishErr -> Bool +isImpossibleError ImpossibleError = True +isImpossibleError _ = False diff --git a/src/Data/Gibberish/Types.hs b/src/Data/Gibberish/Types.hs index e5845ff..d35f034 100644 --- a/src/Data/Gibberish/Types.hs +++ b/src/Data/Gibberish/Types.hs @@ -2,7 +2,6 @@ module Data.Gibberish.Types ( GenPassOptions (..), - Error (..), Unigram (..), Digram (..), Trigram (..), @@ -10,16 +9,17 @@ module Data.Gibberish.Types Frequencies (..), Trigraph (..), Word (..), + module Data.Gibberish.Errors, ) where +import Data.Gibberish.Errors + import Control.DeepSeq (NFData) -import Control.Exception (Exception ()) import Data.Aeson (FromJSON (..), FromJSONKey (..), ToJSON (..), ToJSONKey (..)) import Data.Aeson qualified as Aeson import Data.Aeson.Types (FromJSONKeyFunction (..), Parser (), toJSONKeyText) import Data.Map (Map ()) import Data.Text (Text ()) -import Data.Typeable (Typeable ()) import GHC.Generics (Generic ()) import TextShow (TextShow (..), fromString) import Prelude hiding (Word ()) @@ -37,12 +37,6 @@ data GenPassOptions = GenPassOptions } deriving stock (Eq, Show) --- | Exceptions that can occur at runtime -data Error - = TrigraphNotFound FilePath - | ImpossibleError - deriving stock (Eq, Typeable) - -- | A unigram is a single letter newtype Unigram = Unigram {unUnigram :: Char} deriving stock (Eq, Ord, Show) @@ -79,12 +73,6 @@ newtype Trigraph = Trigraph {unTrigraph :: Map Digram Frequencies} newtype Word = Word {unWord :: Text} deriving stock (Eq, Show) -instance Exception Error - -instance Show Error where - show (TrigraphNotFound path) = "Trigraph file " <> show path <> " does not exist!" - show ImpossibleError = "The impossible happened! Please file a bug report." - instance TextShow Digram where showb (Digram c1 c2) = fromString [c1, c2] diff --git a/test/Data/Gibberish/TrigraphSpec.hs b/test/Data/Gibberish/TrigraphSpec.hs index daf103f..6262ceb 100644 --- a/test/Data/Gibberish/TrigraphSpec.hs +++ b/test/Data/Gibberish/TrigraphSpec.hs @@ -70,7 +70,7 @@ spec = do it "handles load failure" $ do loadTrigraph (CustomTrigraph $ TrigraphConfig "doesnotexist.json") - `shouldThrow` (== TrigraphNotFound "doesnotexist.json") + `shouldThrow` isTrigraphNotFound trigrams :: Text -> [Trigram] trigrams ts = case Text.take 3 ts of