pub struct CountryCode { /* private fields */ }Expand description
A validated ISO 3166-1 alpha-2 country code.
CountryCode is a two byte, Copy, allocation free value object. Once constructed, it is
guaranteed to be a code that ISO 3166-1 officially assigns. There is no way to get a
CountryCode that has not passed validation.
Internally, the code is stored as two raw uppercase ASCII letters ('A'..='Z').
§Constructing a CountryCode
CountryCode::parseandCountryCode::newaccept two character strings, in any ASCII case, trimmed of surrounding whitespace.CountryCode::from_bytesaccepts exactly two pre normalized uppercase ASCII bytes.FromStrandTryFrom<&str>behave likeparse, for use in generic code.
All of them run the same validation and return CountryCodeError on failure. See the
module level documentation for the validation rules and design rationale.
Implementations§
Source§impl CountryCode
impl CountryCode
Sourcepub fn parse(input: &str) -> Result<Self, CountryCodeError>
pub fn parse(input: &str) -> Result<Self, CountryCodeError>
Parses a country code from a string.
The parser trims surrounding whitespace and folds ASCII letters to uppercase before
validation. This is the primary constructor. CountryCode::new, FromStr, and
TryFrom<&str> all delegate to it.
§Errors
Returns CountryCodeError if the input is empty, does not contain exactly two characters
after trimming, contains a non letter character, or names a code that ISO 3166-1 does not
officially assign.
§Examples
use ftracker_identifiers::CountryCode;
assert!(CountryCode::parse("US").is_ok());
assert!(CountryCode::parse("us").is_ok()); // lowercase is folded automatically
assert!(CountryCode::parse(" BR ").is_ok()); // surrounding whitespace is trimmed
assert!(CountryCode::parse("ZZ").is_err()); // well formed but not assignedSourcepub fn new(input: &str) -> Result<Self, CountryCodeError>
pub fn new(input: &str) -> Result<Self, CountryCodeError>
Alias for CountryCode::parse.
§Errors
See CountryCode::parse.
§Examples
use ftracker_identifiers::CountryCode;
assert_eq!(CountryCode::new("US"), CountryCode::parse("US"));Sourcepub fn from_bytes(bytes: [u8; 2]) -> Result<Self, CountryCodeError>
pub fn from_bytes(bytes: [u8; 2]) -> Result<Self, CountryCodeError>
Constructs a CountryCode directly from two raw ASCII bytes.
Each byte must already be an uppercase letter. Use CountryCode::parse if the input might
contain surrounding whitespace or lowercase letters.
§Errors
Returns CountryCodeError under the same conditions as CountryCode::parse, except that
length is guaranteed by the [u8; 2] type itself: CountryCodeError::InvalidLength cannot
occur here.
§Examples
use ftracker_identifiers::CountryCode;
let code = CountryCode::from_bytes(*b"US").unwrap();
assert_eq!(code.as_str(), "US");
// A well formed but unassigned code is rejected just like it would be through `parse`.
assert!(CountryCode::from_bytes(*b"ZZ").is_err());Trait Implementations§
Source§impl<'a> Arbitrary<'a> for CountryCode
impl<'a> Arbitrary<'a> for CountryCode
Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl AsRef<[u8]> for CountryCode
impl AsRef<[u8]> for CountryCode
Source§impl AsRef<str> for CountryCode
impl AsRef<str> for CountryCode
Source§fn as_ref(&self) -> &str
fn as_ref(&self) -> &str
Equivalent to CountryCode::as_str.
Source§impl Clone for CountryCode
impl Clone for CountryCode
Source§fn clone(&self) -> CountryCode
fn clone(&self) -> CountryCode
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for CountryCode
Source§impl Debug for CountryCode
impl Debug for CountryCode
Source§impl<'de> Deserialize<'de> for CountryCode
impl<'de> Deserialize<'de> for CountryCode
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for CountryCode
impl Display for CountryCode
impl Eq for CountryCode
Source§impl FromStr for CountryCode
impl FromStr for CountryCode
Source§impl Hash for CountryCode
impl Hash for CountryCode
Source§impl JsonSchema for CountryCode
impl JsonSchema for CountryCode
Source§fn json_schema(_: &mut SchemaGenerator) -> Schema
fn json_schema(_: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl Ord for CountryCode
impl Ord for CountryCode
Source§fn cmp(&self, other: &CountryCode) -> Ordering
fn cmp(&self, other: &CountryCode) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for CountryCode
impl PartialEq for CountryCode
Source§impl PartialEq<&str> for CountryCode
impl PartialEq<&str> for CountryCode
Source§impl PartialEq<CountryCode> for str
impl PartialEq<CountryCode> for str
Source§impl PartialEq<CountryCode> for &str
impl PartialEq<CountryCode> for &str
Source§impl PartialEq<str> for CountryCode
impl PartialEq<str> for CountryCode
Source§impl PartialOrd for CountryCode
impl PartialOrd for CountryCode
Source§impl Serialize for CountryCode
impl Serialize for CountryCode
impl StructuralPartialEq for CountryCode
Source§impl TryFrom<&[u8]> for CountryCode
impl TryFrom<&[u8]> for CountryCode
Source§fn try_from(value: &[u8]) -> Result<Self, Self::Error>
fn try_from(value: &[u8]) -> Result<Self, Self::Error>
Validates a byte slice as a country code. The slice must be exactly two pre normalized
uppercase ASCII bytes; any other length yields CountryCodeError::InvalidLength. Once the
length is confirmed, this behaves like CountryCode::from_bytes.
Source§type Error = CountryCodeError
type Error = CountryCodeError
Source§impl TryFrom<&str> for CountryCode
impl TryFrom<&str> for CountryCode
Source§fn try_from(value: &str) -> Result<Self, Self::Error>
fn try_from(value: &str) -> Result<Self, Self::Error>
Delegates to CountryCode::parse, enabling CountryCode::try_from(input) and use in
generic code bounded by TryFrom<&str>.