Skip to content

Commit

Permalink
Adjust case of first letter for namespace (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisCardwell committed Oct 11, 2024
1 parent 1c78748 commit c946fe3
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions hs-bindgen/src/HsBindgen/Hs/AST/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module HsBindgen.Hs.AST.Name (
, toHsName
) where

import Data.Char qualified as Char
import Data.String
import Data.Text (Text)
import Data.Text qualified as T

import HsBindgen.C.AST (CName(..))

Expand Down Expand Up @@ -39,9 +41,38 @@ newtype HsName (ns :: Namespace) = HsName { getHsName :: Text }
class ToHsName (ns :: Namespace) where
toHsName :: CName -> HsName ns

-- | (Wrong) catch-all instance
-- TODO <https://github.com/well-typed/hs-bindgen/issues/189>
--
-- TODO: <https://github.com/well-typed/hs-bindgen/issues/189>
-- We need to properly implement this.
instance ToHsName ns where
toHsName = HsName . getCName
-- The following instances just adjust the case of the first letter of the name
-- as required, according to the target namespace. We will add a context and
-- options as well as consider edge cases in future commits.

instance ToHsName NsVar where
toHsName = toHsName' Char.toLower

instance ToHsName NsConstr where
toHsName = toHsName' Char.toUpper

instance ToHsName NsTypeVar where
toHsName = toHsName' Char.toLower

instance ToHsName NsTypeConstr where
toHsName = toHsName' Char.toUpper

instance ToHsName NsTypeClass where
toHsName = toHsName' Char.toUpper

instance ToHsName NsModuleName where
toHsName = toHsName' Char.toUpper

toHsName' ::
(Char -> Char) -- ^ case conversion for first character
-> CName
-> HsName ns
toHsName' f = HsName . aux . getCName
where
aux :: Text -> Text
aux t = case T.uncons t of
Just (c, t')
| Char.isLetter c -> T.cons (f c) t'
_otherwise -> t

0 comments on commit c946fe3

Please sign in to comment.