private static String sanitizeMethodOrProperty(final String name) { if (shadowsKeyword(name)) { return name + "_"; } else { return name; } }
public CodeGenerator newInstance(final Ir ir, final String outputDir) { return defaultRustGenerator(ir, outputDir); }
static Appendable indent(final Appendable appendable) throws IOException { return indent(appendable, 1); }
private static void generateMessageEncoder( final OutputManager outputManager, final MessageComponents components, final List<GroupTreeNode> groupTree, final Optional<FieldsRepresentationSummary> fieldsRepresentation, final int headerSize) throws IOException { final Token msgToken = components.messageToken; final String messageTypeName = formatTypeName(msgToken.name()); final RustCodecType codecType = RustCodecType.Encoder; String topType = codecType.generateDoneCoderType(outputManager, messageTypeName); topType = generateTopVarDataCoders(messageTypeName, components.varData, outputManager, topType, codecType); topType = generateGroupsCoders(groupTree, outputManager, topType, codecType); topType = generateFixedFieldCoder(messageTypeName, outputManager, topType, fieldsRepresentation, codecType); topType = codecType.generateMessageHeaderCoder(messageTypeName, outputManager, topType, headerSize); generateEntryPoint(messageTypeName, outputManager, topType, codecType); }
void appendWrapMethod(final Appendable appendable, final String structName) throws IOException { appendable.append("\n").append(INDENT).append(String.format( "fn wrap(%s: %s) -> %s {%n", scratchProperty(), RustGenerator.withLifetime(scratchType()), RustGenerator.withLifetime(structName))); indent(appendable, 2, "%s { %s: %s }\n", structName, scratchProperty(), scratchProperty()); indent(appendable).append("}\n"); }
void appendDirectCodeMethods( final Appendable appendable, final String methodName, final String representationType, final String nextCoderType, final int numBytes) throws IOException { indent(appendable, 1, "pub fn %s(mut self) -> CodecResult<(&%s %s, %s)> {\n", methodName, DATA_LIFETIME, representationType, RustGenerator.withLifetime(nextCoderType)); indent(appendable, 2, "let v = self.%s.read_type::<%s>(%s)?;\n", RustCodecType.Decoder.scratchProperty(), representationType, numBytes); indent(appendable, 2, "Ok((v, %s::wrap(self.%s)))\n", nextCoderType, RustCodecType.Decoder.scratchProperty()); indent(appendable).append("}\n"); }
public static CodeGenerator defaultRustGenerator(final Ir ir, final String outputDir) { return new RustGenerator(ir, new RustFlatFileOutputManager(outputDir, ir.applicableNamespace())); } }
@Test public void generateSharedImports() throws IOException { RustGenerator.generateSharedImports(generateIrForResource(BROAD_USE_CASES_SCHEMA), outputManager); assertContainsSharedImports(outputManager.toString()); }
private static String fullGenerateForResource( final SingleStringOutputManager outputManager, final String localResourceName) { outputManager.clear(); final RustGenerator rustGenerator = new RustGenerator(generateIrForResource(localResourceName), outputManager); try { rustGenerator.generate(); } catch (final IOException e) { throw new AssertionFailedError("Unexpected IOException during generation. " + e.getMessage()); } return outputManager.toString(); }
@Test(expected = NullPointerException.class) public void nullOutputManagerTossesNpe() { new RustGenerator(minimalDummyIr(), null); }
@Test(expected = NullPointerException.class) public void nullDirectoryParamThrowsNPE() { new RustFlatFileOutputManager(null, PACKAGE_NAME); }
@Test(expected = NullPointerException.class) public void generateRustLiteralNullValueParam() { generateRustLiteral(PrimitiveType.INT8, null); }
private void generateGroupFieldRepresentations( final OutputManager outputManager, final List<GroupTreeNode> groupTree) throws IOException { try (Writer writer = outputManager.createOutput("Group fixed-field member representations")) { generateGroupFieldRepresentations(writer, groupTree); } }
@Test(expected = NullPointerException.class) public void nullOutputDirParamShouldTossNPE() { new Rust().newInstance(minimalDummyIr(), null); }
@Test public void fullGenerateBasicTypes() { final String generatedRust = fullGenerateForResource(outputManager, BASIC_TYPES_SCHEMA); assertContainsSharedImports(generatedRust); assertContainsNumericEnum(generatedRust); }
private void assertSchemaInterpretableAsRust(final String localResourceSchema) throws IOException, InterruptedException { final String rust = fullGenerateForResource(outputManager, localResourceSchema); assertRustBuildable(rust, Optional.of(localResourceSchema)); }
@Test(expected = IllegalArgumentException.class) public void emptyParamToEightBitCharacterThrowsIAE() { RustUtil.eightBitCharacter(""); }
private static void generateMessageDecoder( final OutputManager outputManager, final MessageComponents components, final List<GroupTreeNode> groupTree, final Optional<FieldsRepresentationSummary> fieldsRepresentation, final int headerSize) throws IOException { final Token msgToken = components.messageToken; final String messageTypeName = formatTypeName(msgToken.name()); final RustCodecType codecType = RustCodecType.Decoder; String topType = codecType.generateDoneCoderType(outputManager, messageTypeName); topType = generateTopVarDataCoders(messageTypeName, components.varData, outputManager, topType, codecType); topType = generateGroupsCoders(groupTree, outputManager, topType, codecType); topType = generateFixedFieldCoder(messageTypeName, outputManager, topType, fieldsRepresentation, codecType); topType = codecType.generateMessageHeaderCoder(messageTypeName, outputManager, topType, headerSize); generateEntryPoint(messageTypeName, outputManager, topType, codecType); }
@Test public void generateBasicEnum() throws IOException { RustGenerator.generateSharedImports(generateIrForResource(BASIC_TYPES_SCHEMA), outputManager); assertContainsSharedImports(outputManager.toString()); }
@Test(expected = IllegalArgumentException.class) public void tooManyCharactersParamToEightBitCharacterThrowsIAE() { RustUtil.eightBitCharacter("ABC"); }