{- This file is part of settings.
 -
 - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

module Data.Settings.Route
    ( parseRoute
    , parseRoute'
    , showRoute
    , showRoute'
    )
where

import Data.Settings.Types

import qualified Data.Text as T

-- | Split a path string into its components, if it's a valid path
-- syntactically.
parseRoute :: OptPath -> Maybe OptRoute
parseRoute = parseRoute' '\\' '.'

-- | Like 'parseRoute', but allows to choose the escape character (e.g. @'\'@)
-- and the path separator character (e.g. @'.'@).
parseRoute' :: Char -> Char -> OptPath -> Maybe OptRoute
parseRoute' esc sep = f [] T.empty
    where
    {-f []    ""   "" = Just []
    f route ""   "" = Nothing
    f route part "" = Just $ reverse $ part : route
    f route part [c]
        | c == esc             = Nothing
        | c == sep             = Nothing
        | otherwise            = f route (part `T.snoc` c) ""
    f route part (a:r@(b:s))
        | a == esc && b == esc = f route (part `T.snoc` esc) s
        | a == esc && b == sep = f route (part `T.snoc` sep) s
        | a == esc             = Nothing
        | a == sep             = f (part : route) "" r
        | otherwise            = f route (part `T.snoc` a) r
    -}
    f route part path =
        case T.uncons path of
            Nothing ->
                if T.null part
                    then if null route then Just [] else Nothing
                    else Just $ reverse $ part : route
            Just (a, r) ->
                case T.uncons r of
                    Nothing
                        | a == esc  -> Nothing
                        | a == sep  -> Nothing
                        | otherwise -> f route (part `T.snoc` a) T.empty
                    Just (b, s)
                        | a == esc && b == esc -> f route (part `T.snoc` esc) s
                        | a == esc && b == sep -> f route (part `T.snoc` sep) s
                        | a == esc             -> Nothing
                        | a == sep             -> f (part : route) T.empty r
                        | otherwise            -> f route (part `T.snoc` a) r

-- | Create a string representation of a path, with the parts separated by
-- periods, and literal periods escaped using backslashes.
showRoute :: OptRoute -> OptPath
showRoute = showRoute' '\\' '.'

-- | Like 'showRoute', but allows to choose the escape character (e.g. @'\\'@)
-- and the path separator character (e.g. @'.'@).
showRoute' :: Char -> Char -> OptRoute -> OptPath
showRoute' esc sep = T.intercalate (T.singleton sep) . map escape
    where
    escape = T.foldl f T.empty
    f s c
        | c == esc  = s `T.snoc` esc `T.snoc` esc
        | c == sep  = s `T.snoc` esc `T.snoc` sep
        | otherwise = s `T.snoc` c