Skip to main content

proto_types/common/
postal_address.rs

1use crate::common::PostalAddress;
2
3impl PostalAddress {
4	/// Checks if this [`PostalAddress`]'s `region_code` is empty. If it is, it means that the instance is invalid.
5	#[must_use]
6	#[inline]
7	pub const fn has_region_code(&self) -> bool {
8		!self.region_code.is_empty()
9	}
10
11	/// Checks if the `region_code` of this address matches the given `code`.
12	/// The `code` should be a CLDR region code (ISO 3166-1 alpha-2, e.g., "US", "CH").
13	#[must_use]
14	#[inline]
15	pub fn matches_region_code(&self, code: &str) -> bool {
16		self.region_code == code
17	}
18
19	/// Checks if the `language_code` of this address matches the given BCP-47 `code`.
20	/// The `code` should be a BCP-44 language tag (e.g., "en-US", "ja").
21	#[must_use]
22	#[inline]
23	pub fn has_language_code(&self, code: &str) -> bool {
24		self.language_code == code
25	}
26
27	/// Checks if the `postal_code` of this address matches the given `code`.
28	#[must_use]
29	#[inline]
30	pub fn has_postal_code(&self, code: &str) -> bool {
31		self.postal_code == code
32	}
33
34	/// Checks if the `sorting_code` of this address matches the given `code`.
35	#[must_use]
36	#[inline]
37	pub fn has_sorting_code(&self, code: &str) -> bool {
38		self.sorting_code == code
39	}
40
41	/// Checks if the `administrative_area` of this address matches the given `name`.
42	#[must_use]
43	#[inline]
44	pub fn has_administrative_area(&self, name: &str) -> bool {
45		self.administrative_area == name
46	}
47
48	/// Checks if the `locality` (city/town) of this address matches the given `name`.
49	#[must_use]
50	#[inline]
51	pub fn has_locality(&self, name: &str) -> bool {
52		self.locality == name
53	}
54
55	/// Checks if the `sublocality` of this address matches the given `name`.
56	#[must_use]
57	#[inline]
58	pub fn has_sublocality(&self, name: &str) -> bool {
59		self.sublocality == name
60	}
61}