FieldText

Struct FieldText 

Source
pub struct FieldText<'string>(/* private fields */);
Expand description

A wrapper for &str that is checked to be printable ASCII, which is defined as not containing control characters in RFC8907 section 3.7.

This type implements TryFrom<&str> and TryFrom<&[u8]>; in both cases, an invalid argument will be returned as an Err variant.

§Examples

Conversions from &str:

use tacacs_plus_protocol::FieldText;

let valid_ascii = "a string";
assert!(FieldText::try_from(valid_ascii).is_ok());

let beyond_ascii = "💀";
assert!(FieldText::try_from(beyond_ascii).is_err());

Conversions from &[u8]:


let valid_slice = b"this is (almost) a string";
assert!(FieldText::try_from(valid_slice.as_slice()).is_ok());

let not_printable = b"all ASCII characters with - oh no! - a\ttab";
assert!(FieldText::try_from(not_printable.as_slice()).is_err());

let invalid_utf8 = [0x80]; // where'd the rest of the codepoint go?
assert!(FieldText::try_from(invalid_utf8.as_slice()).is_err());

If the std feature is enabled, the FieldText::from_string_lossy() constructor is also available in case a .try_into().unwrap() is undesirable:

let already_valid = "all ASCII!";
let valid_text = FieldText::from_string_lossy(String::from(already_valid));
assert_eq!(valid_text, already_valid);

let unicode_fun = "\tsome chars and ✨emojis✨ (and a quote: ')";
let escaped_text = FieldText::from_string_lossy(String::from(unicode_fun));
assert_eq!(escaped_text, "\\tsome chars and \\u{2728}emojis\\u{2728} (and a quote: ')");

// now that escaped_text is valid ASCII, a .try_into().unwrap() should be guaranteed
// not to panic with the escaped string
let _: FieldText<'_> = escaped_text.as_ref().try_into().unwrap();

Implementations§

Source§

impl FieldText<'_>

Source

pub fn from_string_lossy(string: String) -> FieldText<'static>

Available on crate feature std only.

Creates a FieldText from a String, escaping any non-printable-ASCII characters as necessary.

Source

pub fn into_owned(self) -> FieldText<'static>

Available on crate feature std only.

Converts this FieldText to one that owns its underlying data, extending its lifetime to 'static.

Source§

impl<'string> FieldText<'string>

Source

pub fn len(&self) -> usize

Gets the length of the underlying &str.

Source

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

Gets the byte slice representation of the underlying &str.

Source

pub fn is_empty(&self) -> bool

Returns true if the underlying &str is empty.

Source

pub fn contains_any(&self, characters: &[char]) -> bool

Returns true if the underlying &str contains any of the provided characters, or false otherwise.

Trait Implementations§

Source§

impl AsRef<str> for FieldText<'_>

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'string> Clone for FieldText<'string>

Source§

fn clone(&self) -> FieldText<'string>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'string> Debug for FieldText<'string>

Source§

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

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

impl<'string> Default for FieldText<'string>

Source§

fn default() -> FieldText<'string>

Returns the “default value” for a type. Read more
Source§

impl Display for FieldText<'_>

Source§

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

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

impl FromStr for FieldText<'static>

Available on crate feature std only.
Source§

type Err = InvalidText<String>

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

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

Parses a string s to return a value of this type. Read more
Source§

impl<'string> Hash for FieldText<'string>

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<'string> Ord for FieldText<'string>

Source§

fn cmp(&self, other: &FieldText<'string>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl PartialEq<&str> for FieldText<'_>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<FieldText<'_>> for &str

Source§

fn eq(&self, other: &FieldText<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'string> PartialEq for FieldText<'string>

Source§

fn eq(&self, other: &FieldText<'string>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'string> PartialOrd for FieldText<'string>

Source§

fn partial_cmp(&self, other: &FieldText<'string>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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<'bytes> TryFrom<&'bytes [u8]> for FieldText<'bytes>

Source§

type Error = InvalidText<&'bytes [u8]>

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

fn try_from(value: &'bytes [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'string> TryFrom<&'string str> for FieldText<'string>

Source§

type Error = InvalidText<&'string str>

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

fn try_from(value: &'string str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for FieldText<'_>

Available on crate feature std only.
Source§

type Error = InvalidText<String>

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'string> Eq for FieldText<'string>

Source§

impl<'string> StructuralPartialEq for FieldText<'string>

Auto Trait Implementations§

§

impl<'string> Freeze for FieldText<'string>

§

impl<'string> RefUnwindSafe for FieldText<'string>

§

impl<'string> Send for FieldText<'string>

§

impl<'string> Sync for FieldText<'string>

§

impl<'string> Unpin for FieldText<'string>

§

impl<'string> UnwindSafe for FieldText<'string>

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<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> 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> Same for T

Source§

type Output = T

Should always be Self
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.