kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/file_vname_generator.h (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #ifndef KYTHE_CXX_COMMON_FILE_VNAME_GENERATOR_H_
    18  #define KYTHE_CXX_COMMON_FILE_VNAME_GENERATOR_H_
    19  
    20  #include <memory>
    21  #include <string>
    22  #include <vector>
    23  
    24  #include "absl/status/status.h"
    25  #include "absl/status/statusor.h"
    26  #include "absl/strings/string_view.h"
    27  #include "google/protobuf/io/zero_copy_stream.h"
    28  #include "kythe/proto/storage.pb.h"
    29  #include "re2/re2.h"
    30  
    31  namespace kythe {
    32  
    33  /// \brief Generates file VNames based on user-configurable paths and templates.
    34  class FileVNameGenerator {
    35   public:
    36    explicit FileVNameGenerator() = default;
    37  
    38    /// \brief Adds configuration entries from the json-encoded `json_string`.
    39    /// \param json_string The string containing the configuration to add.
    40    /// \param error_text Non-null. Will be set to text describing any errors.
    41    /// \return false if the string could not be parsed.
    42    bool LoadJsonString(absl::string_view data, std::string* error_text);
    43    absl::Status LoadJsonString(absl::string_view data);
    44  
    45    /// \brief Adds configuration entries from the JSON-encoded list of entries.
    46    /// \param input The input stream containing the JSON entries.
    47    /// \return Non-OK status if the input was invalid.
    48    absl::Status LoadJsonStream(google::protobuf::io::ZeroCopyInputStream& input);
    49  
    50    /// \brief Returns a base VName for a given file path (or an empty VName if
    51    /// no configuration rule matches the path).
    52    kythe::proto::VName LookupBaseVName(absl::string_view path) const;
    53  
    54    /// \brief Returns a VName for the given file path.
    55    kythe::proto::VName LookupVName(absl::string_view path) const;
    56  
    57    /// \brief Sets the default base VName to use when no rules match.
    58    void set_default_base_vname(const kythe::proto::VName& default_vname) {
    59      default_vname_ = default_vname;
    60    }
    61  
    62   private:
    63    /// \brief A rule to apply to certain paths.
    64    struct VNameRule {
    65      /// The pattern used to match against a path.
    66      std::shared_ptr<const re2::RE2> pattern;
    67      /// Substitution pattern used to construct the corpus.
    68      std::string corpus;
    69      /// Substitution pattern used to construct the root.
    70      std::string root;
    71      /// Substitution pattern used to construct the path.
    72      std::string path;
    73    };
    74    /// The rules to apply to incoming paths. The first one to match is used.
    75    std::vector<VNameRule> rules_;
    76    /// The default base VName to use when no rules match.
    77    kythe::proto::VName default_vname_;
    78  };
    79  }  // namespace kythe
    80  
    81  #endif