• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lehins / mempack / 13

26 Sep 2025 08:57PM UTC coverage: 84.043% (-1.9%) from 85.987%
13

push

github

web-flow
Merge pull request #14 from lehins/add-buffer-instances-for-vector

Add instances for `primitive` and `vector`

61 of 91 new or added lines in 2 files covered. (67.03%)

56 existing lines in 3 files now uncovered.

711 of 846 relevant lines covered (84.04%)

1.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

34.78
/src/Data/MemPack/Error.hs
1
{-# LANGUAGE DefaultSignatures #-}
2
{-# LANGUAGE DerivingStrategies #-}
3
{-# LANGUAGE GADTs #-}
4
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5
{-# LANGUAGE RecordWildCards #-}
6

7
-- |
8
-- Module      : Data.MemPack.Error
9
-- Copyright   : (c) Alexey Kuleshevich 2024-2025
10
-- License     : BSD3
11
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
12
-- Stability   : experimental
13
-- Portability : non-portable
14
module Data.MemPack.Error where
15

16
import Control.Exception
17
import Data.List.NonEmpty as NE
18
import Data.Text (Text)
19
import Data.Typeable
20
import GHC.Exts
21

22
data SomeError where
23
  SomeError :: (Typeable e, Error e) => e -> SomeError
24

UNCOV
25
instance Show SomeError where
×
26
  showsPrec p (SomeError e) = showsPrec p e
×
27

UNCOV
28
instance Exception SomeError
×
29

30
-- | Very similar interface to `Exceptions`, except not intended for runtime exceptions.
31
class Show e => Error e where
32
  toSomeError :: e -> SomeError
33
  default toSomeError :: Typeable e => e -> SomeError
34
  toSomeError = SomeError
2✔
35

36
  fromSomeError :: SomeError -> Maybe e
37
  default fromSomeError :: Typeable e => SomeError -> Maybe e
38
  fromSomeError (SomeError t) = cast t
2✔
39

40
instance Error SomeError where
41
  toSomeError = id
2✔
UNCOV
42
  fromSomeError = Just
×
43

44
newtype TextError = TextError Text
UNCOV
45
  deriving newtype (Eq, Show, IsString)
×
46

UNCOV
47
instance Error TextError
×
48

49
instance IsString SomeError where
UNCOV
50
  fromString = toSomeError . TextError . fromString
×
51

52
newtype ManyErrors = ManyErrors (NonEmpty SomeError)
UNCOV
53
  deriving (Show)
×
54

55
instance Error ManyErrors
2✔
56

57
data UnknownError = UnknownError
UNCOV
58
  deriving (Show)
×
59

UNCOV
60
instance Error UnknownError
×
61

62
fromMultipleErrors :: [SomeError] -> SomeError
63
fromMultipleErrors es =
2✔
64
  case es of
2✔
UNCOV
65
    [] -> toSomeError UnknownError
×
66
    [e] -> e
2✔
67
    e : rest -> toSomeError $ ManyErrors (e :| rest)
2✔
68
{-# NOINLINE fromMultipleErrors #-}
69

70

71
data RanOutOfBytesError = RanOutOfBytesError
72
  { ranOutOfBytesRead :: Int
2✔
73
  , ranOutOfBytesAvailable :: Int
2✔
74
  , ranOutOfBytesRequested :: Int
2✔
75
  }
UNCOV
76
  deriving (Eq, Ord)
×
77

UNCOV
78
instance Show RanOutOfBytesError where
×
79
  show RanOutOfBytesError{..} =
×
80
    "Ran out of bytes. Read "
×
81
      <> showBytes ranOutOfBytesRead
×
82
      <> " out of "
×
83
      <> showBytes ranOutOfBytesAvailable
×
84
      <> ". Requested to read "
×
85
      <> showBytes ranOutOfBytesRequested
×
86
      <> " more."
×
87

88
instance Error RanOutOfBytesError
2✔
89

90
data NotFullyConsumedError = NotFullyConsumedError
91
  { notFullyConsumedRead :: Int
2✔
92
  , notFullyConsumedAvailable :: Int
2✔
93
  , notFullyConsumedTypeName :: String
2✔
94
  }
UNCOV
95
  deriving (Eq, Ord)
×
96

UNCOV
97
instance Show NotFullyConsumedError where
×
98
  show NotFullyConsumedError{..} =
×
99
    "Buffer of length " <> showBytes notFullyConsumedAvailable
×
100
      ++ " was not fully consumed while unpacking '" <> notFullyConsumedTypeName
×
101
      ++ "'. Unconsumed " <> showBytes (notFullyConsumedAvailable - notFullyConsumedRead)
×
102
      ++ " was leftover."
×
103

104
instance Error NotFullyConsumedError
2✔
105

106
showBytes :: Int -> String
UNCOV
107
showBytes 1 = "1 byte"
×
108
showBytes n = show n ++ " bytes"
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc