Composing parsers and decoders, parse overload selection #211
-
Hello. I am experimenting with trying to compose a pipeline of some existing What I've come up with is this: extension FooDecodableType {
static func parser() -> Parsers.AnyParser<Data, Self> {
.init { (input: inout Data) -> Self in
try FooDecoder.static.decode(Self.self, from: input)
}
}
}
extension BarDecodableType {
static func parser() -> Parsers.AnyParser<Data, Self> {
.init { (input: inout Data) -> Self in
try BarDecoder.static.decode(Self.self, from: input)
}
}
}
enum Either {
case foo(FooDecodableType)
case bar(BarDecodableType)
}
let eitherParser = OneOf {
Parse(Either.foo) {
FooDecodableType.parser()
}
Parse(Either.bar) {
BarDecodableType.parser()
}
} At this point, I would like to call: let myData: Data = // ...
let parsed = try eitherParser.parse(data) I would like to call the following overload, for the ergonomics and because I'm not interested in unconsumed input: swift-parsing/Sources/Parsing/Parser.swift Lines 43 to 67 in 4932348 But Swift selects this overload, I presume because Data conforms to Collection :swift-parsing/Sources/Parsing/Parser.swift Lines 69 to 136 in 4932348 Processing my input Data as a Collection like this causes parsing to fail. If I forgo the ergonomic overload, and pass my Data as an inout parameter, parsing suceeds:
var myData: Data = // ...
let parsed = try eitherParser.parse(&data) So, on one level, I suppose I'm asking about overload selection from libraries and if there's a way to elect the overload I want. On a broader level, though, I wonder what people think about composing decoding and parsing like this, or if maybe there's another way to approach this that would obviate the overload issue. Maybe I should put the decoding in a map parser's Thanks for any thoughts! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thinking on it some more, I am leaning towards decoding in a transform is indeed the idiomatic integration with swift-parsing. |
Beta Was this translation helpful? Give feedback.
-
@kamcma Is there a reason your parsers aren't consuming the Alternately, you may want to use a conversion here, which better describe non-incremental transformations (and is what we do with JSON encoding/decoding). I understand you may not care about creating full parser-printer here, though, and maybe there is no encoder available to use conversions instead. But if you do, you could use the conversion in |
Beta Was this translation helpful? Give feedback.
@kamcma Is there a reason your parsers aren't consuming the
input
? Parsers are supposed to incrementally consume the data handed to them, so you might want to callinput.removeAll()
after successfully decoding those types.Alternately, you may want to use a conversion here, which better describe non-incremental transformations (and is what we do with JSON encoding/decoding). I understand you may not care about creating full parser-printer here, though, and maybe there is no encoder available to use conversions instead. But if you do, you could use the conversion in
MapConversion
.