-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to parse a primitive type to a more constrained type? e.g. how to parse an 'IP' from a text field? #411
Comments
So there were a few functions I have missed when looking through the documentation before.
|
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? |
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. |
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 = .....
|
Here's a codec for 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 |
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
(orText -> Either Text IPAddress
) for the parsing, andIPAddress -> Text
for the prettyprinting.My hunch is that I'm looking for something that is very close to the signature of
dimatch
:but with the opposite variance.
(Of course, using an
Either
to keep track of the error messages is nicer than usingMaybe
here).But maybe there is also a much simpler way that I'm missing?
I'm still very new to bidirectional parsing.
The text was updated successfully, but these errors were encountered: