error-or-utils (empty) → 0.1.1
raw patch · 6 files changed
+221/−0 lines, 6 filesdep +basedep +containersdep +error-orsetup-changed
Dependencies added: base, containers, error-or, text
Files
- LICENSE +30/−0
- README.md +3/−0
- Setup.hs +2/−0
- error-or-utils.cabal +38/−0
- src/Data/ErrorOr/Utils.hs +55/−0
- src/Data/ErrorOr/Validation.hs +93/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Kamil Dworakowski (c) 2020++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Kamil Dworakowski nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,3 @@+# error-or-utils++Utilities that build on error-or package.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ error-or-utils.cabal view
@@ -0,0 +1,38 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.33.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: f0885d5941e462b134b8a0763a986916a5b00a0c4a030a1690c157a1bec1e67a++name: error-or-utils+version: 0.1.1+synopsis: Utilities using ErrorOr datatype+description: Builds on the error type defined in the error-or package+category: data,library,testing+homepage: https://github.com/luntain/error-or-bundle/blob/master/error-or-utils#readme+author: Kamil Dworakowski+maintainer: kamil@dworakowski.name+copyright: 2020 Kamil Dworakowski+license: BSD3+license-file: LICENSE+tested-with: GHC==8.0.1, GHC==8.2.2, GHC==8.8.1, GHC==8.8.3, GHC==8.10.2+build-type: Simple+extra-source-files:+ README.md++library+ exposed-modules:+ Data.ErrorOr.Utils+ Data.ErrorOr.Validation+ other-modules:+ Paths_error_or_utils+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , containers+ , error-or+ , text+ default-language: Haskell2010
+ src/Data/ErrorOr/Utils.hs view
@@ -0,0 +1,55 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedStrings #-}+-- | A few utility functions with improved error reporting.+module Data.ErrorOr.Utils (+ tryRead, Data.ErrorOr.Utils.lookup, lookupIn, OverloadedLookup(..)+) where++import qualified Data.Map as M+import qualified Data.List as List+import qualified Data.Text as T+import qualified Data.Char++#if __GLASGOW_HASKELL__ < 880+import Prelude hiding (fail)+import Data.Semigroup+import Control.Monad.Fail (MonadFail(..))+#endif++-- | A class used to implement `lookup` and `lookupIn`.+class OverloadedLookup t k v | t -> k, t -> v where overloadedLookup :: k -> t -> Maybe v+instance Eq k => OverloadedLookup [(k,v)] k v where overloadedLookup = List.lookup+instance Ord k => OverloadedLookup (M.Map k v) k v where overloadedLookup = M.lookup++-- | Overloaded lookup with good error messages.+lookup :: (OverloadedLookup t k v, Show k, Show t, MonadFail m, Applicative m) => k -> t -> m v+lookup k xs = lookupIn (shorten 256 (T.pack $ show xs)) k xs++-- | This is a version of `lookup` for where there is no Show instance for the collection.+lookupIn :: (OverloadedLookup t k v, Show k, MonadFail m, Applicative m) => T.Text -> k -> t -> m v+lookupIn name k =+ maybe (fail $ "Can't lookup " <> show k <> " in " <> T.unpack name) pure . overloadedLookup k++shorten :: Int -> T.Text -> T.Text+shorten maxLen msg =+ case T.splitAt maxLen msg of+ (x, t) | t == T.empty -> x+ (x, _rest) -> T.take (maxLen - 5) x <> "(...)"++-- | A read with better error messages.+tryRead :: (Read a, Show a, MonadFail m) => String -> m a+tryRead str =+ case reads str of+ [] -> fail ("Can't read: " ++ shorten 256 str)+ [(a, rest)]+ | all Data.Char.isSpace rest -> return a+ parses -> fail ("Ambiguous parse: '" ++ str ++ "' " ++ shorten 256 (show parses))+ where+ shorten :: Int -> [Char] -> [Char]+ shorten maxLen msg =+ case splitAt maxLen msg of+ (x, t) | t == [] -> x+ (x, _rest) -> take (maxLen - 5) x ++ "(...)"
+ src/Data/ErrorOr/Validation.hs view
@@ -0,0 +1,93 @@+{-# Language CPP #-}+{-# Language OverloadedStrings #-}+-- | Utilities for data validation+module Data.ErrorOr.Validation+ ( (>!),+ (>=!),+ (<!),+ (<=!),+ (=!),+ (/=!),+ ensure,+ ensureIsNothing,+ ensureIsJust,+ approxEqual,+ ensureAll,+ )+where++import Data.ErrorOr+import qualified Data.Text as T+import Data.Foldable (sequenceA_)++#if __GLASGOW_HASKELL__ < 880+import Prelude hiding (fail)+import Data.Semigroup+import Control.Monad.Fail (MonadFail(..))+#endif++-- <> is infixr 6 :|, which forces parentheses around >! etc, but if I increase+-- priority on >! above 6, it will break relation to the arithmetic operators++infix 4 >!+(>!) :: (Ord a, Show a) => a -> a -> ErrorOr ()+a >! b = if a > b then pure () else binaryErr a "is not greater than" b++infix 4 >=!+(>=!) :: (Ord a, Show a) => a -> a -> ErrorOr ()+a >=! b = if a >= b then pure () else binaryErr a "is not >= than" b++infix 4 <!+(<!) :: (Ord a, Show a) => a -> a -> ErrorOr ()+a <! b = if a < b then pure () else binaryErr a "is not smaller than" b++infix 4 <=!+(<=!) :: (Ord a, Show a) => a -> a -> ErrorOr ()+a <=! b = if a <= b then pure () else binaryErr a "is not <= than" b++infix 4 =!+(=!) :: (Eq a, Show a) => a -> a -> ErrorOr ()+a =! b = if a == b then pure () else binaryErr a "is not equal to" b++infix 4 /=!+(/=!) :: (Eq a, Show a) => a -> a -> ErrorOr ()+a /=! b = if a/=b then pure () else binaryErr a "is equal to" b++-- | Checks the difference of the numbers is less than ratio times+-- the average of the two numbers.+approxEqual :: (RealFrac a, Show a) =>+#if __GLASGOW_HASKELL__ >= 880+ -- | ratio+#endif+ Double+ -> a+ -> a+ -> ErrorOr ()+approxEqual ratio x y =+ if abs (realToFrac x - realToFrac y) <= abs (ratio * avg)+ then pure ()+ else err ("The numbers are too far apart: " <> T.pack (show x) <> " " <> T.pack (show y))+ where+ avg :: Double+ avg = realToFrac (x + y) / 2++binaryErr :: Show a => a -> T.Text -> a -> ErrorOr ()+binaryErr a label b = fail ((show a) ++ " " ++ T.unpack label ++ " " ++ show b)++ensureIsNothing :: Show a => Maybe a -> ErrorOr ()+ensureIsNothing Nothing = pure ()+ensureIsNothing x = err ("Expected Nothing, but got " <> T.pack (show x))++ensureIsJust :: Maybe a -> ErrorOr ()+ensureIsJust Nothing = err ("Expected Just, but got Nothing")+ensureIsJust (Just _) = pure ()++-- | It annotates a failure with the element's show result.+ensureAll :: Show a => (a -> ErrorOr ()) -> [a] -> ErrorOr ()+ensureAll p = sequenceA_ . map p'+ where p' x = if p x /= pure ()+ then tag (T.pack $ show x) (p x)+ else pure ()++ensure :: T.Text -> Bool -> ErrorOr ()+ensure label condition = if condition then pure () else err label