From d2b41777eea3250250b31c206648e9b77f9cbae7 Mon Sep 17 00:00:00 2001 From: Nicholas Clarke Date: Wed, 15 Jan 2025 14:34:19 +0100 Subject: [PATCH] Add optics for modifying name and comment --- cuddle.cabal | 1 + src/Codec/CBOR/Cuddle/Huddle/Optics.hs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/Codec/CBOR/Cuddle/Huddle/Optics.hs diff --git a/cuddle.cabal b/cuddle.cabal index 5315b25..3ff1486 100644 --- a/cuddle.cabal +++ b/cuddle.cabal @@ -51,6 +51,7 @@ library Codec.CBOR.Cuddle.CDDL.Resolve Codec.CBOR.Cuddle.Huddle Codec.CBOR.Cuddle.Huddle.HuddleM + Codec.CBOR.Cuddle.Huddle.Optics Codec.CBOR.Cuddle.Parser Codec.CBOR.Cuddle.Pretty diff --git a/src/Codec/CBOR/Cuddle/Huddle/Optics.hs b/src/Codec/CBOR/Cuddle/Huddle/Optics.hs new file mode 100644 index 0000000..45ee0e8 --- /dev/null +++ b/src/Codec/CBOR/Cuddle/Huddle/Optics.hs @@ -0,0 +1,24 @@ +-- | Optics for mutating Huddle rules +module Codec.CBOR.Cuddle.Huddle.Optics (commentL, nameL) where + +import Codec.CBOR.Cuddle.Huddle +import Data.Generics.Product (HasField' (field')) +import Data.Text qualified as T +import Optics.Core + +mcommentL :: + (HasField' "description" a (Maybe T.Text)) => + Lens a a (Maybe T.Text) (Maybe T.Text) +mcommentL = field' @"description" + +-- | Traversal to the comment field of a description. Using this we can for +-- example set the comment with 'a & commentL .~ "This is a comment"' +commentL :: + (HasField' "description" a (Maybe T.Text)) => + AffineTraversal a a T.Text T.Text +commentL = mcommentL % _Just + +-- | Lens to the name of a rule (or other named entity). Using this we can +-- for example append to the name with 'a & nameL %~ (<> "_1")' +nameL :: Lens (Named a) (Named a) T.Text T.Text +nameL = field' @"name"