Skip to content

Commit d38b4bf

Browse files
authored
[Rust] generate message schema level info in lib.rs (#1019)
* [Rust] generate message schema level info in lib.rs 1. add `SBE_SCHEMA_ID` 2. add `SBE_SCHEMA_VERSION` 3. add `SBE_SEMANTIC_VERSION` * [Rust] use pub use instead of duplicated defines for message schema items in message codec * [Rust] add tests for issue #1018
1 parent 320850d commit d38b4bf

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

rust/tests/baseline_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,10 @@ fn encode_car_from_scratch() -> SbeResult<(usize, Vec<u8>)> {
266266
let limit = car.get_limit();
267267
Ok((limit, buffer))
268268
}
269+
270+
#[test]
271+
fn test_issue_1018() {
272+
assert_eq!(1, examples_baseline::SBE_SCHEMA_ID);
273+
assert_eq!(0, examples_baseline::SBE_SCHEMA_VERSION);
274+
assert_eq!("5.2", examples_baseline::SBE_SEMANTIC_VERSION);
275+
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/LibRsDef.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.LinkedHashSet;
2525
import java.util.stream.Stream;
26+
import uk.co.real_logic.sbe.ir.Ir;
2627

2728
import static java.nio.ByteOrder.LITTLE_ENDIAN;
2829
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.*;
@@ -54,7 +55,7 @@ class LibRsDef
5455
this.schemaVersionType = schemaVersionType;
5556
}
5657

57-
void generate() throws IOException
58+
void generate(final Ir ir) throws IOException
5859
{
5960
try (Writer libRs = outputManager.createOutput("lib"))
6061
{
@@ -84,6 +85,8 @@ void generate() throws IOException
8485
}
8586
indent(libRs, 0, "\n");
8687

88+
generateSbeSchemaConsts(libRs, ir);
89+
8790
generateSbeErrorEnum(libRs);
8891
generateEitherEnum(libRs);
8992

@@ -123,6 +126,17 @@ static void generateDecoderTraits(final String schemaVersionType, final Writer w
123126
indent(writer, 0, "}\n\n");
124127
}
125128

129+
static void generateSbeSchemaConsts(final Writer writer, final Ir ir) throws IOException
130+
{
131+
final String schemaIdType = rustTypeName(ir.headerStructure().schemaIdType());
132+
final String schemaVersionType = rustTypeName(ir.headerStructure().schemaVersionType());
133+
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();
134+
135+
indent(writer, 0, "pub const SBE_SCHEMA_ID: %s = %d;\n", schemaIdType, ir.id());
136+
indent(writer, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n", schemaVersionType, ir.version());
137+
indent(writer, 0, "pub const SBE_SEMANTIC_VERSION: &str = \"%s\";\n\n", semanticVersion);
138+
}
139+
126140
static void generateSbeErrorEnum(final Writer writer) throws IOException
127141
{
128142
indent(writer, 0, "pub type SbeResult<T> = core::result::Result<T, SbeErr>;\n\n");

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,22 @@ public void generate() throws IOException
159159
indent(out, 0, "use crate::*;\n\n");
160160
indent(out, 0, "pub use decoder::%sDecoder;\n", formatStructName(msgToken.name()));
161161
indent(out, 0, "pub use encoder::%sEncoder;\n\n", formatStructName(msgToken.name()));
162+
163+
indent(out, 0, "pub use crate::SBE_SCHEMA_ID;\n");
164+
indent(out, 0, "pub use crate::SBE_SCHEMA_VERSION;\n");
165+
indent(out, 0, "pub use crate::SBE_SEMANTIC_VERSION;\n\n");
166+
162167
final String blockLengthType = blockLengthType();
163168
final String templateIdType = rustTypeName(ir.headerStructure().templateIdType());
164-
final String schemaIdType = rustTypeName(ir.headerStructure().schemaIdType());
165-
final String schemaVersionType = schemaVersionType();
166-
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();
167169
indent(out, 0, "pub const SBE_BLOCK_LENGTH: %s = %d;\n", blockLengthType, msgToken.encodedLength());
168-
indent(out, 0, "pub const SBE_TEMPLATE_ID: %s = %d;\n", templateIdType, msgToken.id());
169-
indent(out, 0, "pub const SBE_SCHEMA_ID: %s = %d;\n", schemaIdType, ir.id());
170-
indent(out, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n", schemaVersionType, ir.version());
171-
indent(out, 0, "pub const SBE_SEMANTIC_VERSION: &str = \"%s\";\n\n", semanticVersion);
170+
indent(out, 0, "pub const SBE_TEMPLATE_ID: %s = %d;\n\n", templateIdType, msgToken.id());
172171

173172
MessageCoderDef.generateEncoder(ir, out, msgToken, fields, groups, varData);
174173
MessageCoderDef.generateDecoder(ir, out, msgToken, fields, groups, varData);
175174
}
176175
}
177176

178-
libRsDef.generate();
177+
libRsDef.generate(ir);
179178
}
180179

181180
String blockLengthType()

0 commit comments

Comments
 (0)