GitXplorerGitXplorer
j

dynfmt

public
31 stars
5 forks
6 issues

Commits

List of commits on branch master.
Unverified
38860a6db203f30686ef8050bb117b20bf7fd5c9

Use GitHub Actions for CI

jjan-auer committed 4 years ago
Unverified
3a70a8cfde6ec386a7868e1151125a1f981f87ee

Fix lints and add Debug implementations

jjan-auer committed 4 years ago
Verified
6fe6ee476eb256643762e0d89fb2902975d64b6d

Merge pull request #3 from ZackPierce/use_maintained_error_lib

jjan-auer committed 4 years ago
Verified
39524a8e0385f08f4125980a70b0f037ef2e62c4

Io error variant should work with source but not impl From

ZZackPierce committed 4 years ago
Verified
012bd02d45ad6f91a0e9e2e97ebb9762ca1df310

Version bump and update changelog

ZZackPierce committed 4 years ago
Verified
641121c56e2ef09e7833c597c2686de53df25f7c

Do error derivation using a maintained and public library

ZZackPierce committed 4 years ago

README

The README file for this repository.

dynfmt - Dynamic Formatting in Rust

A crate for formatting strings dynamically.

dynfmt provides several implementations for formats that implement a subset of the std::fmt facilities. Parsing of the format string and arguments checks are performed at runtime. There is also the option to implement new formats.

The public API is exposed via the Format trait, which contains formatting helper functions and lower-level utilities to interface with format strings. See the Features section for a list of provided implementations.

Usage

use dynfmt::{Format, NoopFormat};

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

See the Format trait for more methods.

Features

This crate ships with a set of features that either activate formatting capabilities or new format implementations:

  • json (default): Implements the serialization of complex structures via JSON. Certain formats, such as Python, also have a representation format (%r) that makes use of this feature, if enabled. Without this feature, such values will cause an error.
  • python: Implements the printf-like format that python 2 used for formatting strings. See PythonFormat for more information.
  • curly: A simple format string syntax using curly braces for arguments. Similar to .NET and Rust, but much less capable. See SimpleCurlyFormat for mor information.

Extensibility

Implement the Format trait to create a new format. The only required method is iter_args, which must return an iterator over ArgumentSpec structs. Based on the capabilities of the format, the specs can be parameterized with formatting parameters.

use std::str::MatchIndices;
use dynfmt::{ArgumentSpec, Format, Error};

struct HashFormat;

impl<'f> Format<'f> for HashFormat {
    type Iter = HashIter<'f>;

    fn iter_args(&self, format: &'f str) -> Result<Self::Iter, Error<'f>> {
        Ok(HashIter(format.match_indices('#')))
    }
}

struct HashIter<'f>(MatchIndices<'f, char>);

impl<'f> Iterator for HashIter<'f> {
    type Item = Result<ArgumentSpec<'f>, Error<'f>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|(index, _)| Ok(ArgumentSpec::new(index, index + 1)))
    }
}

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

License: MIT