From 3e07c7c88343ae7ce92d43f326804a666c01ca11 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 2 Jul 2026 09:27:01 +0100 Subject: Add crate path segment --- .../compiler/output/lang/rust/RustWriter.java | 4 +++ .../output/lang/rust/ast/RustPathIdentSegment.java | 2 +- .../lang/rust/ast/RustPathIdentSegmentCrate.java | 24 ++++++++++++++++ .../output/lang/rust/ast/RustTypePath.java | 14 +++++++++ .../output/lang/rust/ast/RustTypePathSegment.java | 9 ++++-- .../compiler/output/lang/rust/RustWriterTests.java | 9 ++++++ .../rust/ast/RustPathIdentSegmentCrateMatcher.java | 33 ++++++++++++++++++++++ 7 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrate.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrateMatcher.java (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index 6cd67c6..77ed461 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -124,6 +124,10 @@ public class RustWriter implements AutoCloseable { private void writePathIdentSegment(RustPathIdentSegment pathIdentSegment) throws IOException { switch (pathIdentSegment) { + case RustPathIdentSegmentCrate _ -> { + this.writer.write("crate"); + } + case RustPathIdentSegmentIdentifier pathIdentSegmentIdentifier -> { this.writeIdentifier(pathIdentSegmentIdentifier.name()); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java index 325a4a4..a342288 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment imports -public sealed interface RustPathIdentSegment permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier { +public sealed interface RustPathIdentSegment permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrate.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrate.java new file mode 100644 index 0000000..d1a73cc --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrate.java @@ -0,0 +1,24 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate imports + +public record RustPathIdentSegmentCrate() implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.Builder(); + } + + public record Builder() implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate(); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java index 9b44334..7d56e5e 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Stream; // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath imports public record RustTypePath(java.util.List segments) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType { @@ -57,6 +58,19 @@ public record RustTypePath(java.util.List new RustTypePathSegment( + new RustPathIdentSegmentIdentifier(new RustIdentifier(name)), + Optional.empty() + )) + ) + .toList(); + return new RustTypePath(segments); + } + public RustType withArgs(List args) { var segments = new ArrayList<>(this.segments); var lastSegment = segments.removeLast().withArgs(args); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java index 78348c5..beb4c64 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java @@ -3,14 +3,13 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment imports -// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment imports - import java.util.List; import java.util.Optional; +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment imports public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier.arbitrary().build(), java.util.Optional.empty()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.arbitrary().build(), java.util.Optional.empty()); } public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { @@ -39,6 +38,10 @@ public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rus return new RustTypePathSegment(new RustPathIdentSegmentIdentifier(name), Optional.empty()); } + public static RustTypePathSegment crate() { + return new RustTypePathSegment(new RustPathIdentSegmentCrate(), Optional.empty()); + } + public RustTypePathSegment withArgs(List args) { if (this.args.isPresent()) { throw new IllegalArgumentException("Type path segment already has args"); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index f52d275..319393e 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -95,6 +95,15 @@ public class RustWriterTests { assertThat(string, equalTo("std::string::String")); } + @Test + public void crateTypeSegmentIsWritten() throws IOException { + var rust = RustTypePath.crate("a", "b"); + + var string = write(writer -> writer.writeType(rust)); + + assertThat(string, equalTo("crate::a::b")); + } + @Test public void typeSegmentGenericArgsAreWritten() throws IOException { var rust = RustTypePath.of("std", "collections", "HashMap") diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrateMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrateMatcher.java new file mode 100644 index 0000000..3a2285c --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentCrateMatcher.java @@ -0,0 +1,33 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher imports + +public class RustPathIdentSegmentCrateMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher isRustPathIdentSegmentCrate() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustPathIdentSegmentCrateMatcher(java.util.List> submatchers) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.class, this.submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrateMatcher body +} -- cgit v1.2.3