-- should this fail if not all the input was consumed?
--
-- in theory we
-- require the pathInfo to have the initial '/', but this code will
-- still work if it is missing.
--

-- If there are multiple //// at the beginning, we only drop the first
-- one, because we only added one in toPathInfo. Hence the others
-- should be significant.
--
-- However, if the pathInfo was prepend with http://example.org/ with
-- a trailing slash, then things might not line up.

-- | parse a 'String' into 'url' using 'PathInfo'.
--
-- returns @Left "parse error"@ on failure
--
-- returns @Right url@ on success

fromPathInfo :: (PathInfo url) => ByteString -> Either String url
fromPathInfo pi =
  parseSegments fromPathSegments (decodePathInfo $ dropSlash pi)
  where
    dropSlash s =
        if ((B.singleton '/') `B.isPrefixOf` s)
        then B.tail s
        else s

-- | turn a routing function into a 'Site' value using the 'PathInfo' class
fromPathInfo :: (PathInfo url) => ByteString -> Either String url
fromPathInfo pi =
  parseSegments fromPathSegments (decodePathInfo $ dropSlash pi)
  where
    dropSlash s =
        if ((B.singleton '/') `B.isPrefixOf` s)
        then B.tail s
        else s

-- | turn a routing function into a 'Site' value using the 'PathInfo' class
fromPathInfo pi =
  parseSegments fromPathSegments (decodePathInfo $ dropSlash pi)
  where
    dropSlash s =
        if ((B.singleton '/') `B.isPrefixOf` s)
        then B.tail s
        else s

-- | turn a routing function into a 'Site' value using the 'PathInfo' class