Skip to main content

StringValue

Enum StringValue 

Source
pub enum StringValue<T: StringValueData> {
    Raw(String),
    Parsed(T),
}
Expand description

A string value that can either be stored as a raw unparsed string or as a parsed value of type T.

The type T must implement the StringValueData trait, which defines how to parse and serialize the value. This type is used whenever an EML_NL document element or attribute contains a string value that could be parsed, but where strict parsing is not always desired.

Depending on the parsing mode used when parsing a document, StringValue instances may either contain the raw string or the parsed value. When EMLParsingMode::Strict is used, all StringValue instances will contain the parsed value. When EMLParsingMode::StrictFallback is used, the library will attempt to parse values but will fall back to storing the raw string if parsing fails. When EMLParsingMode::Loose is used, the library will not even attempt to parse values and will store all values as raw strings.

In most cases you will want to use the parsed value by using one of the value retrieving methods:

  • StringValue::value: Get a Cow containing either a borrowed reference to the parsed value or an owned parsed value if the StringValue contains a raw string. If the StringValue contains a raw string and parsing fails, the error from the parsing attempt will be returned as an error.
  • StringValue::copied_value: Same as value, but only available for types that implement Copy and returns a copy of the value instead.
  • StringValue::cloned_value: Same as value, but returns a cloned copy of the value instead of a reference. Only available for types that implement Clone.

Variants§

§

Raw(String)

A raw unparsed string value that potentially can be parsed into a value of type T.

§

Parsed(T)

Parsed value of type T.

Implementations§

Source§

impl<T: StringValueData> StringValue<T>

Source

pub fn from_raw_parsed(s: impl AsRef<str>) -> Result<Self, T::Error>

Try to create a StringValue from the given raw string by parsing it.

Source

pub fn from_raw(s: impl Into<String>) -> Self

Create a StringValue from a raw string.

Source

pub fn from_value(v: T) -> Self

Create a StringValue from a parsed value.

Source

pub fn raw(&self) -> Cow<'_, str>

Get the raw string value.

Source

pub fn value_err(&self) -> Result<Cow<'_, T>, T::Error>

Get the parsed value, returning any possible parsing errors.

Source

pub fn cloned_value_err(&self) -> Result<T, T::Error>

Get the parsed value, returning a cloned copy of it.

If there is an error, the original parsing error will be returned.

Source

pub fn cloned_value(&self) -> Result<T, EMLError>

Get the parsed value, returning a cloned copy of it.

If there is an error, it will be wrapped in an EMLError.

Source

pub fn value(&self) -> Result<Cow<'_, T>, EMLError>

Get the parsed value, returning any possible parsing errors as an EMLError.

The element_name and span parameters are used to provide context in the error if parsing fails.

Source§

impl<T: StringValueData + Copy> StringValue<T>

Source

pub fn copied_value_err(&self) -> Result<T, T::Error>

Get the parsed value, returning a copy of it.

This is only available for types that implement Copy.

Source

pub fn copied_value(&self) -> Result<T, EMLError>

Get the parsed value, returning a copy of it.

If there is an error, it will be wrapped in an EMLError. This is only available for types that implement Copy.

Trait Implementations§

Source§

impl<T: Clone + StringValueData> Clone for StringValue<T>

Source§

fn clone(&self) -> StringValue<T>

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<T: Debug + StringValueData> Debug for StringValue<T>

Source§

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

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

impl<T: Hash + StringValueData> Hash for StringValue<T>

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<T: PartialEq + StringValueData> PartialEq for StringValue<T>

Source§

fn eq(&self, other: &StringValue<T>) -> 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<T: Eq + StringValueData> Eq for StringValue<T>

Source§

impl<T: StringValueData> StructuralPartialEq for StringValue<T>

Auto Trait Implementations§

§

impl<T> Freeze for StringValue<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StringValue<T>
where T: RefUnwindSafe,

§

impl<T> Send for StringValue<T>
where T: Send,

§

impl<T> Sync for StringValue<T>
where T: Sync,

§

impl<T> Unpin for StringValue<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StringValue<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for StringValue<T>
where T: UnwindSafe,

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