Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_FIELD_FORMATTER_H_ |
| 6 | #define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_FIELD_FORMATTER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 10 | #include "components/autofill/core/browser/data_model/autofill_profile.h" |
| 11 | #include "components/autofill/core/browser/data_model/credit_card.h" |
sandromaggi | 2fde7d0d | 2021-05-11 06:22:56 | [diff] [blame] | 12 | #include "components/autofill_assistant/browser/action_value.pb.h" |
| 13 | #include "components/autofill_assistant/browser/client_status.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 14 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 15 | |
| 16 | namespace autofill_assistant { |
| 17 | namespace field_formatter { |
| 18 | |
| 19 | // Replaces all placeholder occurrences of the form ${key} in |input| with the |
| 20 | // corresponding value in |mappings|, where |key| is an arbitrary string that |
Clemens Arbesser | 5358d4d | 2020-07-06 14:51:32 | [diff] [blame] | 21 | // does not contain curly braces. If |strict| is true, this will fail if any of |
| 22 | // the found placeholders is not in |mappings|. Otherwise, placeholders other |
| 23 | // than those from |mappings| will be left unchanged. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 24 | absl::optional<std::string> FormatString( |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 25 | const std::string& input, |
Clemens Arbesser | 5358d4d | 2020-07-06 14:51:32 | [diff] [blame] | 26 | const std::map<std::string, std::string>& mappings, |
| 27 | bool strict = true); |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 28 | |
sandromaggi | 2fde7d0d | 2021-05-11 06:22:56 | [diff] [blame] | 29 | // Turns a |value_expression| into a string, replacing |key| chunks with |
| 30 | // corresponding values in |mappings|. This will fail if any of the keys are |
| 31 | // not in |mappings|. If |quote_meta| the replacement pieces will be quoted. |
| 32 | ClientStatus FormatExpression( |
| 33 | const ValueExpression& value_expression, |
| 34 | const std::map<std::string, std::string>& mappings, |
| 35 | bool quote_meta, |
| 36 | std::string* out_value); |
| 37 | |
sandromaggi | b86209d | 2021-05-12 13:57:26 | [diff] [blame] | 38 | // Returns a human-readable string representation of |value_expression| for |
| 39 | // use in logging and error reporting. |
| 40 | std::string GetHumanReadableValueExpression( |
| 41 | const ValueExpression& value_expression); |
| 42 | |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 43 | // Creates a lookup map for all non-empty autofill and custom |
| 44 | // AutofillFormatProto::AutofillAssistantCustomField field types in |
| 45 | // |autofill_data_model|. |
| 46 | // |locale| should be a locale string such as "en-US". |
| 47 | template <typename T> |
| 48 | std::map<std::string, std::string> CreateAutofillMappings( |
| 49 | const T& autofill_data_model, |
| 50 | const std::string& locale); |
| 51 | |
| 52 | } // namespace field_formatter |
sandromaggi | b86209d | 2021-05-12 13:57:26 | [diff] [blame] | 53 | |
| 54 | // Debug output operator for value expressions. The output is only useful in |
| 55 | // debug builds. |
| 56 | std::ostream& operator<<(std::ostream& out, |
| 57 | const ValueExpression& value_expression); |
| 58 | |
Clemens Arbesser | ff4559a4 | 2020-06-11 12:56:54 | [diff] [blame] | 59 | } // namespace autofill_assistant |
| 60 | |
| 61 | #endif // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_FIELD_FORMATTER_H_ |