com.zarbosoft.interface1.Events Maven / Gradle / Ivy
package com.zarbosoft.interface1;
import com.zarbosoft.interface1.events.InterfaceEvent;
import com.zarbosoft.interface1.events.ReadEventGrammar;
import com.zarbosoft.pidgoon.events.Grammar;
import com.zarbosoft.pidgoon.events.Parse;
import com.zarbosoft.rendaw.common.Pair;
import io.github.classgraph.ScanResult;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.stream.Stream;
/**
* Helper methods to use with pidgoon Grammars.
*/
public class Events {
/**
* Parse an object from a stream of common InterfaceEvents. Can be used to easily create type deserializers given
* a parser that emits the basic common events.
*
* @param scanner
* @param typeInfo The type to deserialize.
* @param data The stream of events.
* @param typeInfo
* @return The deserialized object.
*/
public static T parse(
final ScanResult scanner, final TypeInfo typeInfo, final Stream> data
) {
final HashSet seen = new HashSet<>();
final Grammar grammar = ReadEventGrammar.buildGrammar(scanner, typeInfo);
return new Parse().grammar(grammar).errorHistory(5).parse(data.map(pair -> pair));
}
}