packages feed

keuringsdienst-0.1.0.3 / src / Keuringsdienst / Helpers.hs

{-  Copyright © 2023 Josep Bigorra  This file is part of Keuringsdienst.  Keuringsdienst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,    either version 3 of the License, or (at your option) any later version.  Keuringsdienst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  You should have received a copy of the GNU General Public License along with Keuringsdienst.  If not, see <https://www.gnu.org/licenses/>.-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE UndecidableInstances #-}module Keuringsdienst.Helpers  ( filterFailedValidations,    isEqualTo,    isLesserThan,    isNegative,    isNegativeOrZero,    isNonEmptyText,    isPositive,    isPositiveOrZero,    isTextOfLength,    isTextSmallerThan,    isTextSmallerThanOrEqual,    isNotEqualTo,  )whereimport Data.Map as Mapimport Data.Text as Timport Keuringsdienst as KisEqualTo :: (Show a, Eq a) => a -> ValidationRule aisEqualTo value = ValidationRule $ \actual ->  if actual == value    then Success    else      Failure        [pack $ "Expected " <> show actual <> " to equal " <> show value]isNotEqualTo :: (Show a, Eq a) => a -> ValidationRule aisNotEqualTo value = ValidationRule $ \actual ->  if actual /= value    then Success    else      Failure        [pack $ "Expected " <> show actual <> " to not equal " <> show value]isGreaterThan :: (Show a, Ord a) => a -> ValidationRule aisGreaterThan ruleValue = ValidationRule $ \actual ->  if actual > ruleValue    then Success    else      Failure        [ pack (show actual <> " was expected to be greater than " <> show ruleValue)        ]isLesserThan :: (Show a, Ord a) => a -> ValidationRule aisLesserThan ruleValue = ValidationRule $ \actual ->  if actual < ruleValue    then Success    else      Failure        [ pack (show actual <> " was expected to be lesser than " <> show ruleValue)        ]isNonEmptyText :: ValidationRule TextisNonEmptyText = ValidationRule $ \actual ->  if T.null actual    then Failure [T.pack "Text was expected to be non-empty"]    else SuccessisTextOfLength :: Int -> ValidationRule TextisTextOfLength ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength /= ruleValue    then      Failure        [ T.pack            ( "Text was expected to be of size "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else SuccessisTextSmallerThan :: Int -> ValidationRule TextisTextSmallerThan ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength >= ruleValue    then      Failure        [ T.pack            ( "Text was expected to be smaller than "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else SuccessisTextSmallerThanOrEqual :: Int -> ValidationRule TextisTextSmallerThanOrEqual ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength > ruleValue    then      Failure        [ T.pack            ( "Text was expected to be smaller than "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else SuccessisNegative :: ValidationRule IntisNegative = isLesserThan 0isPositive :: ValidationRule IntisPositive = isGreaterThan 0isPositiveOrZero :: ValidationRule IntisPositiveOrZero = isPositive *||* isEqualTo 0isNegativeOrZero :: ValidationRule IntisNegativeOrZero = isNegative *||* isEqualTo 0filterFailedValidations :: Map Text (Validation err) -> Map Text (Validation err)filterFailedValidations =  Map.filter    ( \x -> do        case x of          Failure _ -> True          Success -> False    )