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.hsindex 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 Preludeimport Protolude hiding (Constructor, TypeError, (<>))import Codec.Serialise (Serialise)@@ -99,7 +100,8 @@ errorDataToValue e = case e ofWrongNumberOfArgs 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) wherefromRad (Vec (x :<| y :<| Seq.Empty)) = (,) <$> fromRad x <*> fromRad yfromRad _ = 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) wherefromRad = pureinstance CPA t => FromRad t Rational where@@ -642,8 +644,8 @@ instance CPA t => ToRad t Bool whereinstance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) wheretoRad (x,y) = Vec $ toRad x :<| toRad y :<| Emptyinstance (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 :<| Emptyinstance CPA t => ToRad t Int wheretoRad = Number . fromIntegralinstance CPA t => ToRad t Integer where@@ -755,12 +757,12 @@ instance (CPA t, ToRadFields t a, ToRadG t b) => ToRadG t (Either a b) wheretoRadConss (_ : r) (Right next) = toRadConss r nexttoRadConss [] _ = panic "impossible"-radCons :: CPA t => Text -> [Annotated t ValueF] -> Annotated t ValueF+radCons :: CPA t => Prelude.String -> [Annotated t ValueF] -> Annotated t ValueFradCons name args = case args of[] -> consKw_ -> Vec ( consKw :<| Seq.fromList args )where- consKw = Keyword (Ident (Identifier.keywordWord name))+ consKw = Keyword . Ident . Identifier.kebabCons $ nameinstance ToRadG t Void wheretoRadConss _ = absurd@@ -792,7 +794,7 @@ gDecodeErr e = "Couldn't generically decode radicle value: " <> einstance (FromRadFields t a, FromRadG t b) => FromRadG t (Either a b) wherefromRadConss (Constructor name fieldMeta : r) name' args = do- if Identifier.keywordWord (toS name) /= name'+ if Identifier.kebabCons (toS name) /= name'then Right <$> fromRadConss r name' argselse Left <$> fromRadFields fieldMeta argsfromRadConss [] _ _ = panic "impossible"diff --git a/src/Radicle/Internal/Identifier.hs b/src/Radicle/Internal/Identifier.hsindex e3b7713..aaca2d5 100644--- a/src/Radicle/Internal/Identifier.hs+++ b/src/Radicle/Internal/Identifier.hs@@ -3,7 +3,8 @@ module Radicle.Internal.Identifier whereimport qualified Preludeimport Protolude-import Data.Char (isAlphaNum, isLetter)+import Data.Char (isAlphaNum, isLetter, isUpper, toLower)+import qualified Data.Map as Mapimport qualified Data.Text as T-- These are made top-level so construction of arbitrary instances that matches@@ -24,23 +25,45 @@ extendedChar :: Prelude.StringextendedChar = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<' , '=', '>', '?', '@', '^', '_', '~']--- | 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