-
Notifications
You must be signed in to change notification settings - Fork 4
Introduce HuddleM #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| {-# LANGUAGE OverloadedLists #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE RecursiveDo #-} | ||
| module Monad where | ||
|
|
||
| import Codec.CBOR.Cuddle.Huddle.HuddleM | ||
| import Data.Word (Word64) | ||
|
|
||
| spec :: Huddle | ||
| spec = huddleDef $ mdo | ||
| transaction <- "transaction" =:= mp | ||
| [ idx 0 ==> set txIn, | ||
| idx 1 ==> set txOut | ||
| ] | ||
| txIn <- "txIn" =:= arr [ "transaction_id" ==> hash32, "index" ==> txId] | ||
| txOut <- "txOut" =:= arr [ idx 0 ==> address, idx 1 ==> value] | ||
| txId <- "txId" =:= VUInt `sized` (2 :: Word64) | ||
| address <- "address" =:= VBytes `sized` (32 :: Word64) | ||
| hash32 <- "hash32" =:= VBytes `sized` (32 :: Word64) | ||
| value <- "value" =:= VUInt | ||
| set <- binding $ \x -> "set" =::= arr [0 <+ a x] | ||
|
|
||
| setRootRules [transaction] | ||
| pure () | ||
This file contains hidden or 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 hidden or 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,75 @@ | ||
| -- | Monad for declaring Huddle constructs | ||
| module Codec.CBOR.Cuddle.Huddle.HuddleM | ||
| ( module Huddle, | ||
| (=:=), | ||
| (=:~), | ||
| (=::=), | ||
| binding, | ||
| setRootRules, | ||
| huddleDef, | ||
| huddleDef', | ||
| include, | ||
| ) | ||
| where | ||
|
|
||
| import Codec.CBOR.Cuddle.Huddle hiding (binding, (=:=), (=:~)) | ||
| import Codec.CBOR.Cuddle.Huddle qualified as Huddle | ||
| import Control.Monad.State.Strict (State, modify, runState) | ||
| import Data.Default.Class (def) | ||
| import Data.Generics.Product (HasField (..)) | ||
| import Data.Text qualified as T | ||
| import Optics.Core (Field2 (..), set, (%), (%~)) | ||
|
|
||
| type HuddleM = State Huddle | ||
|
|
||
| -- | Overridden version of assignment which also adds the rule to the state | ||
| (=:=) :: (IsType0 a) => T.Text -> a -> HuddleM Rule | ||
| n =:= b = let r = n Huddle.=:= b in include r | ||
|
|
||
| infixl 1 =:= | ||
|
|
||
| -- | Overridden version of group assignment which adds the rule to the state | ||
| (=:~) :: T.Text -> Group -> HuddleM (Named Group) | ||
| n =:~ b = let r = n Huddle.=:~ b in include r | ||
|
|
||
| infixl 1 =:~ | ||
|
|
||
| binding :: | ||
| forall t0. | ||
| (IsType0 t0) => | ||
| (GRef -> Rule) -> | ||
| HuddleM (t0 -> GRuleCall) | ||
| binding fRule = include (Huddle.binding fRule) | ||
|
|
||
| -- | Renamed version of Huddle's underlying '=:=' for use in generic bindings | ||
| (=::=) :: (IsType0 a) => T.Text -> a -> Rule | ||
| n =::= b = n Huddle.=:= b | ||
|
|
||
| infixl 1 =::= | ||
|
|
||
| setRootRules :: [Rule] -> HuddleM () | ||
| setRootRules = modify . set (field @"roots") | ||
|
|
||
| huddleDef :: HuddleM a -> Huddle | ||
| huddleDef = snd . huddleDef' | ||
|
|
||
| huddleDef' :: HuddleM a -> (a, Huddle) | ||
| huddleDef' mh = (_2 % field @"items") %~ reverse $ runState mh def | ||
|
|
||
| class Includable a where | ||
| -- | Include a rule, group, or generic rule defined elsewhere | ||
| include :: a -> HuddleM a | ||
|
|
||
| instance Includable Rule where | ||
| include r = modify (field @"items" %~ (HIRule r :)) >> pure r | ||
|
|
||
| instance Includable (Named Group) where | ||
| include r = modify ((field @"items") %~ (HIGroup r :)) >> pure r | ||
|
|
||
| instance (IsType0 t0) => Includable (t0 -> GRuleCall) where | ||
| include gr = | ||
| let fakeT0 = error "Attempting to unwrap fake value in generic call" | ||
| grDef = callToDef <$> gr fakeT0 | ||
| in do | ||
| modify (field @"items" %~ (HIGRule grDef :)) | ||
| pure gr |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would make sense to return all of the roots instead of setting them via
setRootRulesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I didn't do this is that it might be needed to return specific things to allow them to be re-used.