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:
| FmtType | Format Specifier | Description |
|---|---|---|
| Default | (none) | Default formatting for the type |
| Debug | ? | Debug representation |
| DebugLowHex | x? | Debug representation with lowercase hexadecimal |
| DebugUpHex | X? | Debug representation with uppercase hexadecimal |
| LowerHex | x | Lowercase hexadecimal |
| UpperHex | X | Uppercase hexadecimal |
| Octal | o | Octal representation |
| Ptr | p | Pointer address |
| Bin | b | Binary representation |
| LowExp | e | Lowercase exponential notation |
| UpperExp | E | Uppercase 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.