Something went wrong. Try again.
Define datatypes once, get polyglot results
Something went wrong. Try again.
1.7 kB · 56 lines
Haskell
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657{-# LANGUAGE OverloadedStrings #-}
module DataTypeTool.Config.Extras where
import qualified Antelude.List as List
import qualified Data.Map.Strict as Mapimport qualified Data.Text as Textimport qualified Data.Version as Version
import Text.Read ( readMaybe )
import Toml ( (.=) )import qualified Toml
versionCodec :: Toml.Key -> Toml.TomlCodec Version.VersionversionCodec 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