{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Codec.Xlsx.Types.Internal.CfPair where
import GHC.Generics (Generic)
import Text.XML.Cursor
import Codec.Xlsx.Parser.Internal
import Codec.Xlsx.Types.Common
import Codec.Xlsx.Types.ConditionalFormatting
import Codec.Xlsx.Writer.Internal
newtype CfPair = CfPair
{ CfPair -> (SqRef, ConditionalFormatting)
unCfPair :: (SqRef, ConditionalFormatting)
} deriving (CfPair -> CfPair -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CfPair -> CfPair -> Bool
$c/= :: CfPair -> CfPair -> Bool
== :: CfPair -> CfPair -> Bool
$c== :: CfPair -> CfPair -> Bool
Eq, Int -> CfPair -> ShowS
[CfPair] -> ShowS
CfPair -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CfPair] -> ShowS
$cshowList :: [CfPair] -> ShowS
show :: CfPair -> String
$cshow :: CfPair -> String
showsPrec :: Int -> CfPair -> ShowS
$cshowsPrec :: Int -> CfPair -> ShowS
Show, forall x. Rep CfPair x -> CfPair
forall x. CfPair -> Rep CfPair x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CfPair x -> CfPair
$cfrom :: forall x. CfPair -> Rep CfPair x
Generic)
instance FromCursor CfPair where
fromCursor :: Cursor -> [CfPair]
fromCursor Cursor
cur = do
SqRef
sqref <- forall a. FromAttrVal a => Name -> Cursor -> [a]
fromAttribute Name
"sqref" Cursor
cur
let cfRules :: ConditionalFormatting
cfRules = Cursor
cur forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Name -> Axis
element (Text -> Name
n_ Text
"cfRule") forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. FromCursor a => Cursor -> [a]
fromCursor
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (SqRef, ConditionalFormatting) -> CfPair
CfPair (SqRef
sqref, ConditionalFormatting
cfRules)
instance FromXenoNode CfPair where
fromXenoNode :: Node -> Either Text CfPair
fromXenoNode Node
root = do
SqRef
sqref <- forall a. Node -> AttrParser a -> Either Text a
parseAttributes Node
root forall a b. (a -> b) -> a -> b
$ forall a. FromAttrBs a => ByteString -> AttrParser a
fromAttr ByteString
"sqref"
ConditionalFormatting
cfRules <- forall a. Node -> ChildCollector a -> Either Text a
collectChildren Node
root forall a b. (a -> b) -> a -> b
$ forall a. FromXenoNode a => ByteString -> ChildCollector [a]
fromChildList ByteString
"cfRule"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (SqRef, ConditionalFormatting) -> CfPair
CfPair (SqRef
sqref, ConditionalFormatting
cfRules)
instance ToElement CfPair where
toElement :: Name -> CfPair -> Element
toElement Name
nm (CfPair (SqRef
sqRef, ConditionalFormatting
cfRules)) =
Name -> [(Name, Text)] -> [Element] -> Element
elementList Name
nm [ Name
"sqref" forall a. ToAttrVal a => Name -> a -> (Name, Text)
.= forall a. ToAttrVal a => a -> Text
toAttrVal SqRef
sqRef ]
(forall a b. (a -> b) -> [a] -> [b]
map (forall a. ToElement a => Name -> a -> Element
toElement Name
"cfRule") ConditionalFormatting
cfRules)