{-# LANGUAGE OverloadedStrings #-}

--------------------------------------------------------------------------------
--  See end of this file for licence information.
--------------------------------------------------------------------------------
-- |
--  Module      :  MapDecimal
--  Copyright   :  (c) 2003, Graham Klyne, 2009 Vasili I Galchin,
--                     2011 William Waites, 2011, 2012 Douglas Burke
--  License     :  GPL V2
--
--  Maintainer  :  Douglas Burke
--  Stability   :  experimental
--  Portability :  OverloadedStrings
--
--  This module defines the datatytpe mapping and relation values
--  used for RDF dataype @xsd:decimal@.
--
--------------------------------------------------------------------------------

module Swish.RDF.Datatype.XSD.MapDecimal (mapXsdDecimal) where

import Swish.Datatype (DatatypeMap(..))

import qualified Data.Text as T
import qualified Data.Text.Read as T

------------------------------------------------------------
--  Implementation of DatatypeMap for xsd:decimal
------------------------------------------------------------

-- | Functions that perform lexical-to-value
--  and value-to-canonical-lexical mappings for @xsd:decimal@ values.
--
mapXsdDecimal :: DatatypeMap Double
mapXsdDecimal :: DatatypeMap Double
mapXsdDecimal = DatatypeMap
    { -- mapL2V :: T.Text -> Maybe Double
      mapL2V :: Text -> Maybe Double
mapL2V = \Text
txt -> case Reader Double
T.double Text
txt of
                         Right (Double
val, Text
"") -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
val
                         Either String (Double, Text)
_ -> Maybe Double
forall a. Maybe a
Nothing
         
      -- mapV2L :: Double -> Maybe T.Text
      -- TODO: for now convert via String as issues with text-format
      --       (inability to use with ghci)   
    , mapV2L :: Double -> Maybe Text
mapV2L = Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> (Double -> Text) -> Double -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (Double -> String) -> Double -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> String
forall a. Show a => a -> String
show
    }

--------------------------------------------------------------------------------
--
--  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,
--                2011 William Waites, 2011, 2012 Douglas Burke
--
--  All rights reserved.
--
--  This file is part of Swish.
--
--  Swish 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 2 of the License, or
--  (at your option) any later version.
--
--  Swish 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 Swish; if not, write to:
--    The Free Software Foundation, Inc.,
--    59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--
--------------------------------------------------------------------------------