Utils.purs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. module LinkedIn.Profile.Utils where
  2. import Prelude
  3. import Control.Alt ((<|>))
  4. import Data.Either (Either(..))
  5. import Data.Maybe (Maybe(..))
  6. import LinkedIn.DetachedNode (DetachedNode(..))
  7. import LinkedIn.UIElements.Parser (uiStringP)
  8. import LinkedIn.UIElements.Types (UIElement(..))
  9. import Parsing (ParseError(..), initialPos, runParser)
  10. -- TODO : should certainly use another type than ParseError here
  11. toUIElement ∷ DetachedNode → Either ParseError UIElement
  12. toUIElement (DetachedElement {content}) = wrapInUiElement content
  13. toUIElement (DetachedComment str) = wrapInUiElement str
  14. toUIElement (DetachedText str) = wrapInUiElement str
  15. toUIElement (DetachedSvgElement {id, dataTestIcon, tag: "svg"}) = case id <|> dataTestIcon of
  16. Just i -> Right (UIIcon i)
  17. Nothing -> Left (ParseError "SVG element could not be identified" initialPos)
  18. toUIElement (DetachedSvgElement _) = Left (ParseError "SVG element could not be identified" initialPos)
  19. toUIElement (DetachedLiIcon i) = Right (UIIcon i)
  20. toUIElement (DetachedButton {content, role}) = map toButton $ runParser content uiStringP
  21. where toButton ui = UIButton role ui
  22. toUIElement (DetachedA {content, href}) = map toLink $ runParser content uiStringP
  23. where toLink ui = UILink href ui
  24. wrapInUiElement ∷ String → Either ParseError UIElement
  25. wrapInUiElement string = map UIElement $ runParser string uiStringP