Skip to content

A peer-to-peer stack for code collaboration

ToRad lowecases haskell constructors.

James Henri Haydon2a86a2ae33ec
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 53347c5..d0ed0c6 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -4,6 +4,7 @@
-- | The core radicle datatypes and functionality.
module Radicle.Internal.Core where
+import qualified Prelude
import Protolude hiding (Constructor, TypeError, (<>))
import Codec.Serialise (Serialise)
@@ -99,7 +100,8 @@ errorDataToValue e = case e of
WrongNumberOfArgs i expected actual -> makeVal
( "wrong-number-of-args"
, [ ("function", makeA $ Ident i)
- , ("expected", Number $ fromIntegral expected)
+ , ("expected", Number $ fromIntegral
+ expected)
, ("actual", Number $ fromIntegral actual)]
)
NonHashableKey -> makeVal ( "non-hashable-key", [])
@@ -576,9 +578,9 @@ instance (CPA t, FromRad t a, FromRad t b) => FromRad t (a,b) where
fromRad (Vec (x :<| y :<| Seq.Empty)) = (,) <$> fromRad x <*> fromRad y
fromRad _ = Left "Expecting a vector of length 2"
instance (CPA t, FromRad t a) => FromRad t (Maybe a) where
- fromRad (Vec (Keyword (Ident "Just") :<| x :<| Empty)) = Just <$> fromRad x
- fromRad (Keyword (Ident "Nothing")) = pure Nothing
- fromRad _ = Left "Expecting :Nothing or [:Just _]"
+ fromRad (Vec (Keyword (Ident "just") :<| x :<| Empty)) = Just <$> fromRad x
+ fromRad (Keyword (Ident "nothing")) = pure Nothing
+ fromRad _ = Left "Expecting `:nothing` or `[:just _]`"
instance FromRad t (Annotated t ValueF) where
fromRad = pure
instance CPA t => FromRad t Rational where
@@ -642,8 +644,8 @@ instance CPA t => ToRad t Bool where
instance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) where
toRad (x,y) = Vec $ toRad x :<| toRad y :<| Empty
instance (CPA t, ToRad t a) => ToRad t (Maybe a) where
- toRad Nothing = Keyword (Ident "Nothing")
- toRad (Just x) = Vec $ Keyword (Ident "Just") :<| toRad x :<| Empty
+ toRad Nothing = Keyword (Ident "nothing")
+ toRad (Just x) = Vec $ Keyword (Ident "just") :<| toRad x :<| Empty
instance CPA t => ToRad t Int where
toRad = Number . fromIntegral
instance CPA t => ToRad t Integer where
@@ -755,12 +757,12 @@ instance (CPA t, ToRadFields t a, ToRadG t b) => ToRadG t (Either a b) where
toRadConss (_ : r) (Right next) = toRadConss r next
toRadConss [] _ = panic "impossible"
-radCons :: CPA t => Text -> [Annotated t ValueF] -> Annotated t ValueF
+radCons :: CPA t => Prelude.String -> [Annotated t ValueF] -> Annotated t ValueF
radCons name args = case args of
[] -> consKw
_ -> Vec ( consKw :<| Seq.fromList args )
where
- consKw = Keyword (Ident (Identifier.keywordWord name))
+ consKw = Keyword . Ident . Identifier.kebabCons $ name
instance ToRadG t Void where
toRadConss _ = absurd
@@ -792,7 +794,7 @@ gDecodeErr e = "Couldn't generically decode radicle value: " <> e
instance (FromRadFields t a, FromRadG t b) => FromRadG t (Either a b) where
fromRadConss (Constructor name fieldMeta : r) name' args = do
- if Identifier.keywordWord (toS name) /= name'
+ if Identifier.kebabCons (toS name) /= name'
then Right <$> fromRadConss r name' args
else Left <$> fromRadFields fieldMeta args
fromRadConss [] _ _ = panic "impossible"
diff --git a/src/Radicle/Internal/Identifier.hs b/src/Radicle/Internal/Identifier.hs
index e3b7713..aaca2d5 100644
--- a/src/Radicle/Internal/Identifier.hs
+++ b/src/Radicle/Internal/Identifier.hs
@@ -3,7 +3,8 @@ module Radicle.Internal.Identifier where
import qualified Prelude
import Protolude
-import Data.Char (isAlphaNum, isLetter)
+import Data.Char (isAlphaNum, isLetter, isUpper, toLower)
+import qualified Data.Map as Map
import qualified Data.Text as T
-- These are made top-level so construction of arbitrary instances that matches
@@ -24,23 +25,45 @@ extendedChar :: Prelude.String
extendedChar = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<' , '=', '>'
, '?', '@', '^', '_', '~']
--- | Expands characters into strings that are safe to use in keywords.
-keywordChar :: Char -> Text
-keywordChar x = case x of
- _ | isValidIdentRest x -> toS [x]
- '{' -> "_lbrace_"
- '}' -> "_rbrace_"
- '[' -> "_lbrack_"
- ']' -> "_rbrack_"
- '(' -> "_lparen_"
- ')' -> "_rparen_"
- '|' -> "_pipe_"
- ' ' -> "_space_"
- '\'' -> "_prime_"
- ',' -> "_comma_"
- '#' -> "_hash_"
- _ -> "_" <> show (fromEnum x) <> "_"
-
--- | Makes a string safe to use in a keyword.
-keywordWord :: Text -> Text
-keywordWord x = T.concat $ keywordChar <$> T.unpack x
+replacements :: Map Char Prelude.String
+replacements = Map.fromList
+ [ ('{', "_lbrace_")
+ , ('}', "_rbrace_")
+ , ('[', "_lbrack_")
+ , (']', "_rbrack_")
+ , ('(', "_lparen_")
+ , (')', "_rparen_")
+ , ('|', "_pipe_")
+ , (' ', "_space_")
+ , ('\'', "_prime_")
+ , (',', "_comma_")
+ , ('#', "_hash_")
+ ]
+
+-- | Converts a Haskell constructor name into a valid radicle identifier.
+kebabCons :: Prelude.String -> Text
+kebabCons = T.intercalate "-" . fmap (toS . lowerFirst) . go .keywordWord
+ where
+ go "" = []
+ go xs@(x:_) | isLower x = lowers : go rest
+ where
+ (lowers, rest) = Prelude.span isLower xs
+ isLower = not . isUpper
+ go (x:xs) = consCase x rest
+ where
+ rest = go xs
+ consCase c [] = [[c]]
+ consCase c (p:ps) = (c:p):ps
+
+ lowerFirst "" = ""
+ lowerFirst (c:cs) = toLower c : cs
+
+ -- Expands characters into strings that are safe to use in keywords.
+ keywordChar x | isValidIdentRest x = [x]
+ keywordChar x =
+ case Map.lookup x replacements of
+ Just r -> r
+ Nothing -> "_" <> show (fromEnum x) <> "_"
+
+ -- Makes a string safe to use in a keyword.
+ keywordWord = concat . fmap keywordChar