X.U.ExtensibleConf: Add high-level idioms for non-Semigroup, but Default types

For configuration values that don't compose well using a Semigroup
instance, provide a high-level API allowing arbitrary modification of
the value, taking its Default if absent. This API is only usable for
separate configuration data and cannot be used to guard addition of hook
using `once`.
This commit is contained in:
Tomas Janousek
2021-10-17 22:47:58 +01:00
parent 3dbdc51158
commit 3a72dd5355
2 changed files with 70 additions and 8 deletions

View File

@@ -21,11 +21,21 @@ spec = do
specify "lookup @() . add @String . add @[Int]" $
XC.lookup (XC.add "a" (XC.add [1 :: Int] def)) `shouldBe` (Nothing :: Maybe ())
specify "once" $
borderWidth (XC.once incBorderWidth "a" def) `shouldBe` succ (borderWidth def)
specify "once . once" $
borderWidth (XC.once incBorderWidth "b" (XC.once incBorderWidth "a" def))
`shouldBe` succ (borderWidth def)
specify "once" $ do
let c = XC.once incBorderWidth "a" def
borderWidth c `shouldBe` succ (borderWidth def)
XC.lookup c `shouldBe` Just "a"
specify "once . once" $ do
let c = XC.once incBorderWidth "b" (XC.once incBorderWidth "a" def)
borderWidth c `shouldBe` succ (borderWidth def)
XC.lookup c `shouldBe` Just "ab"
specify "modifyDef" $ do
let c = XC.modifyDef (<> "a") def
XC.lookup c `shouldBe` Just "a"
specify "modifyDef . modifyDef" $ do
let c = XC.modifyDef (<> "b") (XC.modifyDef (<> "a") def)
XC.lookup c `shouldBe` Just "ab"
incBorderWidth :: XConfig l -> XConfig l
incBorderWidth c = c{ borderWidth = succ (borderWidth c) }