Skip to main content

CountryCode

Struct CountryCode 

Source
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

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

Source

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 assigned
Source

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"));
Source

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());
Source

pub fn as_bytes(&self) -> &[u8; 2]

Returns the two raw ASCII bytes backing this code (for example, b"US").

§Examples
use ftracker_identifiers::CountryCode;

let code = CountryCode::parse("US").unwrap();
assert_eq!(code.as_bytes(), b"US");
Source

pub fn as_str(&self) -> &str

Returns the two character country code as a &str.

This never allocates. The bytes are guaranteed to be valid ASCII by construction.

§Examples
use ftracker_identifiers::CountryCode;

let code = CountryCode::parse("US").unwrap();
assert_eq!(code.as_str(), "US");

Trait Implementations§

Source§

impl<'a> Arbitrary<'a> for CountryCode

Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl AsRef<[u8]> for CountryCode

Source§

fn as_ref(&self) -> &[u8]

Equivalent to CountryCode::as_bytes, borrowed as a slice.

Source§

impl AsRef<str> for CountryCode

Source§

fn as_ref(&self) -> &str

Equivalent to CountryCode::as_str.

Source§

impl Clone for CountryCode

Source§

fn clone(&self) -> CountryCode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CountryCode

Source§

impl Debug for CountryCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for CountryCode

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for CountryCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for CountryCode

Source§

impl FromStr for CountryCode

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Delegates to CountryCode::parse, enabling input.parse::<CountryCode>() and use in generic code bounded by FromStr.

Source§

type Err = CountryCodeError

The associated error which can be returned from parsing.
Source§

impl Hash for CountryCode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl JsonSchema for CountryCode

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn json_schema(_: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

impl Ord for CountryCode

Source§

fn cmp(&self, other: &CountryCode) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for CountryCode

Source§

fn eq(&self, other: &CountryCode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<&str> for CountryCode

Source§

fn eq(&self, other: &&str) -> bool

Compares against a string slice by its canonical two letter representation.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<CountryCode> for str

Source§

fn eq(&self, other: &CountryCode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<CountryCode> for &str

Source§

fn eq(&self, other: &CountryCode) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<str> for CountryCode

Source§

fn eq(&self, other: &str) -> bool

Compares against a string slice by its canonical two letter representation.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for CountryCode

Source§

fn partial_cmp(&self, other: &CountryCode) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for CountryCode

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CountryCode

Source§

impl TryFrom<&[u8]> for CountryCode

Source§

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

The type returned in the event of a conversion error.
Source§

impl TryFrom<&str> for CountryCode

Source§

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>.

Source§

type Error = CountryCodeError

The type returned in the event of a conversion error.
Source§

impl TryFrom<[u8; 2]> for CountryCode

Source§

fn try_from(value: [u8; 2]) -> Result<Self, Self::Error>

Delegates to CountryCode::from_bytes. The two bytes must already be pre normalized uppercase ASCII letters.

Source§

type Error = CountryCodeError

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V