Struct FormatString

Source
pub struct FormatString { /* private fields */ }
Expand description

A parsed format string that can be used for dynamic formatting.

FormatString represents a string with embedded format specifications that has been parsed and can be used with the dformat! macro to perform dynamic formatting operations. It contains the original string along with information about the format specifications found within it.

§Examples

Basic usage:

use dyf::FormatString;

let fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
assert!(fmt.contains_format());

Creating and using a format string:

use dyf::{FormatString, dformat};

let fmt = FormatString::from_string("{:>10} {:.2}".to_string()).unwrap();
let result = dformat!(&fmt, 42, 3.14159).unwrap();
assert_eq!(result, "        42 3.14");

Converting between string types:

use dyf::FormatString;

let fmt = FormatString::from_string("Value: {:05}".to_string()).unwrap();
let fmt_str = fmt.to_string_lossy();
let owned_str = fmt.into_string();

Implementations§

Source§

impl FormatString

Source

pub fn from_string(s: String) -> Result<Self, Error>

Creates a new FormatString from a string.

§Arguments
  • s - The string containing format specifications
§Returns

A Result containing the parsed FormatString or an error if parsing fails.

§Errors

This function may return an error if the input string contains invalid format specifications that cannot be parsed.

§Examples
use dyf::FormatString;

let fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
Source

pub fn into_string(self) -> String

Converts the FormatString into its inner string.

This consumes the FormatString and returns the original string that was used to create it.

§Examples
use dyf::FormatString;

let fmt = FormatString::from_string("Test: {}".to_string()).unwrap();
let s = fmt.into_string();
assert_eq!(s, "Test: {}");
Source

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

Returns a borrowed version of the format string.

This provides access to the original string without consuming the FormatString.

§Examples
use dyf::FormatString;

let fmt = FormatString::from_string("Value: {:.2}".to_string()).unwrap();
let borrowed = fmt.to_string_lossy();
assert_eq!(&*borrowed, "Value: {:.2}");
Source

pub fn contains_format(&self) -> bool

Returns true if the format string contains any format specifications.

§Examples
use dyf::FormatString;

let with_fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
assert!(with_fmt.contains_format());

let without_fmt = FormatString::from_string("Hello, world!".to_string()).unwrap();
assert!(!without_fmt.contains_format());

Trait Implementations§

Source§

impl Clone for FormatString

Source§

fn clone(&self) -> FormatString

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 Debug for FormatString

Source§

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

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

impl<'s> From<&'s FormatString> for Formatter<'s>

Source§

fn from(value: &'s FormatString) -> Self

Converts to this type from the input type.

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