{-# LANGUAGE OverloadedStrings #-}
module NetSpider.Found
( FoundNode(..),
FoundLink(..),
LinkState(..),
linkStateToText,
linkStateFromText
) where
import Data.Bifunctor (Bifunctor(..))
import Data.Greskell (FromGraphSON(..))
import Data.Text (Text, unpack)
import NetSpider.Timestamp (Timestamp)
data LinkState =
LinkUnused
| LinkToTarget
| LinkToSubject
| LinkBidirectional
deriving (Show,Eq,Ord,Bounded,Enum)
linkStateToText :: LinkState -> Text
linkStateToText ls = case ls of
LinkUnused -> "unused"
LinkToTarget -> "to_target"
LinkToSubject -> "to_subject"
LinkBidirectional -> "bidirectional"
linkStateFromText :: Text -> Maybe LinkState
linkStateFromText t = case t of
"unused" -> Just LinkUnused
"to_target" -> Just LinkToTarget
"to_subject" -> Just LinkToSubject
"bidirectional" -> Just LinkBidirectional
_ -> Nothing
instance FromGraphSON LinkState where
parseGraphSON gv = fromText =<< parseGraphSON gv
where
fromText t = case linkStateFromText t of
Just ls -> return ls
Nothing -> fail ("Unrecognized LinkState: " ++ unpack t)
data FoundLink n la =
FoundLink
{ targetNode :: n,
linkState :: LinkState,
linkAttributes :: la
}
deriving (Show,Eq,Ord)
instance Functor (FoundLink n) where
fmap f l = l { linkAttributes = f $ linkAttributes l }
instance Bifunctor FoundLink where
bimap fn fla l = l { targetNode = fn $ targetNode l,
linkAttributes = fla $ linkAttributes l
}
data FoundNode n na la =
FoundNode
{ subjectNode :: n,
foundAt :: Timestamp,
neighborLinks :: [FoundLink n la],
nodeAttributes :: na
}
deriving (Show,Eq)
instance Functor (FoundNode n na) where
fmap f n = n { neighborLinks = (fmap . fmap) f $ neighborLinks n }
instance Bifunctor (FoundNode n) where
bimap fna fla n = n { neighborLinks = (fmap . fmap) fla $ neighborLinks n,
nodeAttributes = fna $ nodeAttributes n
}