Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] generate message schema level info in lib.rs #1019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading