Crate rt_format

Source
Expand description

Fully-runtime equivalent of the format! macro.

Allows formatting strings like the format! macro, with the formatting string and the arguments provided at runtime. This crate supports all the formatting features of the format! macro, except for the fill character.

§Examples

use rt_format::{Format, FormatArgument, ParsedFormat, Specifier};
use std::cmp::PartialEq;
use std::fmt;

#[derive(Debug, PartialEq)]
pub enum Variant {
    Int(i32),
    Float(f64),
}
 
impl FormatArgument for Variant {
    fn supports_format(&self, spec: &Specifier) -> bool {
        match self {
            Self::Int(_) => true,
            Self::Float(_) => match spec.format {
                Format::Display | Format::Debug | Format::LowerExp | Format::UpperExp => true,
                _ => false,
            },
        }
    }
 
    fn fmt_display(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::Display::fmt(&val, f),
            Self::Float(val) => fmt::Display::fmt(&val, f),
        }
    }
 
    fn fmt_debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
 
    fn fmt_octal(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::Octal::fmt(&val, f),
            _ => Err(fmt::Error),
        }
    }
 
    fn fmt_lower_hex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::LowerHex::fmt(&val, f),
            _ => Err(fmt::Error),
        }
    }
 
    fn fmt_upper_hex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::UpperHex::fmt(&val, f),
            _ => Err(fmt::Error),
        }
    }
 
    fn fmt_binary(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::Binary::fmt(&val, f),
            _ => Err(fmt::Error),
        }
    }
 
    fn fmt_lower_exp(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::LowerExp::fmt(&val, f),
            Self::Float(val) => fmt::LowerExp::fmt(&val, f),
        }
    }
 
    fn fmt_upper_exp(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Int(val) => fmt::UpperExp::fmt(&val, f),
            Self::Float(val) => fmt::UpperExp::fmt(&val, f),
        }
    }
 
    fn to_usize(&self) -> Result<usize, ()> {
        use std::convert::TryInto;
        match self {
            Variant::Int(val) => (*val).try_into().map_err(|_| ()),
            Variant::Float(_) => Err(()),
        }
    }
}
 
fn main() {
    use std::collections::HashMap;
 
    let pos_args = [Variant::Int(42), Variant::Int(5)];
 
    let mut named_args = HashMap::new();
    named_args.insert("foo".to_string(), Variant::Float(42.042));
 
    let args = ParsedFormat::parse("{:#x} [{0:<5}] {foo:.1$}", &pos_args, &named_args).unwrap();
    assert_eq!("0x2a [42   ] 42.04200", format!("{}", args));
}

Re-exports§

pub use crate::argument::FormatArgument;
pub use crate::argument::NoNamedArguments;
pub use crate::argument::NoPositionalArguments;
pub use crate::parser::ParsedFormat;
pub use crate::parser::Substitution;

Modules§

argument
Defines traits and types to help make arbitrary values formattable at runtime.
parser
Provides support for parsing typical Rust formatting strings.

Structs§

Specifier
The specification for the format of an argument in the formatting string.

Enums§

Align
Specifies the alignment of an argument with a specific width.
Format
Specifies how to format an argument.
Pad
Specifies whether a numeric argument with specific width should be padded with spaces or zeroes.
Precision
Specifies whether an argument should be formatted with a specific precision.
Repr
Specifies whether to use the alternate representation for certain formats.
Sign
Specifies whether the sign of a numeric argument should always be emitted.
Width
Specifies whether an argument should be padded to a specific width.

Functions§

format_value
Formats the given value using the given formatter and the given format specification.