A peer-to-peer stack for code collaboration
Added some From/ToRad instances and primfns. (#156)
James Haydon56d9d81dae24
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 1343f1b..653b4ae 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -19,7 +19,8 @@ import qualified Data.IntMap as IntMapimport Data.List.NonEmpty (NonEmpty)import qualified Data.List.NonEmpty as NonEmptyimport qualified Data.Map as Map-import Data.Scientific (Scientific, floatingOrInteger)+import Data.Scientific+ (Scientific, floatingOrInteger, toBoundedInteger)import Data.Semigroup ((<>))import Data.Sequence (Seq(..))import qualified Data.Sequence as Seq@@ -494,12 +495,22 @@ instance CPA t => FromRad t () whereinstance (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 _]"instance FromRad Ann.WithPos Value wherefromRad = pureinstance CPA t => FromRad t Scientific wherefromRad x = case x ofNumber n -> pure n_ -> Left "Expecting number"+instance CPA t => FromRad t Int where+ fromRad = \case+ Number s -> case toBoundedInteger s of+ Just i -> pure i+ Nothing -> Left "Number was not a bounded int"+ _ -> Left "Expecting number"instance CPA t => FromRad t Integer wherefromRad = \caseNumber s -> case floatingOrInteger s of@@ -544,8 +555,13 @@ class ToRad t a whereinstance CPA t => ToRad t () wheretoRad _ = Vec Empty+instance CPA t => ToRad t Bool where+ toRad = Booleaninstance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) wheretoRad (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 :<| Emptyinstance CPA t => ToRad t Int wheretoRad = Number . fromIntegralinstance CPA t => ToRad t Integer where@@ -555,7 +571,7 @@ instance CPA t => ToRad t Scientific whereinstance CPA t => ToRad t Text wheretoRad = Stringinstance (CPA t, ToRad t a) => ToRad t [a] where- toRad xs = List $ toRad <$> xs+ toRad xs = Vec . Seq.fromList $ toRad <$> xsinstance (CPA t, ToRad t a) => ToRad t (Map.Map Text a) wheretoRad xs = Dict $ Map.mapKeys String $ toRad <$> xsinstance ToRad Ann.WithPos (Env Value) wherediff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex 3202e29..300442b 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -236,6 +236,11 @@ purePrimFns = PrimFns $ fromList $ first Ident <$>Dict kvs -> pure $ List [List [k, v] | (k,v) <- Map.toList kvs ]_ -> throwErrorHere $ TypeError "seq: can only be used on a list, vector or dictionary")+ , ( "from-just"+ , oneArg "from-just" $ \case+ Vec (Keyword (Ident "Just") Seq.:<| x Seq.:<| Seq.Empty) -> pure x+ _ -> throwErrorHere $ TypeError "from-just: called on a non-just"+ ), ( "to-json", oneArg "to-json" $ \v -> String . toS . Aeson.encode <$>maybeJson v ?? toLangError (OtherError "Could not serialise value to JSON")diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 563edbe..fe15c3e 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -673,6 +673,8 @@ test_from_to_radicle =[ testForType (Proxy :: Proxy Scientific) ], testGroup "Text"[ testForType (Proxy :: Proxy Text) ]+ , testGroup "Maybe Text"+ [ testForType (Proxy :: Proxy (Maybe Text))], testGroup "[Text]"[ testForType (Proxy :: Proxy [Text]) ], testGroup "Generic a => a"