Enum FmtType

Source
pub enum FmtType {
    Default,
    Debug,
    DebugLowHex,
    DebugUpHex,
    LowerHex,
    UpperHex,
    Octal,
    Ptr,
    Bin,
    LowExp,
    UpperExp,
}
Expand description

Specifies the type of formatting to apply to a value.

The FmtType enum represents the various format types that can be specified in a format string after the colon. These determine how values are converted to strings, including different representations for numbers, debugging output, and other special formats.

§Format String Representation

In format strings, these types are represented as follows:

FmtTypeFormat SpecifierDescription
Default(none)Default formatting for the type
Debug?Debug representation
DebugLowHexx?Debug representation with lowercase hexadecimal
DebugUpHexX?Debug representation with uppercase hexadecimal
LowerHexxLowercase hexadecimal
UpperHexXUppercase hexadecimal
OctaloOctal representation
PtrpPointer address
BinbBinary representation
LowExpeLowercase exponential notation
UpperExpEUppercase exponential notation

§Examples

Basic usage with format specifications:

use dyf::{FmtType, FormatSpec};

// Create a format specification for hexadecimal output
let hex_spec = FormatSpec {
    ty: FmtType::LowerHex,
    ..Default::default()
};

// Create a format specification for debug output
let debug_spec = FormatSpec {
    ty: FmtType::Debug,
    ..Default::default()
};

Using with custom formatting:

use dyf::{FmtType, FormatSpec, DynDisplay, Error};

struct Color {
    r: u8,
    g: u8,
    b: u8,
}

impl DynDisplay for Color {
    fn dyn_fmt(&self, spec: &FormatSpec) -> Result<String, Error> {
        match spec.ty {
            FmtType::LowerHex => Ok(format!(
                "#{:02x}{:02x}{:02x}",
                self.r, self.g, self.b
            )),
            FmtType::UpperHex => Ok(format!(
                "#{:02X}{:02X}{:02X}",
                self.r, self.g, self.b
            )),
            FmtType::Debug => Ok(format!(
                "Color {{ r: {}, g: {}, b: {} }}",
                self.r, self.g, self.b
            )),
            _ => Ok(format!(
                "RGB({}, {}, {})",
                self.r, self.g, self.b
            )),
        }
    }
}

Variants§

§

Default

Default formatting for the type.

This uses the standard display formatting for the type, equivalent to not specifying a format type in the format string.

§

Debug

Debug representation.

This uses the debug formatting for the type, equivalent to the {:?} format specifier.

§

DebugLowHex

Debug representation with lowercase hexadecimal.

This combines debug formatting with lowercase hexadecimal representation.

§

DebugUpHex

Debug representation with uppercase hexadecimal.

This combines debug formatting with uppercase hexadecimal representation.

§

LowerHex

Lowercase hexadecimal representation.

This formats numbers in lowercase hexadecimal, equivalent to the {:x} format specifier.

§

UpperHex

Uppercase hexadecimal representation.

This formats numbers in uppercase hexadecimal, equivalent to the {:X} format specifier.

§

Octal

Octal representation.

This formats numbers in octal (base-8), equivalent to the {:o} format specifier.

§

Ptr

Pointer address representation.

This formats pointer values as memory addresses, equivalent to the {:p} format specifier.

§

Bin

Binary representation.

This formats numbers in binary (base-2), equivalent to the {:b} format specifier.

§

LowExp

Lowercase exponential notation.

This formats floating-point numbers in scientific notation with lowercase ‘e’, equivalent to the {:e} format specifier.

§

UpperExp

Uppercase exponential notation.

This formats floating-point numbers in scientific notation with uppercase ‘E’, equivalent to the {:E} format specifier.

Trait Implementations§

Source§

impl Clone for FmtType

Source§

fn clone(&self) -> FmtType

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 FmtType

Source§

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

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

impl Display for FmtType

Source§

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

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

impl Copy for FmtType

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