[core] Simplify parseInterpolate

* Merge type checking into match arms
* No need to use interpolator->match(...)
This commit is contained in:
John Firebaugh
2018-06-27 14:01:37 -07:00
parent 63517b1b6f
commit 5ecfed5b79

View File

@@ -155,52 +155,28 @@ ParseResult parseInterpolate(const Convertible& value, ParsingContext& ctx) {
assert(outputType);
if (
*outputType != type::Number &&
*outputType != type::Color &&
!(
outputType->is<type::Array>() &&
outputType->get<type::Array>().itemType == type::Number &&
outputType->get<type::Array>().N
)
)
{
ctx.error("Type " + toString(*outputType) + " is not interpolatable.");
return ParseResult();
}
return outputType->match(
[&](const type::NumberType&) -> ParseResult {
return interpolator->match([&](const auto& interpolator_) {
return ParseResult(std::make_unique<Interpolate<double>>(
*outputType, interpolator_, std::move(*input), std::move(stops)
));
});
return ParseResult(std::make_unique<Interpolate<double>>(
*outputType, *interpolator, std::move(*input), std::move(stops)
));
},
[&](const type::ColorType&) -> ParseResult {
return interpolator->match([&](const auto& interpolator_) {
return ParseResult(std::make_unique<Interpolate<Color>>(
*outputType, interpolator_, std::move(*input), std::move(stops)
));
});
return ParseResult(std::make_unique<Interpolate<Color>>(
*outputType, *interpolator, std::move(*input), std::move(stops)
));
},
[&](const type::Array& arrayType) -> ParseResult {
return interpolator->match(
[&](const auto& continuousInterpolator) {
if (arrayType.itemType != type::Number || !arrayType.N) {
assert(false); // interpolability already checked above.
return ParseResult();
}
return ParseResult(std::make_unique<Interpolate<std::vector<Value>>>(
*outputType, continuousInterpolator, std::move(*input), std::move(stops)
));
}
);
if (arrayType.itemType != type::Number || !arrayType.N) {
ctx.error("Type " + toString(*outputType) + " is not interpolatable.");
return ParseResult();
}
return ParseResult(std::make_unique<Interpolate<std::vector<Value>>>(
*outputType, *interpolator, std::move(*input), std::move(stops)
));
},
[&](const auto&) {
// unreachable: Null, Boolean, String, Object, Value output types
// are not interpolatable, and interpolability was already checked above
assert(false);
ctx.error("Type " + toString(*outputType) + " is not interpolatable.");
return ParseResult();
}
);