blob: 4f14be911754c4177ae923d9106b71c6f1ace515 [file] [log] [blame]
Clemens Arbesserff4559a42020-06-11 12:56:541// 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 Arbesserff4559a42020-06-11 12:56:5410#include "components/autofill/core/browser/data_model/autofill_profile.h"
11#include "components/autofill/core/browser/data_model/credit_card.h"
sandromaggi2fde7d0d2021-05-11 06:22:5612#include "components/autofill_assistant/browser/action_value.pb.h"
13#include "components/autofill_assistant/browser/client_status.h"
Anton Bikineev1156b5f2021-05-15 22:35:3614#include "third_party/abseil-cpp/absl/types/optional.h"
Clemens Arbesserff4559a42020-06-11 12:56:5415
16namespace autofill_assistant {
17namespace 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 Arbesser5358d4d2020-07-06 14:51:3221// 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 Bikineev1156b5f2021-05-15 22:35:3624absl::optional<std::string> FormatString(
Clemens Arbesserff4559a42020-06-11 12:56:5425 const std::string& input,
Clemens Arbesser5358d4d2020-07-06 14:51:3226 const std::map<std::string, std::string>& mappings,
27 bool strict = true);
Clemens Arbesserff4559a42020-06-11 12:56:5428
sandromaggi2fde7d0d2021-05-11 06:22:5629// 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.
32ClientStatus 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
sandromaggib86209d2021-05-12 13:57:2638// Returns a human-readable string representation of |value_expression| for
39// use in logging and error reporting.
40std::string GetHumanReadableValueExpression(
41 const ValueExpression& value_expression);
42
Clemens Arbesserff4559a42020-06-11 12:56:5443// 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".
47template <typename T>
48std::map<std::string, std::string> CreateAutofillMappings(
49 const T& autofill_data_model,
50 const std::string& locale);
51
52} // namespace field_formatter
sandromaggib86209d2021-05-12 13:57:2653
54// Debug output operator for value expressions. The output is only useful in
55// debug builds.
56std::ostream& operator<<(std::ostream& out,
57 const ValueExpression& value_expression);
58
Clemens Arbesserff4559a42020-06-11 12:56:5459} // namespace autofill_assistant
60
61#endif // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_FIELD_FORMATTER_H_