module Data.ProtoLens.Compiler.ModuleName
    ( protoModuleName ) where

import Data.Char (toUpper)
import Data.List (intercalate)
import System.FilePath

-- | Get the Haskell module name corresponding to a given .proto file.
protoModuleName :: FilePath -> String
protoModuleName :: FilePath -> FilePath
protoModuleName FilePath
path = FilePath -> FilePath
fixModuleName FilePath
rawModuleName
  where
    fixModuleName :: FilePath -> FilePath
fixModuleName FilePath
"" = FilePath
""
    -- Characters allowed in Bazel filenames but not in module names:
    fixModuleName (Char
'.':Char
c:FilePath
cs) = Char
'.' Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: Char -> Char
toUpper Char
c Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: FilePath -> FilePath
fixModuleName FilePath
cs
    fixModuleName (Char
'_':Char
c:FilePath
cs) = Char -> Char
toUpper Char
c Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: FilePath -> FilePath
fixModuleName FilePath
cs
    fixModuleName (Char
'-':Char
c:FilePath
cs) = Char -> Char
toUpper Char
c Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: FilePath -> FilePath
fixModuleName FilePath
cs
    fixModuleName (Char
c:FilePath
cs) = Char
c Char -> FilePath -> FilePath
forall a. a -> [a] -> [a]
: FilePath -> FilePath
fixModuleName FilePath
cs
    rawModuleName :: FilePath
rawModuleName = FilePath -> [FilePath] -> FilePath
forall a. [a] -> [[a]] -> [a]
intercalate FilePath
"."
                        ([FilePath] -> FilePath)
-> (FilePath -> [FilePath]) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FilePath
prefix FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
:)
                        ([FilePath] -> [FilePath])
-> (FilePath -> [FilePath]) -> FilePath -> [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
splitDirectories (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
dropExtension
                        (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
path

prefix :: String
prefix :: FilePath
prefix = FilePath
"Proto"