-- | Utils relayted to pdf objects

module Pdf.Core.Object.Util
( intValue
, int64Value
, boolValue
, realValue
, nameValue
, stringValue
, arrayValue
, streamValue
, refValue
, dictValue
)
where

import Pdf.Core.Object

import Data.ByteString (ByteString)
import Data.Scientific (Scientific)
import qualified Data.Scientific as Scientific
import Data.Int (Int64)

-- | Try to convert object to 'Int'
--
-- Floating value doesn't automatically get converted
intValue :: Object -> Maybe Int
intValue :: Object -> Maybe Int
intValue (Number Scientific
n)
  = (Double -> Maybe Int)
-> (Int -> Maybe Int) -> Either Double Int -> Maybe Int
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Int -> Double -> Maybe Int
forall a b. a -> b -> a
const Maybe Int
forall a. Maybe a
Nothing) Int -> Maybe Int
forall a. a -> Maybe a
Just
  (Either Double Int -> Maybe Int)
-> (Scientific -> Either Double Int) -> Scientific -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scientific -> Either Double Int
floatingOrInteger
  (Scientific -> Maybe Int) -> Scientific -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Scientific
n
intValue Object
_ = Maybe Int
forall a. Maybe a
Nothing

-- | Specialized to prevent defaulting warning
floatingOrInteger :: Scientific -> Either Double Int
floatingOrInteger :: Scientific -> Either Double Int
floatingOrInteger = Scientific -> Either Double Int
forall r i. (RealFloat r, Integral i) => Scientific -> Either r i
Scientific.floatingOrInteger

-- | Try to convert object to 'Int64'.
--
-- This is for cases, where according to the specs values above 2^29
-- (Int) have to be expected.
int64Value :: Object -> Maybe Int64
int64Value :: Object -> Maybe Int64
int64Value (Number Scientific
n) = Scientific -> Maybe Int64
forall i. (Integral i, Bounded i) => Scientific -> Maybe i
Scientific.toBoundedInteger Scientific
n
int64Value Object
_ = Maybe Int64
forall a. Maybe a
Nothing

-- | Try to convert object to 'Bool'
boolValue :: Object -> Maybe Bool
boolValue :: Object -> Maybe Bool
boolValue (Bool Bool
b) = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
b
boolValue Object
_ = Maybe Bool
forall a. Maybe a
Nothing

-- | Try to convert object to 'Double'
--
-- Integral value automatically gets converted
realValue :: Object -> Maybe Double
realValue :: Object -> Maybe Double
realValue (Number Scientific
n)
  = (Double -> Maybe Double)
-> (Int -> Maybe Double) -> Either Double Int -> Maybe Double
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> (Int -> Double) -> Int -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
  (Either Double Int -> Maybe Double)
-> (Scientific -> Either Double Int) -> Scientific -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scientific -> Either Double Int
floatingOrInteger
  (Scientific -> Maybe Double) -> Scientific -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Scientific
n
realValue Object
_ = Maybe Double
forall a. Maybe a
Nothing

-- | Try to convert object to 'Name'
nameValue :: Object -> Maybe Name
nameValue :: Object -> Maybe Name
nameValue (Name Name
n) = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n
nameValue Object
_ = Maybe Name
forall a. Maybe a
Nothing

-- | Try to convert object to 'ByteString'
stringValue :: Object -> Maybe ByteString
stringValue :: Object -> Maybe ByteString
stringValue (String ByteString
s) = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
s
stringValue Object
_ = Maybe ByteString
forall a. Maybe a
Nothing

-- | Try to convert object to array
arrayValue :: Object -> Maybe Array
arrayValue :: Object -> Maybe Array
arrayValue (Array Array
arr) = Array -> Maybe Array
forall a. a -> Maybe a
Just Array
arr
arrayValue Object
_ = Maybe Array
forall a. Maybe a
Nothing

-- | Try to convert object to stream
streamValue :: Object -> Maybe Stream
streamValue :: Object -> Maybe Stream
streamValue (Stream Stream
s) = Stream -> Maybe Stream
forall a. a -> Maybe a
Just Stream
s
streamValue Object
_ = Maybe Stream
forall a. Maybe a
Nothing

-- | Try to convert object to reference
refValue :: Object -> Maybe Ref
refValue :: Object -> Maybe Ref
refValue (Ref Ref
ref) = Ref -> Maybe Ref
forall a. a -> Maybe a
Just Ref
ref
refValue Object
_ = Maybe Ref
forall a. Maybe a
Nothing

-- | Try to convert object to dictionary
dictValue :: Object -> Maybe Dict
dictValue :: Object -> Maybe Dict
dictValue (Dict Dict
d) = Dict -> Maybe Dict
forall a. a -> Maybe a
Just Dict
d
dictValue Object
_ = Maybe Dict
forall a. Maybe a
Nothing