1 // Copyright (C) 2014 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <libaddressinput/address_metadata.h>
16
17 #include <libaddressinput/address_field.h>
18
19 #include <algorithm>
20 #include <string>
21
22 #include "format_element.h"
23 #include "region_data_constants.h"
24 #include "rule.h"
25
26 namespace i18n {
27 namespace addressinput {
28
IsFieldRequired(AddressField field,const std::string & region_code)29 bool IsFieldRequired(AddressField field, const std::string& region_code) {
30 if (field == COUNTRY) {
31 return true;
32 }
33
34 Rule rule;
35 rule.CopyFrom(Rule::GetDefault());
36 if (!rule.ParseSerializedRule(
37 RegionDataConstants::GetRegionData(region_code))) {
38 return false;
39 }
40
41 return std::find(rule.GetRequired().begin(),
42 rule.GetRequired().end(),
43 field) != rule.GetRequired().end();
44 }
45
IsFieldUsed(AddressField field,const std::string & region_code)46 bool IsFieldUsed(AddressField field, const std::string& region_code) {
47 if (field == COUNTRY) {
48 return true;
49 }
50
51 Rule rule;
52 rule.CopyFrom(Rule::GetDefault());
53 if (!rule.ParseSerializedRule(
54 RegionDataConstants::GetRegionData(region_code))) {
55 return false;
56 }
57
58 return std::find(rule.GetFormat().begin(),
59 rule.GetFormat().end(),
60 FormatElement(field)) != rule.GetFormat().end();
61 }
62
63 } // namespace addressinput
64 } // namespace i18n
65