static String generateRustLiteral(final PrimitiveType type, final String value) { Verify.notNull(type, "type"); Verify.notNull(value, "value"); final String typeName = rustTypeName(type); if (typeName == null) { throw new IllegalArgumentException("Unknown Rust type name found for primitive " + type.primitiveName()); } switch (type) { case CHAR: case UINT8: case INT8: case INT16: case UINT16: case INT32: case INT64: case UINT32: case UINT64: return value + typeName; case FLOAT: case DOUBLE: return value.endsWith("NaN") ? typeName + "::NAN" : value + typeName; default: throw new IllegalArgumentException("Unsupported literal generation for type: " + type.primitiveName()); } }
private static void generateEnum(final List<Token> enumTokens, final OutputManager outputManager) throws IOException { final String originalEnumName = enumTokens.get(0).applicableTypeName(); final String enumRustName = formatTypeName(originalEnumName); try (Writer writer = outputManager.createOutput("Enum " + enumRustName)) { final List<Token> messageBody = getMessageBody(enumTokens); if (messageBody.isEmpty()) { throw new IllegalArgumentException("No valid values provided for enum " + originalEnumName); } writer.append("#[derive(Clone,Copy,Debug,PartialEq,Eq,PartialOrd,Ord,Hash)]").append("\n"); final String rustReprTypeName = rustTypeName(messageBody.get(0).encoding().primitiveType()); writer.append(format("#[repr(%s)]", rustReprTypeName)).append("\n"); writer.append("pub enum ").append(enumRustName).append(" {\n"); for (final Token token : messageBody) { final Encoding encoding = token.encoding(); final String literal = generateRustLiteral(encoding.primitiveType(), encoding.constValue().toString()); indent(writer, 1).append(token.name()) .append(" = ") .append(literal) .append(",\n"); } writer.append("}\n"); } }
final String rustPrimitiveType = RustUtil.rustTypeName(typeToken.encoding().primitiveType()); final String rustFieldType = getRustTypeForPrimitivePossiblyArray(typeToken, rustPrimitiveType); appendable.append(rustFieldType);
formatMethodName(name), DATA_LIFETIME, rustTypeName(this.dataType), atEndOfGroup ? nextDecoderType : withLifetime(nextDecoderType)); indent(writer, 2, "let count = *%s.read_type::<%s>(%s)?;\n", toScratchChain(groupDepth), rustTypeName(this.lengthType), this.lengthType.size()); rustTypeName(this.dataType), this.dataType.size(), goToNext); indent(writer).append("}\n");
formatMethodName(name), DATA_LIFETIME, rustTypeName(this.dataType), atEndOfGroup ? nextCoderType : withLifetime(nextCoderType)); indent(writer, 2).append("// Write data length\n"); indent(writer, 2, "%s.write_type::<%s>(&(l as %s), %s)?; // group length\n", toScratchChain(groupDepth), rustTypeName(this.lengthType), rustTypeName(this.lengthType), this.lengthType.size()); indent(writer, 2).append(format("%s.write_slice_without_count::<%s>(s, %s)?;\n", toScratchChain(groupDepth), rustTypeName(this.dataType), this.dataType.size())); indent(writer, 2, "Ok(%s)\n", atEndOfGroup ? "self.parent" : format("%s::wrap(self.%s)", nextCoderType, contentPropertyName));
final String rustCountType = rustTypeName(node.numInGroupType); final String contentProperty; final String contentBearingType; 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).append("if count > 0 {\n"); indent(out, 3, "Ok(Either::Left(%s::new(self.%s, count)))\n",
final String constantRustPrimitiveType = RustUtil.rustTypeName( signalToken.encoding().primitiveType()); constantRustTypeName = getRustTypeForPrimitivePossiblyArray(
withLifetime(afterGroupCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType), generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)), node.blockLengthType.size()); withLifetime(afterGroupCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType), generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)), node.blockLengthType.size());
final String rustCountType = rustTypeName(node.numInGroupType); final String contentProperty; final String contentBearingType; formatMethodName(node.originalName), withLifetime(memberCoderType)); indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n", scratchChain, rustTypeName(node.blockLengthType), generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)), node.blockLengthType.size());
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"); }
final String rustPrimitiveType = rustTypeName(beginToken.encoding().primitiveType()); writer.append(format("pub struct %s(pub %s);\n", setType, rustPrimitiveType)); writer.append(format("impl %s {\n", setType));
static String generateRustLiteral(final PrimitiveType type, final String value) { Verify.notNull(type, "type"); Verify.notNull(value, "value"); final String typeName = rustTypeName(type); if (typeName == null) { throw new IllegalArgumentException("Unknown Rust type name found for primitive " + type.primitiveName()); } switch (type) { case CHAR: case UINT8: case INT8: case INT16: case UINT16: case INT32: case INT64: case UINT32: case UINT64: return value + typeName; case FLOAT: case DOUBLE: return value.endsWith("NaN") ? typeName + "::NAN" : value + typeName; default: throw new IllegalArgumentException("Unsupported literal generation for type: " + type.primitiveName()); } }
static String generateRustLiteral(final PrimitiveType type, final String value) { Verify.notNull(type, "type"); Verify.notNull(value, "value"); final String typeName = rustTypeName(type); if (typeName == null) { throw new IllegalArgumentException("Unknown Rust type name found for primitive " + type.primitiveName()); } switch (type) { case CHAR: case UINT8: case INT8: case INT16: case UINT16: case INT32: case INT64: case UINT32: case UINT64: return value + typeName; case FLOAT: case DOUBLE: return value.endsWith("NaN") ? typeName + "::NAN" : value + typeName; default: throw new IllegalArgumentException("Unsupported literal generation for type: " + type.primitiveName()); } }
private static void generateEnum(final List<Token> enumTokens, final OutputManager outputManager) throws IOException { final String originalEnumName = enumTokens.get(0).applicableTypeName(); final String enumRustName = formatTypeName(originalEnumName); try (Writer writer = outputManager.createOutput("Enum " + enumRustName)) { final List<Token> messageBody = getMessageBody(enumTokens); if (messageBody.isEmpty()) { throw new IllegalArgumentException("No valid values provided for enum " + originalEnumName); } writer.append("#[derive(Clone,Copy,Debug,PartialEq,Eq,PartialOrd,Ord,Hash)]").append("\n"); final String rustReprTypeName = rustTypeName(messageBody.get(0).encoding().primitiveType()); writer.append(format("#[repr(%s)]", rustReprTypeName)).append("\n"); writer.append("pub enum ").append(enumRustName).append(" {\n"); for (final Token token : messageBody) { final Encoding encoding = token.encoding(); final String literal = generateRustLiteral(encoding.primitiveType(), encoding.constValue().toString()); indent(writer, 1).append(token.name()) .append(" = ") .append(literal) .append(",\n"); } writer.append("}\n"); } }
private static void generateEnum(final List<Token> enumTokens, final OutputManager outputManager) throws IOException { final String originalEnumName = enumTokens.get(0).applicableTypeName(); final String enumRustName = formatTypeName(originalEnumName); try (Writer writer = outputManager.createOutput("Enum " + enumRustName)) { final List<Token> messageBody = getMessageBody(enumTokens); if (messageBody.isEmpty()) { throw new IllegalArgumentException("No valid values provided for enum " + originalEnumName); } writer.append("#[derive(Clone,Copy,Debug,PartialEq,Eq,PartialOrd,Ord,Hash)]").append("\n"); final String rustReprTypeName = rustTypeName(messageBody.get(0).encoding().primitiveType()); writer.append(format("#[repr(%s)]", rustReprTypeName)).append("\n"); writer.append("pub enum ").append(enumRustName).append(" {\n"); for (final Token token : messageBody) { final Encoding encoding = token.encoding(); final String literal = generateRustLiteral(encoding.primitiveType(), encoding.constValue().toString()); indent(writer, 1).append(token.name()) .append(" = ") .append(literal) .append(",\n"); } writer.append("}\n"); } }
final String rustPrimitiveType = RustUtil.rustTypeName(typeToken.encoding().primitiveType()); final String rustFieldType = getRustTypeForPrimitivePossiblyArray(typeToken, rustPrimitiveType); appendable.append(rustFieldType);
final String rustPrimitiveType = RustUtil.rustTypeName(typeToken.encoding().primitiveType()); final String rustFieldType = getRustTypeForPrimitivePossiblyArray(typeToken, rustPrimitiveType); appendable.append(rustFieldType);
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"); }
final String rustPrimitiveType = rustTypeName(beginToken.encoding().primitiveType()); writer.append(format("pub struct %s(pub %s);\n", setType, rustPrimitiveType)); writer.append(format("impl %s {\n", setType));