Skip to content

A peer-to-peer stack for code collaboration

Add CBOR instances for Annotation.

Julian Arniaa99c6e15763
diff --git a/src/Radicle/Internal/Annotation.hs b/src/Radicle/Internal/Annotation.hs
index f834926..38a73eb 100644
--- a/src/Radicle/Internal/Annotation.hs
+++ b/src/Radicle/Internal/Annotation.hs
@@ -8,6 +8,8 @@ import Data.Text (pack)
import Protolude
import qualified Text.Megaparsec.Pos as Par
+import Radicle.Internal.Orphans ()
+
newtype Annotated t f = Annotated (t (f (Annotated t f)))
deriving (Generic)
@@ -49,6 +51,8 @@ data SrcPos = SrcPos Par.SourcePos
| InternalPos Text
deriving (Eq, Ord, Read, Show, Generic)
+instance Serialise SrcPos
+
thisPos :: HasCallStack => SrcPos
thisPos = InternalPos (pack (prettyCallStack callStack))
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index f83a1bc..ee5f303 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -36,6 +36,8 @@ import Radicle.Internal.Orphans ()
data LangError r = LangError [Ann.SrcPos] (LangErrorData r)
deriving (Eq, Show, Read, Generic, Functor)
+instance Serialise r => Serialise (LangError r)
+
-- | An error throw during parsing or evaluating expressions in the language.
data LangErrorData r =
UnknownIdentifier Ident
@@ -50,6 +52,8 @@ data LangErrorData r =
| Exit
deriving (Eq, Show, Read, Generic, Functor)
+instance Serialise r => Serialise (LangErrorData r)
+
throwErrorHere :: (MonadError (LangError Value) m, HasCallStack) => LangErrorData Value -> m a
throwErrorHere = withFrozenCallStack (throwError . LangError [Ann.thisPos])
diff --git a/src/Radicle/Internal/Orphans.hs b/src/Radicle/Internal/Orphans.hs
index 48bf56e..e64cabb 100644
--- a/src/Radicle/Internal/Orphans.hs
+++ b/src/Radicle/Internal/Orphans.hs
@@ -2,10 +2,38 @@
module Radicle.Internal.Orphans () where
import Codec.Serialise (Serialise(..))
+import Codec.Serialise.Decoding (Decoder)
import Data.Scientific
import Prelude (fail)
import Protolude
+import Text.Megaparsec
+ ( ErrorFancy
+ , ErrorItem
+ , ParseError(..)
+ , Pos
+ , SourcePos
+ , mkPos
+ , unPos
+ )
instance Serialise Scientific where
encode = encode . (show :: Scientific -> Text)
decode = decode >>= either fail pure . readEither
+
+instance (Serialise a) => Serialise (ErrorItem a)
+instance (Serialise a) => Serialise (ErrorFancy a)
+instance Serialise SourcePos
+instance Serialise Pos where
+ encode = encode . unPos
+ decode = mkPos <$> decode
+
+instance {-# OVERLAPPABLE #-} (Serialise a, Ord a, Serialise b, Ord b) => Serialise (ParseError a b)
+instance {-# OVERLAPPING #-} (Serialise a, Ord a) => Serialise (ParseError a Void) where
+ encode = encode . unvoidParseError
+ where
+ unvoidParseError x = case x of
+ FancyError{} -> panic "impossible"
+ _ -> x
+ decode = (decode :: Decoder s (ParseError a ())) >>= \case
+ TrivialError a b c -> pure $ TrivialError a b c
+ _ -> fail "Expecting Trivial Error"