packages feed

css-parser-0.0.2 / src / CssParser / Show.hs

{-# LANGUAGE RequiredTypeArguments #-}{-# LANGUAGE UndecidableInstances #-}module CssParser.Show  ( module X  , CssShow (..)  , ShowSpaceBetween (..)  , ShowParenthesis (..)  , Embraced (..)  , Encurled (..)  , CslNe (..)  , SslNe (..)  , Csl (..)  , mayCss  , mkDecodingMap  , mkDecodingMap'  , smartLookup  , toCssStr  ) whereimport CssParser.Preludeimport CssParser.TextMarshal as Ximport Data.HashMap.Strict qualified as HMimport Data.Text qualified as Tclass ShowSpaceBetween (a :: Type) (b :: Type) where  cssSpace :: forall aa -> forall bb -> (aa ~ a, bb ~ b) => LTextclass CssShow a where  toCssText :: a -> LTextinstance (ShowSpaceBetween a a, CssShow a) => CssShow [a] where  toCssText = intercalate (cssSpace a a) . fmap toCssText{- HLINT ignore "Use concatMap" -}instance (ShowSpaceBetween a a, CssShow a) => CssShow (NonEmpty a) where  toCssText = toCssText . toListinstance CssShow () where  toCssText () = " 0 ";instance CssShow a => CssShow (Maybe a) where  toCssText = maybe "" ((<> " ") . toCssText)instance (CssShow a, CssShow b) => CssShow (Either a b) where  toCssText = \case    Left e -> toCssText e    Right v -> toCssText vinstance (CssShow at, CssShow bt, ShowSpaceBetween at bt) => CssShow (These at bt) where  toCssText = \case    This a -> toCssText a    That b -> toCssText b    These a b -> toCssText a <> cssSpace at bt <> toCssText bclass ShowParenthesis (parent :: Type) (this :: Type) where  left :: forall pp -> forall tt -> (pp ~ parent, tt ~ this) => LText  right :: forall pp -> forall tt -> (pp ~ parent, tt ~ this) => LTextnewtype Csl a = Csl [a] deriving (Show, Eq, Ord, Generic)instance CssShow a => CssShow (Csl a) where  toCssText (Csl l) =  intercalate ", " $ fmap toCssText lnewtype CslNe a = CslNe (NonEmpty a) deriving (Show, Eq, Ord, Generic)instance CssShow a => CssShow (CslNe a) where  toCssText (CslNe l) = toCssText . Csl $ toList lnewtype SslNe a = SslNe (NonEmpty a) deriving (Show, Eq, Ord, Generic)instance CssShow a => CssShow (SslNe a) where  toCssText (SslNe l) =  unwords .  fmap toCssText $ toList lnewtype Embraced a = Embraced a deriving (Show, Eq, Ord, Generic)instance CssShow a => CssShow (Embraced a) where  toCssText (Embraced a) = "("  <> toCssText a <> ")"newtype Encurled a = Encurled a deriving (Show, Eq, Ord, Generic)instance CssShow a => CssShow (Encurled a) where  toCssText (Encurled a) = "{"  <> toCssText a <> "}"instance CssShow Integer where  toCssText = numToTextinstance CssShow Bool where  toCssText = \case    False -> "false"    True -> "true"mayCss :: CssShow a => (LText -> LText) -> Maybe a -> LTextmayCss f = maybe "" (f . toCssText)mkDecodingMap :: forall a. (Enum a, Bounded a, CssShow a) => HM.HashMap Text amkDecodingMap = mkDecodingMap' $ enumFromTo minBound maxBoundmkDecodingMap' :: (CssShow a) => [a] -> HM.HashMap Text amkDecodingMap' kds = HM.fromList (zip origKeys kds <> zip lowKeys kds)  where    origKeys = toStrict . toCssText <$> kds    lowKeys = T.toLower <$> origKeyssmartLookup :: Text -> HM.HashMap Text a -> Maybe asmartLookup k m =  case HM.lookup k m of    Nothing -> HM.lookup (T.toLower k) m    o -> otoCssStr :: CssShow a => a -> StringtoCssStr = unpack . toCssText