Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style license that can be |
| 3 | // found in the LICENSE file or at https://opensource.org/licenses/MIT. |
| 4 | |
| 5 | #ifndef THIRD_PARTY_LIBURLPATTERN_PARSE_H_ |
| 6 | #define THIRD_PARTY_LIBURLPATTERN_PARSE_H_ |
| 7 | |
Ben Kelly | d18c0e82 | 2021-03-05 22:42:17 | [diff] [blame] | 8 | #include <functional> |
Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 9 | #include "base/component_export.h" |
| 10 | #include "third_party/abseil-cpp/absl/status/statusor.h" |
| 11 | #include "third_party/abseil-cpp/absl/strings/string_view.h" |
Ben Kelly | 36f7ba3 | 2020-11-24 19:48:46 | [diff] [blame] | 12 | #include "third_party/liburlpattern/options.h" |
Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 13 | |
Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 14 | namespace liburlpattern { |
| 15 | |
| 16 | class Pattern; |
| 17 | |
Ben Kelly | d18c0e82 | 2021-03-05 22:42:17 | [diff] [blame] | 18 | // Define a functor-style callback that will be invoked synchronously by the |
| 19 | // Parse() method. It will be called for each part of the pattern consisting |
| 20 | // of text to match strictly against an input. For example, for the pattern: |
| 21 | // |
| 22 | // `/foo/:bar.html` |
| 23 | // |
| 24 | // The callback will be invoked with `/foo`, `/`, and `.html` separately. |
| 25 | // |
| 26 | // The callback should validate the input and potentially perform any encoding |
| 27 | // necessary. For example, some characters could be percent encoded. The |
| 28 | // final encoded value for the input should be returned. |
| 29 | typedef std::function<absl::StatusOr<std::string>(absl::string_view)> |
| 30 | EncodeCallback; |
| 31 | |
Dan McArdle | b1b9269 | 2021-10-28 13:57:24 | [diff] [blame] | 32 | // Parse a pattern string and return the result. The parse will fail if the |
| 33 | // input |pattern| is not valid UTF-8. Currently only group names may actually |
| 34 | // contain non-ASCII characters, however. Unicode characters in other parts of |
| 35 | // the pattern will cause an error to be returned. A |callback| must be |
Ben Kelly | d18c0e82 | 2021-03-05 22:42:17 | [diff] [blame] | 36 | // provided to validate and encode plain text parts of the pattern. An |
| 37 | // |options| value may be provided to override default behavior. |
Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 38 | COMPONENT_EXPORT(LIBURLPATTERN) |
Ben Kelly | 0e5c63e8 | 2020-11-12 21:24:08 | [diff] [blame] | 39 | absl::StatusOr<Pattern> Parse(absl::string_view pattern, |
Ben Kelly | d18c0e82 | 2021-03-05 22:42:17 | [diff] [blame] | 40 | EncodeCallback callback, |
Ben Kelly | 36f7ba3 | 2020-11-24 19:48:46 | [diff] [blame] | 41 | const Options& options = Options()); |
Ben Kelly | ad8343ba | 2020-10-28 18:48:53 | [diff] [blame] | 42 | |
| 43 | } // namespace liburlpattern |
| 44 | |
| 45 | #endif // THIRD_PARTY_LIBURLPATTERN_PARSE_H_ |