Skip to content

Commit

Permalink
Move addFields to ParseUtils, add aliasField helper
Browse files Browse the repository at this point in the history
`addFields` should be in `ParseUtils` with the rest of the field
helpers.
  • Loading branch information
9999years committed Nov 7, 2024
1 parent a39266d commit 39db107
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 32 additions & 0 deletions cabal-install/src/Distribution/Client/ParseUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module Distribution.Client.ParseUtils
FieldDescr (..)
, liftField
, liftFields
, addFields
, aliasField
, filterFields
, mapFieldNames
, commandOptionToField
Expand Down Expand Up @@ -103,9 +105,15 @@ liftFields get set = map (liftField get set)

-- | Given a collection of field descriptions, keep only a given list of them,
-- identified by name.
--
-- TODO: This makes it easy to footgun by providing a non-existent field name.
filterFields :: [String] -> [FieldDescr a] -> [FieldDescr a]
filterFields includeFields = filter ((`elem` includeFields) . fieldName)

-- | Given a collection of field descriptions, get a field with a given name.
getField :: String -> [FieldDescr a] -> Maybe (FieldDescr a)
getField name = find ((== name) . fieldName)

-- | Apply a name mangling function to the field names of all the field
-- descriptions. The typical use case is to apply some prefix.
mapFieldNames :: (String -> String) -> [FieldDescr a] -> [FieldDescr a]
Expand All @@ -120,6 +128,30 @@ commandOptionToField = viewAsFieldDescr
commandOptionsToFields :: [OptionField a] -> [FieldDescr a]
commandOptionsToFields = map viewAsFieldDescr

-- | Add fields to a field list.
addFields
:: [FieldDescr a]
-> ([FieldDescr a] -> [FieldDescr a])
addFields = (++)

-- | Add a new field which is identical to an existing field but with a
-- different name.
aliasField
:: String
-- ^ The existing field name.
-> String
-- ^ The new field name.
-> [FieldDescr a]
-> [FieldDescr a]
aliasField oldName newName fields =
let fieldToRename = getField oldName fields
in case fieldToRename of
-- TODO: Should this throw?
Nothing -> fields
Just fieldToRename' ->
let newField = fieldToRename'{fieldName = newName}
in newField : fields

------------------------------------------
-- SectionDescr definition and utilities
--
Expand Down
6 changes: 0 additions & 6 deletions cabal-install/src/Distribution/Client/ProjectConfig/Legacy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2073,9 +2073,3 @@ showTokenQ "" = Disp.empty
showTokenQ x@('-' : '-' : _) = Disp.text (show x)
showTokenQ x@('.' : []) = Disp.text (show x)
showTokenQ x = showToken x

-- Handy util
addFields
:: [FieldDescr a]
-> ([FieldDescr a] -> [FieldDescr a])
addFields = (++)

0 comments on commit 39db107

Please sign in to comment.