module Text.XML.Light.Extractors.Extra where

import Text.Read (readMaybe)


-- | Reads a floating point value or return @'Left' "float"@ if
-- the read fails.
float :: (Floating a, Read a) => String -> Either String a
float :: String -> Either String a
float = Either String a
-> (a -> Either String a) -> Maybe a -> Either String a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Either String a
forall a b. a -> Either a b
Left String
"float") a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Either String a)
-> (String -> Maybe a) -> String -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe


-- | Reads an integer value or return @'Left' "integer"@ if the read
-- fails.
integer :: (Integral a, Read a) => String -> Either String a
integer :: String -> Either String a
integer = Either String a
-> (a -> Either String a) -> Maybe a -> Either String a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Either String a
forall a b. a -> Either a b
Left String
"integer") a -> Either String a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Either String a)
-> (String -> Maybe a) -> String -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe