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"); }
private static void generateEntryPoint( final String messageTypeName, final OutputManager outputManager, final String topType, final RustCodecType codecType) throws IOException { try (Writer writer = outputManager.createOutput(messageTypeName + format(" %s entry point", codecType.name()))) { final String gerund = codecType.gerund(); writer.append(format("pub fn start_%s_%s<%s>(data: &%s%s [u8]) -> %s {\n", gerund, formatMethodName(messageTypeName), DATA_LIFETIME, DATA_LIFETIME, codecType == RustCodecType.Encoder ? " mut" : "", withLifetime(topType))); indent(writer, 1, "%s::wrap(%s { data: data, pos: 0 })\n", topType, codecType.scratchType()); writer.append("}\n"); } }
final String headerDecoderType = node.contextualName + "HeaderDecoder"; final String groupLevelNextDecoderType = format("Either<%s, %s>", withLifetime(memberDecoderType), initialNextDecoderType.startsWith("Either") ? initialNextDecoderType : withLifetime(initialNextDecoderType)); String nextDecoderType = groupLevelNextDecoderType;
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"); }
try (Writer writer = outputManager.createOutput(name + " variable-length data")) appendStructHeader(writer, withLifetime(decoderType), false); final String contentPropertyName = groupDepth > 0 ? "parent" : SCRATCH_DECODER_PROPERTY; indent(writer, 1, "%s: %s,\n", contentPropertyName, withLifetime(contentType)); writer.append("}\n"); indent(writer, 1, "fn wrap(%s: %s) -> Self {\n", contentPropertyName, withLifetime(contentType)); indent(writer, 2, "%s { %s: %s }\n", decoderType, contentPropertyName, contentPropertyName).append(INDENT).append("}\n"); atEndOfGroup ? nextDecoderType : withLifetime(nextDecoderType)); indent(writer, 2, "let count = *%s.read_type::<%s>(%s)?;\n", toScratchChain(groupDepth), rustTypeName(this.lengthType), this.lengthType.size());
void appendDirectCodeMethods( final Appendable appendable, final String methodName, final String representationType, final String nextCoderType, final int numBytes) throws IOException { indent(appendable, 1, "\n/// Create a mutable struct reference overlaid atop the data buffer\n"); indent(appendable, 1, "/// such that changes to the struct directly edit the buffer. \n"); indent(appendable, 1, "/// Note that the initial content of the struct's fields may be garbage.\n"); indent(appendable, 1, "pub fn %s(mut self) -> CodecResult<(&%s mut %s, %s)> {\n", methodName, DATA_LIFETIME, representationType, RustGenerator.withLifetime(nextCoderType)); indent(appendable, 2, "let v = self.%s.writable_overlay::<%s>(%s)?;\n", RustCodecType.Encoder.scratchProperty(), representationType, numBytes); indent(appendable, 2, "Ok((v, %s::wrap(self.%s)))\n", nextCoderType, RustCodecType.Encoder.scratchProperty()); indent(appendable).append("}\n\n"); indent(appendable, 1, "/// Copy the bytes of a value into the data buffer\n"); indent(appendable).append(String.format("pub fn %s_copy(mut self, t: &%s) -> CodecResult<%s> {\n", methodName, representationType, RustGenerator.withLifetime(nextCoderType))); indent(appendable, 2) .append(format("self.%s.write_type::<%s>(t, %s)?;\n", RustCodecType.Encoder.scratchProperty(), representationType, numBytes)); indent(appendable, 2).append(format("Ok(%s::wrap(self.%s))\n", nextCoderType, RustCodecType.Encoder.scratchProperty())); indent(appendable).append("}\n"); }
try (Writer writer = outputManager.createOutput(name + " variable-length data")) appendStructHeader(writer, withLifetime(decoderType), false); final String contentPropertyName = groupDepth > 0 ? "parent" : codecType.scratchProperty(); indent(writer, 1, "%s: %s,\n", contentPropertyName, withLifetime(contentType)); writer.append("}\n"); contentPropertyName, withLifetime(contentType)); indent(writer, 2, "%s { %s: %s }\n", decoderType, contentPropertyName, contentPropertyName).append(INDENT).append("}\n"); atEndOfGroup ? nextCoderType : withLifetime(nextCoderType));
appendStructHeader(out, withLifetime(memberDecoderType), false); final String rustCountType = rustTypeName(node.numInGroupType); final String contentProperty; contentBearingType = withLifetime(node.parent.get().contextualName + "MemberDecoder"); contentBearingType = withLifetime(SCRATCH_DECODER_TYPE); nextDecoderType.startsWith("Either") ? nextDecoderType : withLifetime(nextDecoderType))); indent(out, 2).append("}\n").append(INDENT).append("}\n").append("}\n"); appendStructHeader(out, withLifetime(headerDecoderType), false); indent(out, 1, "%s: %s,\n", contentProperty, contentBearingType).append("}\n");
try (Writer out = outputManager.createOutput(node.contextualName + " Encoder for fields and header")) appendStructHeader(out, withLifetime(memberCoderType), false); final String rustCountType = rustTypeName(node.numInGroupType); final String contentProperty; contentBearingType = withLifetime(node.parent.get().contextualName + "MemberEncoder"); contentBearingType = withLifetime(SCRATCH_ENCODER_TYPE); indent(out, 1, "pub fn next_%s_member(mut self, fields: &%s)", formatMethodName(node.originalName), fieldsType); out.append(format(" -> CodecResult<%s> {\n", withLifetime(nextCoderType))); final String scratchChain = toScratchChain(node); indent(out, 2, "%s.write_type::<%s>(fields, %s)?; // block length\n", formatMethodName(node.originalName), withLifetime(afterGroupCoderType)); indent(out, 2, "%s.write_at_position::<%s>(self.count_write_pos, &self.count, %s)?;\n", scratchChain, rustCountType, node.numInGroupType.size()); appendStructHeader(out, withLifetime(headerCoderType), false); indent(out, 1, "%s: %s,\n", contentProperty, contentBearingType); out.append("}\n"); formatMethodName(node.originalName), withLifetime(memberCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType),
indent(out, 1, "pub fn %s_as_slice(mut self, count: %s) -> CodecResult<(&%s mut [%s], %s)> {\n", formatMethodName(node.originalName), rustCountType, DATA_LIFETIME, fieldsType, withLifetime(afterGroupCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType), indent(out, 1, "pub fn %s_from_slice(mut self, s: &[%s]) -> CodecResult<%s> {\n", formatMethodName(node.originalName), fieldsType, withLifetime(afterGroupCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType),
private static void appendFixedSizeMemberGroupDecoderMethods( final String initialNextDecoderType, final GroupTreeNode node, final boolean atEndOfParent, final Writer out, final String contentProperty) throws IOException { indent(out, 1, "pub fn %s_as_slice(mut self) -> CodecResult<(&%s [%s], %s)> {\n", formatMethodName(node.originalName), DATA_LIFETIME, node.contextualName + "Member", initialNextDecoderType.startsWith("Either") ? initialNextDecoderType : withLifetime(initialNextDecoderType)); indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n", toScratchChain(node), node.blockLengthType.size()); indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n", toScratchChain(node), rustTypeName(node.numInGroupType), node.numInGroupType.size()); indent(out, 2, "let s = %s.read_slice::<%s>(count as usize, %s)?;\n", toScratchChain(node), node.contextualName + "Member", node.blockLength); indent(out, 2, "Ok((s,%s))\n", atEndOfParent ? "self.parent.after_member()" : format("%s::wrap(self.%s)", initialNextDecoderType, contentProperty)); indent(out, 1, "}\n"); }
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 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"); }
private static void generateEntryPoint( final String messageTypeName, final OutputManager outputManager, final String topType, final RustCodecType codecType) throws IOException { try (Writer writer = outputManager.createOutput(messageTypeName + format(" %s entry point", codecType.name()))) { final String gerund = codecType.gerund(); writer.append(format("pub fn start_%s_%s<%s>(data: &%s%s [u8]) -> %s {\n", gerund, formatMethodName(messageTypeName), DATA_LIFETIME, DATA_LIFETIME, codecType == RustCodecType.Encoder ? " mut" : "", withLifetime(topType))); indent(writer, 1, "%s::wrap(%s { data: data, pos: 0 })\n", topType, codecType.scratchType()); writer.append("}\n"); } }
private static void generateEntryPoint( final String messageTypeName, final OutputManager outputManager, final String topType, final RustCodecType codecType) throws IOException { try (Writer writer = outputManager.createOutput(messageTypeName + format(" %s entry point", codecType.name()))) { final String gerund = codecType.gerund(); writer.append(format("pub fn start_%s_%s<%s>(data: &%s%s [u8]) -> %s {\n", gerund, formatMethodName(messageTypeName), DATA_LIFETIME, DATA_LIFETIME, codecType == RustCodecType.Encoder ? " mut" : "", withLifetime(topType))); indent(writer, 1, "%s::wrap(%s { data: data, pos: 0 })\n", topType, codecType.scratchType()); writer.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"); }
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"); }
void appendDirectCodeMethods( final Appendable appendable, final String methodName, final String representationType, final String nextCoderType, final int numBytes) throws IOException { indent(appendable, 1, "\n/// Create a mutable struct reference overlaid atop the data buffer\n"); indent(appendable, 1, "/// such that changes to the struct directly edit the buffer. \n"); indent(appendable, 1, "/// Note that the initial content of the struct's fields may be garbage.\n"); indent(appendable, 1, "pub fn %s(mut self) -> CodecResult<(&%s mut %s, %s)> {\n", methodName, DATA_LIFETIME, representationType, RustGenerator.withLifetime(nextCoderType)); indent(appendable, 2, "let v = self.%s.writable_overlay::<%s>(%s)?;\n", RustCodecType.Encoder.scratchProperty(), representationType, numBytes); indent(appendable, 2, "Ok((v, %s::wrap(self.%s)))\n", nextCoderType, RustCodecType.Encoder.scratchProperty()); indent(appendable).append("}\n\n"); indent(appendable, 1, "/// Copy the bytes of a value into the data buffer\n"); indent(appendable).append(String.format("pub fn %s_copy(mut self, t: &%s) -> CodecResult<%s> {\n", methodName, representationType, RustGenerator.withLifetime(nextCoderType))); indent(appendable, 2) .append(format("self.%s.write_type::<%s>(t, %s)?;\n", RustCodecType.Encoder.scratchProperty(), representationType, numBytes)); indent(appendable, 2).append(format("Ok(%s::wrap(self.%s))\n", nextCoderType, RustCodecType.Encoder.scratchProperty())); indent(appendable).append("}\n"); }
private static void appendFixedSizeMemberGroupDecoderMethods( final String initialNextDecoderType, final GroupTreeNode node, final boolean atEndOfParent, final Writer out, final String contentProperty) throws IOException { indent(out, 1, "pub fn %s_as_slice(mut self) -> CodecResult<(&%s [%s], %s)> {\n", formatMethodName(node.originalName), DATA_LIFETIME, node.contextualName + "Member", initialNextDecoderType.startsWith("Either") ? initialNextDecoderType : withLifetime(initialNextDecoderType)); indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n", toScratchChain(node), node.blockLengthType.size()); indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n", toScratchChain(node), rustTypeName(node.numInGroupType), node.numInGroupType.size()); indent(out, 2, "let s = %s.read_slice::<%s>(count as usize, %s)?;\n", toScratchChain(node), node.contextualName + "Member", node.blockLength); indent(out, 2, "Ok((s,%s))\n", atEndOfParent ? "self.parent.after_member()" : format("%s::wrap(self.%s)", initialNextDecoderType, contentProperty)); indent(out, 1, "}\n"); }
private static void appendFixedSizeMemberGroupDecoderMethods( final String initialNextDecoderType, final GroupTreeNode node, final boolean atEndOfParent, final Writer out, final String contentProperty) throws IOException { indent(out, 1, "pub fn %s_as_slice(mut self) -> CodecResult<(&%s [%s], %s)> {\n", formatMethodName(node.originalName), DATA_LIFETIME, node.contextualName + "Member", initialNextDecoderType.startsWith("Either") ? initialNextDecoderType : withLifetime(initialNextDecoderType)); indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n", toScratchChain(node), node.blockLengthType.size()); indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n", toScratchChain(node), rustTypeName(node.numInGroupType), node.numInGroupType.size()); indent(out, 2, "let s = %s.read_slice::<%s>(count as usize, %s)?;\n", toScratchChain(node), node.contextualName + "Member", node.blockLength); indent(out, 2, "Ok((s,%s))\n", atEndOfParent ? "self.parent.after_member()" : format("%s::wrap(self.%s)", initialNextDecoderType, contentProperty)); indent(out, 1, "}\n"); }