runtime-format

Crates.ioruntime-format
lib.rsruntime-format
version0.1.3
sourcesrc
created_at2023-01-25 18:17:21.026613
updated_at2023-05-01 15:44:03.424096
descriptionrust library for formatting dynamic strings
homepage
repositoryhttps://github.com/conradludgate/strfmt
max_upload_size
id767681
size23,424
Conrad Ludgate (conradludgate)

documentation

README

runtime-format

Formatting, but processed at runtime.

use runtime_format::{FormatArgs, FormatKey, FormatKeyError};
use core::fmt;

impl FormatKey for DateTime {
    fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
        use core::fmt::Write;
        match key {
            "year"    => write!(f, "{}", self.year()).map_err(FormatKeyError::Fmt),
            "month"   => write!(f, "{}", self.short_month_name()).map_err(FormatKeyError::Fmt),
            "day"     => write!(f, "{}", self.day()).map_err(FormatKeyError::Fmt),
            "hours"   => write!(f, "{}", self.hours()).map_err(FormatKeyError::Fmt),
            "minutes" => write!(f, "{}", self.minutes()).map_err(FormatKeyError::Fmt),
            "seconds" => write!(f, "{}", self.seconds()).map_err(FormatKeyError::Fmt),
            _ => Err(FormatKeyError::UnknownKey),
        }
    }
}
let now = DateTime::now();
let fmt = "{month} {day} {year} {hours}:{minutes}:{seconds}";
let args = FormatArgs::new(fmt, &now);

// Outputs "Jan 25 2023 16:27:53"
println!("{args}");
Commit count: 83

cargo fmt