module Web.Respond.Types.Path where
import qualified Data.Text as T
import Control.Monad.Trans.State
import qualified Data.Sequence as S
import Data.Foldable (toList)
import Data.Monoid ((<>))
import Control.Lens (makeLenses, snoc, (%=), uses)
import Safe (headMay, tailSafe)
data PathConsumer = PathConsumer {
_pcConsumed :: S.Seq T.Text,
_pcUnconsumed :: [T.Text]
} deriving (Eq, Show)
makeLenses ''PathConsumer
mkPathConsumer :: [T.Text] -> PathConsumer
mkPathConsumer = PathConsumer S.empty
pcGetNext :: PathConsumer -> Maybe T.Text
pcGetNext = headMay . _pcUnconsumed
pcConsumeNext :: PathConsumer -> PathConsumer
pcConsumeNext = execState $ do
next <- uses pcUnconsumed headMay
pcConsumed %= maybe id (flip snoc) next
pcUnconsumed %= tailSafe
getFullPath :: PathConsumer -> [T.Text]
getFullPath pc = toList (_pcConsumed pc) <> _pcUnconsumed pc