Skip to content

Commit 7920ebe

Browse files
authored
Refactored newVertexTree (#142)
- Replace nested case expressions in `newVertexTree` with `runExcept` - Moved "invalid breakpoint" error message to `determineGroup`
1 parent 64cc633 commit 7920ebe

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

jbeam-edit.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ library jbeam-edit-transformation
157157
build-depends:
158158
extra,
159159
ordered-containers,
160+
transformers,
160161
yaml
161162

162163
else

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal-libraries:
8383
when:
8484
condition: flag(transformation)
8585
then:
86-
dependencies: [yaml, extra, ordered-containers]
86+
dependencies: [yaml, extra, transformers, ordered-containers]
8787
else:
8888
buildable: false
8989
source-dirs: src-extra/transformation

src-extra/transformation/JbeamEdit/Transformation.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ addVertexTreeToForest newNames tf grouped forest forestAcc t =
131131
groupAnnotatedVertices
132132
:: XGroupBreakpoints
133133
-> AnnotatedVertex
134-
-> Maybe (VertexTreeType, [AnnotatedVertex])
134+
-> Either Text (VertexTreeType, [AnnotatedVertex])
135135
groupAnnotatedVertices brks g = (,[g]) <$> determineGroup' brks (aVertex g)
136136

137137
updateSupportVertexName
@@ -212,7 +212,7 @@ moveVerticesInVertexForest topNode newNames tfCfg vertexTrees =
212212
vertexTrees
213213
brks = xGroupBreakpoints tfCfg
214214
in case mapM (groupAnnotatedVertices brks) allVertices of
215-
Just movableVertices' -> do
215+
Right movableVertices' -> do
216216
let groupedVertices = M.fromListWith (++) movableVertices'
217217
(badBeamNodes, conns) <-
218218
vertexConns (maxSupportCoordinates tfCfg) topNode groupedVertices
@@ -228,7 +228,7 @@ moveVerticesInVertexForest topNode newNames tfCfg vertexTrees =
228228
supportForest
229229
treesOrder
230230
Right (badBeamNodes, newForest)
231-
Nothing -> Left "invalid breakpoint"
231+
Left err -> Left err
232232

233233
getVertexNamesInForest
234234
:: VertexForest -> M.Map (Scientific, Scientific, Scientific) Text
@@ -356,7 +356,7 @@ assignNames newNames brks treeType prefixMap av =
356356
let v = aVertex av
357357
updatedPrefix cleanPrefix' = M.findWithDefault cleanPrefix' cleanPrefix' newNames
358358
prefix = dropIndex (vName v)
359-
typeSpecific = maybe "" prefixForType (determineGroup brks v)
359+
typeSpecific = either (const "") prefixForType (determineGroup brks v)
360360
(prefix', lastChar) = fromMaybe (error "unreachable") (T.unsnoc prefix)
361361
isLmr = lastChar `elem` ['l', 'm', 'r']
362362
supportPrefixChar = T.singleton 's' <> bool typeSpecific (T.singleton lastChar) isLmr

src-extra/transformation/JbeamEdit/Transformation/VertexExtraction.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module JbeamEdit.Transformation.VertexExtraction (
77
) where
88

99
import Control.Monad (guard)
10+
import Control.Monad.Except (runExcept)
11+
import Control.Monad.Trans.Except (except)
1012
import Data.Char (isDigit)
1113
import Data.List.NonEmpty (NonEmpty (..))
1214
import Data.List.NonEmpty qualified as NE
@@ -201,30 +203,28 @@ newVertexTree brks vertexNames badAcc vertexForest nodes =
201203
topComments = mapMaybe toInternalComment topNodes
202204
topMeta = M.unions . map metaMapFromObject $ topNodes
203205
vertexPrefix = getVertexPrefix' nodes'
204-
in case breakVertices vertexPrefix vertexNames nodes' of
205-
Left err -> Left err
206-
Right (vertexNames', vertexNodes, rest') ->
207-
case nodesToAnnotatedVertices topMeta vertexNodes of
208-
Left err -> Left err
209-
Right (badNodes, avNE) ->
210-
let firstAV = NE.head avNE
211-
vertexTree = VertexTree topComments avNE
212-
in case determineGroup brks (aVertex firstAV) of
213-
Just treeType ->
214-
let updatedForest = insertTreeInForest treeType vertexTree vertexForest
215-
in Right
216-
(vertexNames', badAcc <> badNodes, treeType, vertexTree, updatedForest, rest')
217-
Nothing -> Left "invalid breakpoint"
218-
219-
determineGroup :: XGroupBreakpoints -> Vertex -> Maybe VertexTreeType
206+
in runExcept
207+
( do
208+
(vertexNames', vertexNodes, rest') <-
209+
except (breakVertices vertexPrefix vertexNames nodes')
210+
(badNodes, avNE) <- except (nodesToAnnotatedVertices topMeta vertexNodes)
211+
let firstAV = NE.head avNE
212+
vertexTree = VertexTree topComments avNE
213+
treeType <- except . determineGroup brks . aVertex $ firstAV
214+
let updatedForest = insertTreeInForest treeType vertexTree vertexForest
215+
pure
216+
(vertexNames', badAcc <> badNodes, treeType, vertexTree, updatedForest, rest')
217+
)
218+
219+
determineGroup :: XGroupBreakpoints -> Vertex -> Either Text VertexTreeType
220220
determineGroup (XGroupBreakpoints brks) v =
221221
case [vtype | (XGroupBreakpoint f brk, vtype) <- brks, applyOperator f (vX v) brk] of
222-
(vtype : _) -> Just vtype
223-
[] -> Nothing
222+
(vtype : _) -> Right vtype
223+
[] -> Left "invalid breakpoint"
224224

225-
determineGroup' :: XGroupBreakpoints -> Vertex -> Maybe VertexTreeType
225+
determineGroup' :: XGroupBreakpoints -> Vertex -> Either Text VertexTreeType
226226
determineGroup' brks v
227-
| isSupportVertex v = Just SupportTree
227+
| isSupportVertex v = Right SupportTree
228228
| otherwise = determineGroup brks v
229229

230230
nodesListToTree

0 commit comments

Comments
 (0)