package org.shedlang.compiler.backends; import org.shedlang.compiler.config.json.Json5ObjectWithSource; import org.zwobble.sourcetext.SourceText; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public record ExamplesConfig( List examples ) { public static ExamplesConfig read(Path projectPath) throws IOException { var configPath = projectPath.resolve("shed-examples.json5"); var contents = Files.readString(configPath); var sourceText = SourceText.fromString(configPath.toString(), contents); return fromSourceText(projectPath, sourceText); } public static ExamplesConfig fromSourceText(Path projectPath, SourceText sourceText) { return fromJson5( projectPath, Json5ObjectWithSource.parse(sourceText) ); } public static ExamplesConfig fromJson5(Path root, Json5ObjectWithSource object) { return new ExamplesConfig( examplesFromJson5(root, object) ); } public static List examplesFromJson5( Path root, Json5ObjectWithSource object ) { return object.getArrayOfObjects("examples") .orElseGet(() -> List.of()) .stream() .map(example -> ExampleConfig.fromJson5(root, example)) .toList(); } }