Skip to content

Commit

Permalink
feat: convert more snippets to compiled snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Feb 2, 2024
1 parent 4d0bb38 commit 728811d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<CommandSender> commandBuilder) {
// --8<-- [start:snippet]
final AggregateParser<CommandSender, Location> locationParser = AggregateParser
.<CommandSender>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 {

}
}
29 changes: 29 additions & 0 deletions code/src/main/java/org/incendo/cloud/snippet/EitherExample.java
Original file line number Diff line number Diff line change
@@ -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<C> {

public void example(final Command.@NonNull Builder<C> commandBuilder) {
// --8<-- [start:snippet]
commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser()))
.handler(context -> {
Either<Integer, Boolean> either = context.get("either");
if (either.primary().isPresent()){
int integer = either.primary().get();
} else {
boolean bool = either.fallback().get();
}
});
// --8<-- [end:snippet]
}
}
29 changes: 2 additions & 27 deletions docs/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()") }}.

<!-- prettier-ignore -->
!!! example "Example Aggregate Parser"
```java
final AggregateParser<CommandSender, Location> locationParser = AggregateParser.<CommandSender>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

Expand All @@ -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<Integer, Boolean> 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

Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -159,3 +160,4 @@ copyright: >
<a href="#__consent">Change cookie settings</a>
watch:
- code
- main.py

0 comments on commit 728811d

Please sign in to comment.