diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-03-21 13:01:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-03-21 13:01:34 +0000 |
| commit | f1aa04299d193826643bf6b3da37901b7de4b4b2 (patch) | |
| tree | d02e6386cd2cd8b5caf97610d2389968e6b4a7e4 | |
| parent | 99d37eb1f20e0c7fdcbc4bf42a25b94b33b5f793 (diff) | |
Serialize Patio exception type definition to protobuf
3 files changed, 42 insertions, 1 deletions
diff --git a/protobuf/patio.proto b/protobuf/patio.proto index 8e391e34..be227c2d 100644 --- a/protobuf/patio.proto +++ b/protobuf/patio.proto @@ -19,6 +19,7 @@ message PatioCompilationUnitElement { PatioDataElementImmutableDefinition data_element_immutable_definition = 1; PatioDataElementMutableDeclaration data_element_mutable_declaration = 2; PatioDataElementMutableDefinition data_element_mutable_definition = 3; + PatioExceptionTypeDefinition exception_type_definition = 4; } } @@ -48,6 +49,14 @@ message PatioDataElementMutableDefinition { PatioDataValue initial_value = 4; } +message PatioExceptionTypeDefinition { + option features.(pb.java).nest_in_file_class = YES; + + Source source = 1; + string name = 2; + PatioType value_type = 3; +} + enum PatioVisibility { option features.(pb.java).nest_in_file_class = YES; diff --git a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java index 0f3ffcc0..9a6e8213 100644 --- a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java +++ b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java @@ -53,7 +53,9 @@ public class PatioToProtobuf { ); } case PatioExceptionTypeDefinition exceptionTypeDefinition -> { - throw new UnsupportedOperationException("TODO"); + builder.setExceptionTypeDefinition( + serializeExceptionTypeDefinition(exceptionTypeDefinition, parentSource) + ); } case PatioFunctionDeclaration functionDeclaration -> { throw new UnsupportedOperationException("TODO"); @@ -94,6 +96,13 @@ public class PatioToProtobuf { deserializeSource(elementProtobuf.getDataElementMutableDefinition().getSource(), parentSource) ); + case EXCEPTION_TYPE_DEFINITION -> + new PatioExceptionTypeDefinition( + elementProtobuf.getExceptionTypeDefinition().getName(), + deserializeType(elementProtobuf.getExceptionTypeDefinition().getValueType()), + deserializeSource(elementProtobuf.getExceptionTypeDefinition().getSource(), parentSource) + ); + case VALUE_NOT_SET -> throw new UnsupportedOperationException("TODO"); }; @@ -134,6 +143,17 @@ public class PatioToProtobuf { return builder.build(); } + private static PatioProtobuf.PatioExceptionTypeDefinition serializeExceptionTypeDefinition( + PatioExceptionTypeDefinition element, + Source parentSource + ) { + var builder = PatioProtobuf.PatioExceptionTypeDefinition.newBuilder(); + builder.setSource(serializeSource(element.source(), parentSource)); + builder.setName(element.name()); + builder.setValueType(serializeType(element.valueType())); + return builder.build(); + } + // == Data ptr values == public static PatioProtobuf.PatioDataPtrValue serializeDataPtrValue(PatioDataPtrValue value) { diff --git a/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java b/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java index 30ee7f56..25054ae2 100644 --- a/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java +++ b/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java @@ -66,6 +66,18 @@ public class PatioToProtobufTests { assertThat(deserializedElement, equalTo(element)); } + @Test + public void canSerializeExceptionTypeDefinition() { + var element = Patio.exceptionTypeDefinition("Error", PatioTypes.INT); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + // == Data ptr values == @Test |
