<commit_before>/*
 * Copyright 2013-2021 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.sbe.generation.rust;

import org.agrona.generation.OutputManager;
import uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType;

import java.io.IOException;
import java.io.Writer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;

import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.*;
import static uk.co.real_logic.sbe.generation.rust.RustUtil.*;

/**
 * Generates `lib.rs` specific code.
 */
class LibRsDef
{
    private final LinkedHashMap<String, HashSet<CodecType>> modules = new LinkedHashMap<>();
    private final ArrayList<String> enumDefs = new ArrayList<>();
    private final ArrayList<String> bitSetDefs = new ArrayList<>();

    private final OutputManager outputManager;
    private final ByteOrder byteOrder;

    /**
     * Create a new 'lib.rs' for the library being generated
     *
     * @param outputManager for generating the codecs to.
     * @param byteOrder for the Encoding.
     */
    LibRsDef(
        final OutputManager outputManager,
        final ByteOrder byteOrder)
    {
        this.outputManager = outputManager;
        this.byteOrder = byteOrder;
    }

    void addMod(final String modName, final CodecType codecType)
    {
        modules.computeIfAbsent(modName, __ -> new HashSet<>()).add(codecType);
    }

    void addEnum(final String enumDef)
    {
        enumDefs.add(enumDef);
    }

    void addBitSet(final String bitSetDef)
    {
        bitSetDefs.add(bitSetDef);
    }

    void generate() throws IOException
    {
        try (Writer libRs = outputManager.createOutput("lib"))
        {
            indent(libRs, 0, "#![allow(clippy::upper_case_acronyms)]\n");
            indent(libRs, 0, "#![allow(non_camel_case_types)]\n");
            indent(libRs, 0, "use core::{convert::TryInto};\n\n");

            // add modules
            for (final String mod : modules.keySet())
            {
                indent(libRs, 0, "pub mod %s;\n", toLowerSnakeCase(mod));
            }
            indent(libRs, 0, "\n");

            // add re-export of modules
            for (final Map.Entry<String, HashSet<CodecType>> entry : modules.entrySet())
            {
                final String mod = entry.getKey();
                final HashSet<CodecType> codecTypes = entry.getValue();

                if (codecTypes.size() == 1)
                {
                    indent(libRs, 0, "pub use %s::%s::*;\n",
                        toLowerSnakeCase(mod),
                        toLowerSnakeCase(codecTypes.toArray()[0].toString()));<commit_msg>[Rust] lib.rs will be generated with "#![forbid(unsafe_code)]"<commit_after>/*
 * Copyright 2013-2021 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.sbe.generation.rust;

import org.agrona.generation.OutputManager;
import uk.co.real_logic.sbe.generation.rust.RustGenerator.CodecType;

import java.io.IOException;
import java.io.Writer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;

import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static uk.co.real_logic.sbe.generation.rust.RustGenerator.*;
import static uk.co.real_logic.sbe.generation.rust.RustUtil.*;

/**
 * Generates `lib.rs` specific code.
 */
class LibRsDef
{
    private final LinkedHashMap<String, HashSet<CodecType>> modules = new LinkedHashMap<>();
    private final ArrayList<String> enumDefs = new ArrayList<>();
    private final ArrayList<String> bitSetDefs = new ArrayList<>();

    private final OutputManager outputManager;
    private final ByteOrder byteOrder;

    /**
     * Create a new 'lib.rs' for the library being generated
     *
     * @param outputManager for generating the codecs to.
     * @param byteOrder for the Encoding.
     */
    LibRsDef(
        final OutputManager outputManager,
        final ByteOrder byteOrder)
    {
        this.outputManager = outputManager;
        this.byteOrder = byteOrder;
    }

    void addMod(final String modName, final CodecType codecType)
    {
        modules.computeIfAbsent(modName, __ -> new HashSet<>()).add(codecType);
    }

    void addEnum(final String enumDef)
    {
        enumDefs.add(enumDef);
    }

    void addBitSet(final String bitSetDef)
    {
        bitSetDefs.add(bitSetDef);
    }

    void generate() throws IOException
    {
        try (Writer libRs = outputManager.createOutput("lib"))
        {
            indent(libRs, 0, "#![forbid(unsafe_code)]\n");
            indent(libRs, 0, "#![allow(clippy::upper_case_acronyms)]\n");
            indent(libRs, 0, "#![allow(non_camel_case_types)]\n");
            indent(libRs, 0, "use core::{convert::TryInto};\n\n");

            // add modules
            for (final String mod : modules.keySet())
            {
                indent(libRs, 0, "pub mod %s;\n", toLowerSnakeCase(mod));
            }
            indent(libRs, 0, "\n");

            // add re-export of modules
            for (final Map.Entry<String, HashSet<CodecType>> entry : modules.entrySet())
            {
                final String mod = entry.getKey();
                final HashSet<CodecType> codecTypes = entry.getValue();

                if (codecTypes.size() == 1)
                {
                    indent(libRs, 0, "pub use %s::%s::*;\n",
                        toLowerSnakeCase(mod),
                        toLowerSnakeCase(codecTypes.toArray()[0].toString()));
{'fraction_non_alphanumeric': 0.11207914943886592, 'fraction_numerical': 0.006054341405788541, 'mean_word_length': 2.709200438116101, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232243', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1922, 'n_words': 532}