Struct PythonFormat

Source
pub struct PythonFormat;
Expand description

Format implementation for old-style Python formatting.

Python uses a syntax similar to sprintf in the C language. Each format argument contains two or more characters and has the following components, which must occur in this order:

  1. The '%' character, which marks the start of the specifier.
  2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
  3. Conversion flags (optional), which affect the result of some conversion types.
  4. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
  5. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
  6. Length modifier (optional).
  7. Conversion type.

Most of the conversion types are mapped to the standard Display trait. The %r conversion type is implemented as JSON, if the json feature is active and will otherwise error.

For the full specification, please refer to the Python string formatting docs.

§Example

use dynfmt::{Format, PythonFormat};

let formatted = PythonFormat.format("hello, %s", &["world"]);
assert_eq!("hello, world", formatted.expect("formatting failed"));

Trait Implementations§

Source§

impl Debug for PythonFormat

Source§

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

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

impl<'f> Format<'f> for PythonFormat

Source§

type Iter = PythonIter<'f>

The iterator returned by iter_args.
Source§

fn iter_args(&self, format: &'f str) -> Result<Self::Iter, Error<'f>>

Returns an iterator over format arguments in the format string. Read more
Source§

fn format<A>( &self, format: &'f str, arguments: A, ) -> Result<Cow<'f, str>, Error<'f>>
where A: FormatArgs,

Formats the given string with the specified arguments. Read more

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