From 728811d3be6ef5847ef0113bda6c26bc2f5912b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 2 Feb 2024 17:12:36 +0100 Subject: [PATCH] feat: convert more snippets to compiled snippets --- .../cloud/snippet/AggregateParserExample.java | 43 +++++++++++++++++++ .../incendo/cloud/snippet/EitherExample.java | 29 +++++++++++++ docs/core/index.md | 29 +------------ mkdocs.yml | 2 + 4 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 code/src/main/java/org/incendo/cloud/snippet/AggregateParserExample.java create mode 100644 code/src/main/java/org/incendo/cloud/snippet/EitherExample.java diff --git a/code/src/main/java/org/incendo/cloud/snippet/AggregateParserExample.java b/code/src/main/java/org/incendo/cloud/snippet/AggregateParserExample.java new file mode 100644 index 0000000..931fe48 --- /dev/null +++ b/code/src/main/java/org/incendo/cloud/snippet/AggregateParserExample.java @@ -0,0 +1,43 @@ +package org.incendo.cloud.snippet; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.Command; +import org.incendo.cloud.parser.ArgumentParseResult; +import org.incendo.cloud.parser.aggregate.AggregateParser; + +import static org.incendo.cloud.parser.standard.IntegerParser.integerParser; +import static org.incendo.cloud.parser.standard.StringParser.stringParser; + +/** + * Example of {@link AggregateParser}. + */ +public class AggregateParserExample { + + public void example(final Command.@NonNull Builder commandBuilder) { + // --8<-- [start:snippet] + final AggregateParser locationParser = AggregateParser + .builder() + .withComponent("world", stringParser()) + .withComponent("x", integerParser()) + .withComponent("y", integerParser()) + .withComponent("z", integerParser()) + .withMapper(Location.class, (commandContext, aggregateCommandContext) -> { + final String world = aggregateCommandContext.get("world"); + final int x = aggregateCommandContext.get("x"); + final int y = aggregateCommandContext.get("y"); + final int z = aggregateCommandContext.get("z"); + return ArgumentParseResult.successFuture(new Location(world, x, y, z)); + }).build(); + // --8<-- [end:snippet] + } + + public static final class Location { + + public Location(final String world, final int x, final int y, final int z) { + } + } + + public static final class CommandSender { + + } +} diff --git a/code/src/main/java/org/incendo/cloud/snippet/EitherExample.java b/code/src/main/java/org/incendo/cloud/snippet/EitherExample.java new file mode 100644 index 0000000..2f6a9e7 --- /dev/null +++ b/code/src/main/java/org/incendo/cloud/snippet/EitherExample.java @@ -0,0 +1,29 @@ +package org.incendo.cloud.snippet; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.incendo.cloud.Command; +import org.incendo.cloud.parser.ArgumentParser; +import org.incendo.cloud.type.Either; + +import static org.incendo.cloud.parser.standard.BooleanParser.booleanParser; +import static org.incendo.cloud.parser.standard.IntegerParser.integerParser; + +/** + * Example of {@link Either}. + */ +public class EitherExample { + + public void example(final Command.@NonNull Builder commandBuilder) { + // --8<-- [start:snippet] + commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser())) + .handler(context -> { + Either either = context.get("either"); + if (either.primary().isPresent()){ + int integer = either.primary().get(); + } else { + boolean bool = either.fallback().get(); + } + }); + // --8<-- [end:snippet] + } +} diff --git a/docs/core/index.md b/docs/core/index.md index 8099076..fea1787 100644 --- a/docs/core/index.md +++ b/docs/core/index.md @@ -608,22 +608,7 @@ interface, or using construct the parser by using a builder that you create by calling {{ javadoc("https://javadoc.io/doc/org.incendo/cloud-core/latest/org/incendo/cloud/parser/aggregate/AggregateParser.html#builder()", "AggregateParser.builder()") }}. - -!!! example "Example Aggregate Parser" - ```java - final AggregateParser locationParser = AggregateParser.builder() - .withComponent("world", worldParser()) - .withComponent("x", integerParser()) - .withComponent("y", integerParser()) - .withComponent("z", integerParser()) - .withMapper(Location.class, (commandContext, aggregateCommandContext) -> { - final World world = aggregateCommandContext.get("world"); - final int x = aggregateCommandContext.get("x"); - final int y = aggregateCommandContext.get("y"); - final int z = aggregateCommandContext.get("z"); - return CompletableFuture.completedFuture(new Location(world, x, y, z)); - }).build(); - ``` +{{ snippet("AggregateParserExample.java", title = "Example Aggregate Parser") }} ### Either @@ -635,17 +620,7 @@ The parser will first attempt to parse the primary type `A`, and if this fails i fallback type `B`. The suggestions of both the primary and fallback parsers will be joined when using the parser as the suggestion provider. -```java title="Example of Either" -commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser())) - .handler(context -> { - Either either = context.get("either"); - if (either.primary().isPresent()){ - int integer = either.primary().get(); - } else { - boolean bool = either.fallback().get(); - } - }); -``` +{{ snippet("EitherExample.java", title = "Example of Either") }} ### Custom Parsers diff --git a/mkdocs.yml b/mkdocs.yml index d9a6b21..f5797f2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -110,6 +110,7 @@ markdown_extensions: base_path: - "./code/src/main/java/org/incendo/cloud/snippet/" check_paths: true + dedent_subsections: true - attr_list - md_in_html plugins: @@ -159,3 +160,4 @@ copyright: > Change cookie settings watch: - code + - main.py