{-# LANGUAGE OverloadedStrings #-} module DataTypeTool.Config.Extras where import qualified Antelude.List as List import qualified Data.Map.Strict as Map import qualified Data.Text as Text import qualified Data.Version as Version import Text.Read ( readMaybe ) import Toml ( (.=) ) import qualified Toml versionCodec :: Toml.Key -> Toml.TomlCodec Version.Version versionCodec key = let splitByDot :: String -> [String] splitByDot str = case List.dropWhile (== '.') str of "" -> [] str' -> w : splitByDot str'' where (w, str'') = List.break (== '.') str' strToVersion :: String -> Version.Version strToVersion str = str |> splitByDot |> List.map ( \s -> (readMaybe s :: Maybe Int) |> \case Just a -> a Nothing -> 0 ) |> Version.makeVersion in Toml.dimap Version.showVersion strToVersion (Toml.string key) optionalTextConsiderBlanks :: Toml.Key -> Toml.TomlCodec (Maybe Text) optionalTextConsiderBlanks key = Toml.dioptional (Toml.text key) |> considerBlankAsNothing where considerJustBlank :: Maybe Text -> Maybe Text considerJustBlank (Nothing) = Nothing considerJustBlank (Just "") = Nothing considerJustBlank (Just s) = Just s considerBlankAsNothing :: Toml.TomlCodec (Maybe Text) -> Toml.TomlCodec (Maybe Text) considerBlankAsNothing = Toml.dimap considerJustBlank considerJustBlank