Skip to content

Commit cfaa3f5

Browse files
committed
Reduce duplication in encodeDumpProject.
1 parent 0df31d2 commit cfaa3f5

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/Stack/ConfigCmd.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,17 @@ configCmdSetScope (ConfigCmdSetSystemGhc scope _) = scope
129129
configCmdSetScope (ConfigCmdSetInstallGhc scope _) = scope
130130

131131
encodeDumpProject :: RawYaml -> ConfigDumpFormat -> Project -> ByteString
132-
encodeDumpProject rawConfig ConfigDumpYaml p = let e = Yaml.encode p in
133-
Yaml.decodeEither' e & either (const e) (\(d :: KeyMap Yaml.Value) ->
132+
encodeDumpProject rawConfig format p
133+
| ConfigDumpYaml <- format = dumpProject (\e d ->
134134
either (const e) encodeUtf8 (cfgRedress rawConfig d ""))
135-
encodeDumpProject rawConfig ConfigDumpJson p = let e = Yaml.encode p in
136-
Yaml.decodeEither' e & either (const e) (\(d :: KeyMap Yaml.Value) ->
137-
toStrictBytes $ encodePretty' (Aeson.defConfig{confCompare = cfgKeyCompare rawConfig d ""}) d)
135+
| ConfigDumpJson <- format = dumpProject (\_ d ->
136+
let cmp = cfgKeyCompare rawConfig d ""
137+
in toStrictBytes $ encodePretty' (Aeson.defConfig{confCompare = cmp}) d)
138+
where
139+
-- REVIEW: Is there a way to encode straight to keymap?
140+
-- encode project to bytestring then decode to keymap.
141+
dumpProject f = let e = Yaml.encode p in Yaml.decodeEither' e &
142+
either (const e) (\(d :: KeyMap Yaml.Value) -> f e d)
138143

139144
cfgKeyCompare :: RawYaml -> KeyMap Yaml.Value -> Text -> (Text -> Text -> Ordering)
140145
cfgKeyCompare (yamlLines -> configLines) (fmap Key.toText . KeyMap.keys -> keys) cmdKey =

0 commit comments

Comments
 (0)