forked from IntersectMBO/plutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CIP-57: Annotations (IntersectMBO#5820)
- Loading branch information
Showing
9 changed files
with
187 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NoStrict #-} | ||
|
||
module PlutusTx.Blueprint.Schema.Annotation ( | ||
SchemaInfo (..), | ||
emptySchemaInfo, | ||
annotationsToSchemaInfo, | ||
SchemaAnn (..), | ||
SchemaTitle (..), | ||
SchemaDescription (..), | ||
SchemaComment (..), | ||
) where | ||
|
||
import Control.Monad.State (execStateT, get, lift, put) | ||
import Data.Aeson (ToJSON (..)) | ||
import Data.Data (Data, Typeable) | ||
import GHC.Generics (Generic) | ||
import Language.Haskell.TH.Syntax (Lift) | ||
import Prelude hiding (max, maximum, min, minimum) | ||
|
||
-- | Additional information optionally attached to any datatype schema definition. | ||
data SchemaInfo = MkSchemaInfo | ||
{ title :: Maybe String | ||
, description :: Maybe String | ||
, comment :: Maybe String | ||
} | ||
deriving stock (Eq, Show, Generic, Data, Lift) | ||
|
||
emptySchemaInfo :: SchemaInfo | ||
emptySchemaInfo = MkSchemaInfo Nothing Nothing Nothing | ||
|
||
type SchemaInfoError = String | ||
|
||
annotationsToSchemaInfo :: [SchemaAnn] -> Either SchemaInfoError SchemaInfo | ||
annotationsToSchemaInfo = | ||
(`execStateT` emptySchemaInfo) . traverse \case | ||
MkSchemaAnnTitle (SchemaTitle t) -> | ||
get >>= \info -> case title info of | ||
Nothing -> put $ info{title = Just t} | ||
Just t' -> failOverride "SchemaTitle" t' t | ||
MkSchemaAnnDescription (SchemaDescription d) -> | ||
get >>= \info -> case description info of | ||
Nothing -> put $ info{description = Just d} | ||
Just d' -> failOverride "SchemaDescription" d' d | ||
MkSchemaAnnComment (SchemaComment c) -> | ||
get >>= \info -> case comment info of | ||
Nothing -> put $ info{comment = Just c} | ||
Just c' -> failOverride "SchemaComment" c' c | ||
where | ||
failOverride label old new = | ||
lift . Left $ concat [label, " annotation error: ", show old, " is overridden with ", show new] | ||
|
||
-- | Annotation that can be attached to a schema definition. | ||
data SchemaAnn | ||
= MkSchemaAnnTitle SchemaTitle | ||
| MkSchemaAnnDescription SchemaDescription | ||
| MkSchemaAnnComment SchemaComment | ||
deriving stock (Eq, Ord, Show, Generic, Typeable, Data, Lift) | ||
|
||
{- | An annotation for the "title" schema attribute. | ||
This annotation could be attached to a type or constructor: | ||
@ | ||
{\-# ANN type MyFoo (SchemaTitle "My Foo Title") #-\} | ||
{\-# ANN MkMyFoo (SchemaTitle "Title") #-\} | ||
newtype MyFoo = MkMyFoo Int | ||
@ | ||
-} | ||
newtype SchemaTitle = SchemaTitle String | ||
deriving newtype (Eq, Ord, Show, Typeable, ToJSON) | ||
deriving stock (Data, Lift) | ||
|
||
{- | An annotation for the "description" schema attribute. | ||
This annotation could be attached to a type or constructor: | ||
@ | ||
{\-# ANN type MyFoo (SchemaDescription "My Foo Description") #-\} | ||
{\-# ANN MkMyFoo (SchemaDescription "Description") #-\} | ||
newtype MyFoo = MkMyFoo Int | ||
@ | ||
-} | ||
newtype SchemaDescription = SchemaDescription String | ||
deriving newtype (Eq, Ord, Show, Typeable, ToJSON) | ||
deriving stock (Data, Lift) | ||
|
||
{- | An annotation for the "$comment" schema attribute. | ||
This annotation could be attached to a type or constructor: | ||
@ | ||
{\-# ANN type MyFoo (SchemaComment "My Foo Comment") #-\} | ||
{\-# ANN MkMyFoo (SchemaComment "Comment") #-\} | ||
newtype MyFoo = MkMyFoo Int | ||
@ | ||
-} | ||
newtype SchemaComment = SchemaComment String | ||
deriving newtype (Eq, Ord, Show, Typeable, ToJSON) | ||
deriving stock (Data, Lift) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters