A peer-to-peer stack for code collaboration
Simplify definition of From/ToRad classes
Thomas Scholtesb938e7c2e64f
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex fa48d87..bed4c1a 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -786,56 +786,56 @@ createModule = \casetype CPA t = (Ann.Annotation t, Copointed t)class FromRad t a where- fromRad :: Annotated t ValueF -> Either Text a+ fromRad :: (CPA t) => Annotated t ValueF -> Either Text adefault fromRad :: (CPA t, HasEot a, FromRadG t (Eot a)) => Annotated t ValueF -> Either Text afromRad = fromRadG-instance CPA t => FromRad t () where+instance FromRad t () wherefromRad (Vec Seq.Empty) = pure ()fromRad _ = Left "Expecting an empty vector"-instance (CPA t, FromRad t a, FromRad t b) => FromRad t (a,b) where+instance (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+instance (FromRad t a) => FromRad t (Maybe a) wherefromRad (Vec (Keyword (Ident "just") :<| x :<| Empty)) = Just <$> fromRad xfromRad (Keyword (Ident "nothing")) = pure NothingfromRad _ = Left "Expecting `:nothing` or `[:just _]`"instance FromRad t (Annotated t ValueF) wherefromRad = pure-instance CPA t => FromRad t Rational where+instance FromRad t Rational wherefromRad (Number n) = pure nfromRad _ = Left "Not a number"-instance CPA t => FromRad t Scientific where+instance FromRad t Scientific wherefromRad = fromRad >=> Num.isSci-instance CPA t => FromRad t Int where+instance FromRad t Int wherefromRad = fromRad >=> Num.isInt-instance CPA t => FromRad t Integer where+instance FromRad t Integer wherefromRad = fromRad >=> Num.isInteger-instance CPA t => FromRad t Text where+instance FromRad t Text wherefromRad x = case x ofString n -> pure n_ -> Left "Expecting string"-instance {-# OVERLAPPING #-} CPA t => FromRad t [Char] where+instance {-# OVERLAPPING #-} FromRad t [Char] wherefromRad x = case x ofString n -> pure $ toS n_ -> Left "Expecting string"-instance CPA t => FromRad t ExitCode where+instance FromRad t ExitCode wherefromRad x = case x ofKeyword (Ident "ok") -> pure $ ExitSuccessVec (Keyword (Ident "error") Seq.:<| errValue Seq.:<| Seq.Empty) -> ExitFailure <$> fromRad errValue_ -> Left "Expecting either :ok or [:error errValue]"-instance (CPA t, FromRad t a) => FromRad t [a] where+instance (FromRad t a) => FromRad t [a] wherefromRad x = case x ofList xs -> traverse fromRad xsVec xs -> traverse fromRad (toList xs)_ -> Left "Expecting list"-instance CPA t => FromRad t (Doc.Docd (Annotated t ValueF)) where+instance FromRad t (Doc.Docd (Annotated t ValueF)) wherefromRad (Dict d) = doval <- kwLookup "val" d ?? "Expecting `:val` key"doc <- traverse fromRad (kwLookup "doc" d)pure (Doc.Docd doc val)fromRad _ = Left "Expecting a dict."-instance CPA t => FromRad t CmdSpec where+instance FromRad t CmdSpec wherefromRad x = case x ofVec (Keyword (Ident "shell") Seq.:<| arg Seq.:<| Seq.Empty) ->ShellCommand <$> fromRad arg@@ -847,13 +847,13 @@ instance CPA t => FromRad t CmdSpec wherethrowError $ "Expecting either :raw or :shell, got: " <> s_ ->throwError "Expecting vector"-instance CPA t => FromRad t StdStream where+instance FromRad t StdStream wherefromRad x = case x ofKeyword (Ident "inherit") -> pure InheritKeyword (Ident "create-pipe") -> pure CreatePipeKeyword (Ident "no-stream") -> pure NoStream_ -> throwError $ "Expecting :inherit, :create-pipe, or :no-stream"-instance CPA t => FromRad t CreateProcess where+instance FromRad t CreateProcess wherefromRad x = case x ofDict d -> docmdspec' <- fromRad =<< (kwLookup "cmdspec" d ?? "Expecting 'cmdspec' key")@@ -881,55 +881,54 @@ instance CPA t => FromRad t CreateProcess whereclass ToRad t a where- toRad :: a -> Annotated t ValueF+ toRad :: CPA t => a -> Annotated t ValueFdefault toRad :: (HasEot a, ToRadG t (Eot a)) => a -> Annotated t ValueF- toRad =- toRadG+ toRad = toRadG-instance CPA t => ToRad t () where+instance ToRad t () wheretoRad _ = Vec Empty-instance CPA t => ToRad t Bool where+instance ToRad t Bool wheretoRad = Boolean-instance (CPA t, ToRad t a, ToRad t b) => ToRad t (a,b) where+instance (ToRad t a, ToRad t b) => ToRad t (a,b) wheretoRad (a,b) = Vec $ toRad a :<| toRad b :<| Empty-instance (CPA t, ToRad t a, ToRad t b, ToRad t c) => ToRad t (a,b,c) where+instance (ToRad t a, ToRad t b, ToRad t c) => ToRad t (a,b,c) wheretoRad (a,b,c) = Vec $ toRad a :<| toRad b :<| toRad c :<| Empty-instance (CPA t, ToRad t a, ToRad t b, ToRad t c, ToRad t d) => ToRad t (a,b,c,d) where+instance (ToRad t a, ToRad t b, ToRad t c, ToRad t d) => ToRad t (a,b,c,d) wheretoRad (a,b,c,d) = Vec $ toRad a :<| toRad b :<| toRad c :<| toRad d :<| Empty-instance (CPA t, ToRad t a) => ToRad t (Maybe a) where+instance (ToRad t a) => ToRad t (Maybe a) wheretoRad Nothing = Keyword (Ident "nothing")toRad (Just x) = Vec $ Keyword (Ident "just") :<| toRad x :<| Empty-instance CPA t => ToRad t Int where+instance ToRad t Int wheretoRad = Number . fromIntegral-instance CPA t => ToRad t Integer where+instance ToRad t Integer wheretoRad = Number . fromIntegral-instance CPA t => ToRad t Scientific where+instance ToRad t Scientific wheretoRad = Number . toRational-instance CPA t => ToRad t Text where+instance ToRad t Text wheretoRad = String-instance {-# OVERLAPPING #-} CPA t => ToRad t [Char] where+instance {-# OVERLAPPING #-} ToRad t [Char] wheretoRad = String . toS-instance CPA t => ToRad t ExitCode where+instance ToRad t ExitCode wheretoRad x = case x ofExitSuccess -> Keyword (Ident "ok")ExitFailure c -> Vec $ Seq.fromList [Keyword (Ident "error"), toRad c]instance ToRad t (Ann.Annotated t ValueF) wheretoRad = identity-instance (CPA t, ToRad t a) => ToRad t [a] where+instance (ToRad t a) => ToRad t [a] wheretoRad xs = Vec . Seq.fromList $ toRad <$> xs-instance (CPA t, ToRad t a) => ToRad t (Map.Map Text a) where+instance (ToRad t a) => ToRad t (Map.Map Text a) wheretoRad xs = Dict $ Map.mapKeys String $ toRad <$> xs-instance CPA t => ToRad t (Doc.Docd (Annotated t ValueF)) where+instance ToRad t (Doc.Docd (Annotated t ValueF)) wheretoRad (Doc.Docd d_ v) = Dict $ Map.fromList $ ( Keyword (Ident "val"), v) : case d_ ofJust d -> [ (Keyword (Ident "doc"), toRad d) ]Nothing -> []-instance (CPA t) => ToRad t StdStream where+instance ToRad t StdStream wheretoRad x = case x ofInherit -> Keyword $ Ident "inherit"CreatePipe -> Keyword $ Ident "create-pipe"NoStream -> Keyword $ Ident "no-stream"_ -> panic "Cannot convert handle"-instance (CPA t) => ToRad t CmdSpec where+instance ToRad t CmdSpec wheretoRad x = case x ofShellCommand comm ->let c = toRad comm@@ -1045,7 +1044,7 @@ instance ToRadG t Void wheretoRadConss _ = absurdclass ToRadFields t a where- toRadFields :: a -> [Annotated t ValueF]+ toRadFields :: CPA t => a -> [Annotated t ValueF]instance (ToRad t a, ToRadFields t as) => ToRadFields t (a, as) wheretoRadFields (x, xs) = toRad x : toRadFields xsdiff --git a/test/spec/Radicle/Internal/Foo.hs b/test/spec/Radicle/Internal/Foo.hsindex f887003..e801ef1 100644--- a/test/spec/Radicle/Internal/Foo.hs+++ b/test/spec/Radicle/Internal/Foo.hs@@ -11,7 +11,6 @@ import Test.QuickCheckimport Test.QuickCheck.Instances ()import Radicle-import Radicle.Internal.Coredata Foo= FooA { a1 :: Text, a2 :: Scientific }@@ -20,8 +19,8 @@ data Foo| FooDderiving (Eq, Show, Generic)-instance CPA t => FromRad t Foo-instance CPA t => ToRad t Foo+instance FromRad t Foo+instance ToRad t Fooinstance Arbitrary Foo wherearbitrary = oneof[ FooA <$> arbitrary <*> arbitrary