kowainik / tomland

🏝 Bidirectional TOML serialization
https://kowainik.github.io/posts/2019-01-14-tomland
Mozilla Public License 2.0
120 stars 39 forks source link

How to parse a primitive type to a more constrained type? e.g. how to parse an 'IP' from a text field? #411

Open Qqwy opened 2 years ago

Qqwy commented 2 years ago

I am looking to convert e.g. a string or text field that follows certain other rules into a more restricted datatype. For instance, consider the 'server' field in this example https://github.com/kowainik/tomland/blob/main/test/examples/example.toml#L12.

We might want to parse this to a valid IP address datastructure (or fail parsing with a descriptive error). Vice-versa we of course want to be able to pretty-print this IP address back as a string.

So we have a Text -> Maybe IPAddress (or Text -> Either Text IPAddress) for the parsing, and IPAddress -> Text for the prettyprinting.

My hunch is that I'm looking for something that is very close to the signature of dimatch:

Toml.dimatch  :: (b -> Maybe a) -> (a -> b) -> Toml.TomlCodec a -> Toml.TomlCodec b

but with the opposite variance.

Toml.diparse  :: (b -> a) -> (a -> Maybe b) -> Toml.TomlCodec b -> Toml.TomlCodec a

(Of course, using an Either to keep track of the error messages is nicer than using Maybe here).

But maybe there is also a much simpler way that I'm missing? I'm still very new to bidirectional parsing.

Qqwy commented 2 years ago

So there were a few functions I have missed when looking through the documentation before. Conclusion: It is already very possible to do this right now, especially when parsing data from a Text field.

Qqwy commented 2 years ago

I'm leaving this issue open, because I think that one (or multiple) of these bullet points might be useful to add to the README in the section about translating between TOML and your desired Haskell.

What do you think? If you give the green light, I'd love to contribute a PR for this.

CGenie commented 1 year ago

It would be nice to add this to the docs. I had to dig this issue to see how to write a custom parser for a custom data type.

meghfossa commented 1 year ago

Adding example here, for future readers as I ran into this today

import Text.Megaparsec
import Data.Text (Text)
import Toml (TomlCodec, (.=))
import Toml qualified

-- name: somename 
-- special: [<MyEntity>, <MyEntity>, ...]

data Example = Example
    {
        name :: Text,
        special :: Maybe [MyEntity]
    }
    deriving (Eq, Ord, Show)

exampleCodec :: TomlCodec Example
exampleCodec = Example 
      <$> Toml.diwrap (Toml.text "name") .= name
      <*> Toml.dioptional (Toml.arrayOf myCodec "special") .= special

myCodec :: Toml.TomlBiMap MyEntity Toml.AnyValue
myCodec = Toml._TextBy (toText . show) parseMyEntity

parseMyEntity :: Text -> Either Text MyEntity
parseMyEntity candidate = case runParser myEntityParser "" candidate of
  Left peb -> Left $ toText $ errorBundlePretty peb
  Right rr -> Right rr

myEntityParser :: Parser MyEntity
myEntityParser = .....
domenkozar commented 8 months ago

Here's a codec for uri-bytestring:

uri :: Toml.Key -> TomlCodec (URI.URIRef URI.Absolute)
uri = Toml.match (_URI2ByteString >>> Toml._ByteString)

_URI2ByteString :: Toml.TomlBiMap (URI.URIRef URI.Absolute) ByteString
_URI2ByteString = Toml.BiMap
    { forward  = Right . URI.serializeURIRef'
    , backward = left (Toml.ArbitraryError . show) . URI.parseURI URI.strictURIParserOptions
    }

Maybe we should start tomland-extras?