Skip to content

Commit

Permalink
[Rust] generate message schema level info in lib.rs (#1019)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
wbprime authored Nov 13, 2024
1 parent 320850d commit d38b4bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 7 additions & 0 deletions rust/tests/baseline_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,10 @@ fn encode_car_from_scratch() -> SbeResult<(usize, Vec<u8>)> {
let limit = car.get_limit();
Ok((limit, buffer))
}

#[test]
fn test_issue_1018() {
assert_eq!(1, examples_baseline::SBE_SCHEMA_ID);
assert_eq!(0, examples_baseline::SBE_SCHEMA_VERSION);
assert_eq!("5.2", examples_baseline::SBE_SEMANTIC_VERSION);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.stream.Stream;
import uk.co.real_logic.sbe.ir.Ir;

import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.*;
Expand Down Expand Up @@ -54,7 +55,7 @@ class LibRsDef
this.schemaVersionType = schemaVersionType;
}

void generate() throws IOException
void generate(final Ir ir) throws IOException
{
try (Writer libRs = outputManager.createOutput("lib"))
{
Expand Down Expand Up @@ -84,6 +85,8 @@ void generate() throws IOException
}
indent(libRs, 0, "\n");

generateSbeSchemaConsts(libRs, ir);

generateSbeErrorEnum(libRs);
generateEitherEnum(libRs);

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

static void generateSbeSchemaConsts(final Writer writer, final Ir ir) throws IOException
{
final String schemaIdType = rustTypeName(ir.headerStructure().schemaIdType());
final String schemaVersionType = rustTypeName(ir.headerStructure().schemaVersionType());
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();

indent(writer, 0, "pub const SBE_SCHEMA_ID: %s = %d;\n", schemaIdType, ir.id());
indent(writer, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n", schemaVersionType, ir.version());
indent(writer, 0, "pub const SBE_SEMANTIC_VERSION: &str = \"%s\";\n\n", semanticVersion);
}

static void generateSbeErrorEnum(final Writer writer) throws IOException
{
indent(writer, 0, "pub type SbeResult<T> = core::result::Result<T, SbeErr>;\n\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,22 @@ public void generate() throws IOException
indent(out, 0, "use crate::*;\n\n");
indent(out, 0, "pub use decoder::%sDecoder;\n", formatStructName(msgToken.name()));
indent(out, 0, "pub use encoder::%sEncoder;\n\n", formatStructName(msgToken.name()));

indent(out, 0, "pub use crate::SBE_SCHEMA_ID;\n");
indent(out, 0, "pub use crate::SBE_SCHEMA_VERSION;\n");
indent(out, 0, "pub use crate::SBE_SEMANTIC_VERSION;\n\n");

final String blockLengthType = blockLengthType();
final String templateIdType = rustTypeName(ir.headerStructure().templateIdType());
final String schemaIdType = rustTypeName(ir.headerStructure().schemaIdType());
final String schemaVersionType = schemaVersionType();
final String semanticVersion = ir.semanticVersion() == null ? "" : ir.semanticVersion();
indent(out, 0, "pub const SBE_BLOCK_LENGTH: %s = %d;\n", blockLengthType, msgToken.encodedLength());
indent(out, 0, "pub const SBE_TEMPLATE_ID: %s = %d;\n", templateIdType, msgToken.id());
indent(out, 0, "pub const SBE_SCHEMA_ID: %s = %d;\n", schemaIdType, ir.id());
indent(out, 0, "pub const SBE_SCHEMA_VERSION: %s = %d;\n", schemaVersionType, ir.version());
indent(out, 0, "pub const SBE_SEMANTIC_VERSION: &str = \"%s\";\n\n", semanticVersion);
indent(out, 0, "pub const SBE_TEMPLATE_ID: %s = %d;\n\n", templateIdType, msgToken.id());

MessageCoderDef.generateEncoder(ir, out, msgToken, fields, groups, varData);
MessageCoderDef.generateDecoder(ir, out, msgToken, fields, groups, varData);
}
}

libRsDef.generate();
libRsDef.generate(ir);
}

String blockLengthType()
Expand Down

0 comments on commit d38b4bf

Please sign in to comment.