{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Codec.Xlsx.Types.Internal.FormulaData where
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Codec.Xlsx.Parser.Internal
import Codec.Xlsx.Types.Cell
import Codec.Xlsx.Types.Common
data FormulaData = FormulaData
{ FormulaData -> CellFormula
frmdFormula :: CellFormula
, FormulaData -> Maybe (SharedFormulaIndex, SharedFormulaOptions)
frmdShared :: Maybe (SharedFormulaIndex, SharedFormulaOptions)
} deriving (forall x. FormulaData -> Rep FormulaData x)
-> (forall x. Rep FormulaData x -> FormulaData)
-> Generic FormulaData
forall x. Rep FormulaData x -> FormulaData
forall x. FormulaData -> Rep FormulaData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FormulaData -> Rep FormulaData x
from :: forall x. FormulaData -> Rep FormulaData x
$cto :: forall x. Rep FormulaData x -> FormulaData
to :: forall x. Rep FormulaData x -> FormulaData
Generic
defaultFormulaType :: Text
defaultFormulaType :: Text
defaultFormulaType = Text
"normal"
instance FromXenoNode FormulaData where
fromXenoNode :: Node -> Either Text FormulaData
fromXenoNode Node
n = do
(bx, ca, t, mSi, mRef) <-
Node
-> AttrParser
(Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
-> Either
Text (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
forall a. Node -> AttrParser a -> Either Text a
parseAttributes Node
n (AttrParser
(Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
-> Either
Text (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser
(Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
-> Either
Text (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
forall a b. (a -> b) -> a -> b
$
(,,,,) (Bool
-> Bool
-> Text
-> Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser Bool
-> AttrParser
(Bool
-> Text
-> Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Bool -> AttrParser Bool
forall a. FromAttrBs a => ByteString -> a -> AttrParser a
fromAttrDef ByteString
"bx" Bool
False
AttrParser
(Bool
-> Text
-> Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser Bool
-> AttrParser
(Text
-> Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
forall a b. AttrParser (a -> b) -> AttrParser a -> AttrParser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> Bool -> AttrParser Bool
forall a. FromAttrBs a => ByteString -> a -> AttrParser a
fromAttrDef ByteString
"ca" Bool
False
AttrParser
(Text
-> Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser Text
-> AttrParser
(Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
forall a b. AttrParser (a -> b) -> AttrParser a -> AttrParser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> Text -> AttrParser Text
forall a. FromAttrBs a => ByteString -> a -> AttrParser a
fromAttrDef ByteString
"t" Text
defaultFormulaType
AttrParser
(Maybe SharedFormulaIndex
-> Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser (Maybe SharedFormulaIndex)
-> AttrParser
(Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
forall a b. AttrParser (a -> b) -> AttrParser a -> AttrParser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> AttrParser (Maybe SharedFormulaIndex)
forall a. FromAttrBs a => ByteString -> AttrParser (Maybe a)
maybeAttr ByteString
"si"
AttrParser
(Maybe CellRef
-> (Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef))
-> AttrParser (Maybe CellRef)
-> AttrParser
(Bool, Bool, Text, Maybe SharedFormulaIndex, Maybe CellRef)
forall a b. AttrParser (a -> b) -> AttrParser a -> AttrParser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> AttrParser (Maybe CellRef)
forall a. FromAttrBs a => ByteString -> AttrParser (Maybe a)
maybeAttr ByteString
"ref"
(expr, shared) <-
case t of
Text
d | Text
d Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
defaultFormulaType -> do
formula <- Node -> Either Text Text
contentX Node
n
return (NormalFormula $ Formula formula, Nothing)
Text
"shared" -> do
si <-
Either Text SharedFormulaIndex
-> (SharedFormulaIndex -> Either Text SharedFormulaIndex)
-> Maybe SharedFormulaIndex
-> Either Text SharedFormulaIndex
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(Text -> Either Text SharedFormulaIndex
forall a b. a -> Either a b
Left Text
"missing si attribute for shared formula")
SharedFormulaIndex -> Either Text SharedFormulaIndex
forall a. a -> Either Text a
forall (m :: * -> *) a. Monad m => a -> m a
return
Maybe SharedFormulaIndex
mSi
formula <- Formula <$> contentX n
return
( SharedFormula si
, mRef >>= \CellRef
ref -> (SharedFormulaIndex, SharedFormulaOptions)
-> Maybe (SharedFormulaIndex, SharedFormulaOptions)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (SharedFormulaIndex
si, (CellRef -> Formula -> SharedFormulaOptions
SharedFormulaOptions CellRef
ref Formula
formula)))
Text
unexpected -> Text
-> Either
Text
(FormulaExpression,
Maybe (SharedFormulaIndex, SharedFormulaOptions))
forall a b. a -> Either a b
Left (Text
-> Either
Text
(FormulaExpression,
Maybe (SharedFormulaIndex, SharedFormulaOptions)))
-> Text
-> Either
Text
(FormulaExpression,
Maybe (SharedFormulaIndex, SharedFormulaOptions))
forall a b. (a -> b) -> a -> b
$ Text
"Unexpected formula type" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Text -> String
forall a. Show a => a -> String
show Text
unexpected)
let f =
CellFormula
{ _cellfAssignsToName :: Bool
_cellfAssignsToName = Bool
bx
, _cellfCalculate :: Bool
_cellfCalculate = Bool
ca
, _cellfExpression :: FormulaExpression
_cellfExpression = FormulaExpression
expr
}
return $ FormulaData f shared