• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef GOOGLE_PROTOBUF_COMPILER_RUST_RELATIVE_PATH_H__
9 #define GOOGLE_PROTOBUF_COMPILER_RUST_RELATIVE_PATH_H__
10 
11 #include <string>
12 #include <vector>
13 
14 #include "absl/algorithm/container.h"
15 #include "absl/log/absl_check.h"
16 #include "absl/strings/match.h"
17 #include "absl/strings/string_view.h"
18 
19 namespace google {
20 namespace protobuf {
21 namespace compiler {
22 namespace rust {
23 
24 // Relative path using '/' as a separator.
25 class RelativePath final {
26  public:
RelativePath(absl::string_view path)27   explicit RelativePath(absl::string_view path) : path_(path) {
28     ABSL_CHECK(!absl::StartsWith(path, "/"))
29         << "only relative paths are supported";
30     // `..` and `.` not supported, since there's no use case for that right now.
31     for (absl::string_view segment : Segments()) {
32       ABSL_CHECK(segment != "..") << "`..` segments are not supported";
33       ABSL_CHECK(segment != ".") << "`.` segments are not supported";
34     }
35   }
36 
37   // Returns a path getting us from the current relative path to the `dest`
38   // path.
39   //
40   // Supports both files and directories.
41   std::string Relative(const RelativePath& dest) const;
42   std::vector<absl::string_view> Segments() const;
43   bool IsDirectory() const;
44 
45  private:
46   absl::string_view path_;
47 };
48 
49 }  // namespace rust
50 }  // namespace compiler
51 }  // namespace protobuf
52 }  // namespace google
53 
54 #endif  // GOOGLE_PROTOBUF_COMPILER_RUST_RELATIVE_PATH_H__
55