Skip to content

Commit

Permalink
refactor(lib): Extract Errors into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed Jan 26, 2024
1 parent d33fc3b commit 2c57d1d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions gibberish.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions src/Data/Gibberish/Errors.hs
Original file line number Diff line number Diff line change
@@ -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
18 changes: 3 additions & 15 deletions src/Data/Gibberish/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

module Data.Gibberish.Types
( GenPassOptions (..),
Error (..),
Unigram (..),
Digram (..),
Trigram (..),
Frequency (..),
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 ())
Expand All @@ -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)
Expand Down Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion test/Data/Gibberish/TrigraphSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c57d1d

Please sign in to comment.