module Validation
  ( nonEmpty
  , number
  ) where

import Data.Text (Text)
import qualified Data.Text as T

nonEmpty :: a -> Text -> Either a Text
nonEmpty x str =
  if T.null str
   then Left x
   else Right str

number :: x -> (Int -> Bool) -> Text -> Either x Int
number x numberForm str =
  case reads (T.unpack str) :: [(Int, String)] of
    (num, _) : _ ->
      if numberForm num
        then Right num
        else Left x
    _ ->
      Left x