module Text.XML.DOM.Parser.FromAttribute
  ( -- * FromAttribute
    FromAttribute(..)
  , proxyFromAttribute
    -- * Explicit methods
  , textFromAttribute
  , stringFromAttribute
  , charFromAttribute
  , intFromAttribute
  , integerFromAttribute
  , doubleFromAttribute
  , fixedFromAttribute
  , boolFromAttribute
  , unitFromAttribute
  , voidFromAttribute
  , scientificFromAttribute
  ) where

import Data.Fixed
import Data.Scientific
import Data.Text as T
import Data.Typeable
import Data.Void
import Text.XML.DOM.Parser.Content

-- | Class of types which can be get from attribute value. Method
-- 'fromAttribute' is a convenient default parameter for
-- 'parseAttribute'
--
-- @since 2.0.0
class FromAttribute a where
  fromAttribute
    :: Text
    -- ^ Attribute contents to parse
    -> Either Text a
    -- ^ Either error message or result

proxyFromAttribute
  :: FromAttribute a
  => Proxy a
  -> Text
  -> Either Text a
proxyFromAttribute :: forall a. FromAttribute a => Proxy a -> Text -> Either Text a
proxyFromAttribute Proxy a
_ = Text -> Either Text a
forall a. FromAttribute a => Text -> Either Text a
fromAttribute

instance FromAttribute () where
  fromAttribute :: Text -> Either Text ()
fromAttribute = Text -> Either Text ()
unitFromAttribute

unitFromAttribute :: Text -> Either Text ()
unitFromAttribute :: Text -> Either Text ()
unitFromAttribute Text
_ = () -> Either Text ()
forall a b. b -> Either a b
Right ()

instance FromAttribute Void where
  fromAttribute :: Text -> Either Text Void
fromAttribute = Text -> Either Text Void
voidFromAttribute

voidFromAttribute :: Text -> Either Text Void
voidFromAttribute :: Text -> Either Text Void
voidFromAttribute Text
_ = Text -> Either Text Void
forall a b. a -> Either a b
Left Text
"Void has been parsed"

instance FromAttribute Text where
  fromAttribute :: Text -> Either Text Text
fromAttribute = Text -> Either Text Text
textFromAttribute

textFromAttribute :: Text -> Either Text Text
textFromAttribute :: Text -> Either Text Text
textFromAttribute = Text -> Either Text Text
forall a b. b -> Either a b
Right

instance FromAttribute String where
  fromAttribute :: Text -> Either Text String
fromAttribute = Text -> Either Text String
stringFromAttribute

stringFromAttribute :: Text -> Either Text String
stringFromAttribute :: Text -> Either Text String
stringFromAttribute = String -> Either Text String
forall a b. b -> Either a b
Right (String -> Either Text String)
-> (Text -> String) -> Text -> Either Text String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack

instance FromAttribute Char where
  fromAttribute :: Text -> Either Text Char
fromAttribute = Text -> Either Text Char
charFromAttribute

charFromAttribute :: Text -> Either Text Char
charFromAttribute :: Text -> Either Text Char
charFromAttribute = Text -> Either Text Char
readChar

instance FromAttribute Int where
  fromAttribute :: Text -> Either Text Int
fromAttribute = Text -> Either Text Int
intFromAttribute

intFromAttribute :: Text -> Either Text Int
intFromAttribute :: Text -> Either Text Int
intFromAttribute = Text -> Either Text Int
forall a. (Read a, Typeable a) => Text -> Either Text a
readContent

instance FromAttribute Integer where
  fromAttribute :: Text -> Either Text Integer
fromAttribute = Text -> Either Text Integer
integerFromAttribute

integerFromAttribute :: Text -> Either Text Integer
integerFromAttribute :: Text -> Either Text Integer
integerFromAttribute = Text -> Either Text Integer
forall a. (Read a, Typeable a) => Text -> Either Text a
readContent

instance FromAttribute Double where
  fromAttribute :: Text -> Either Text Double
fromAttribute = Text -> Either Text Double
doubleFromAttribute

doubleFromAttribute :: Text -> Either Text Double
doubleFromAttribute :: Text -> Either Text Double
doubleFromAttribute = Text -> Either Text Double
forall a. (Read a, Typeable a) => Text -> Either Text a
readContent

instance (Typeable a, HasResolution a) => FromAttribute (Fixed a) where
  fromAttribute :: Text -> Either Text (Fixed a)
fromAttribute = Text -> Either Text (Fixed a)
forall a.
(HasResolution a, Typeable a) =>
Text -> Either Text (Fixed a)
fixedFromAttribute

fixedFromAttribute
  :: (HasResolution a, Typeable a)
  => Text
  -> Either Text (Fixed a)
fixedFromAttribute :: forall a.
(HasResolution a, Typeable a) =>
Text -> Either Text (Fixed a)
fixedFromAttribute = Text -> Either Text (Fixed a)
forall a. (Read a, Typeable a) => Text -> Either Text a
readContent

instance FromAttribute Bool where
  fromAttribute :: Text -> Either Text Bool
fromAttribute = Text -> Either Text Bool
boolFromAttribute

boolFromAttribute :: Text -> Either Text Bool
boolFromAttribute :: Text -> Either Text Bool
boolFromAttribute = Text -> Either Text Bool
readBool

instance FromAttribute Scientific where
  fromAttribute :: Text -> Either Text Scientific
fromAttribute = Text -> Either Text Scientific
scientificFromAttribute

scientificFromAttribute :: Text -> Either Text Scientific
scientificFromAttribute :: Text -> Either Text Scientific
scientificFromAttribute = Text -> Either Text Scientific
forall a. (Read a, Typeable a) => Text -> Either Text a
readContent