Git Product home page Git Product logo

funcfmt's Introduction

funcfmt | Tests

funcfmt is a simple, lightweight templating library that allows templating using custom runtime context and function traits. It was originally created for exifrename, to allow efficiently processing a format and set of callbacks across thousands of EXIF objects.

Documentation

Documentation is available on docs.rs, or through cargo doc --open.

Usage

To add funcfmt to your dependencies:

cargo add funcfmt

The basic flow of funcfmt looks like this:

  1. Given a FormatMap<T> called formatters, call formatters.to_format_pieces(), which preprocesses everything into a FormatPieces<T>, where &T is what your callback function will take as its only argument. This allows avoiding having to reparse the formatters and go through the template each time things are processed.
  2. Call .render(data) on the FormatPieces<T>.

A very small example with Strings passed in, although you can pass an object of any type:

use funcfmt::{fm, FormatMap, Render, ToFormatPieces};

fn main() {
    let formatters = FormatMap::from([
        fm!("foo", |data| Some(format!("foo: {data}"))),
        fm!("bar", |data| Some(format!("bar: {data}"))),
        fm!("baz", |data| Some(format!("baz: {data}"))),
    ]);

    let fp = formatters.to_format_pieces("{foo}, {bar}").unwrap();

    // foo: some data, bar: some data
    let data_one = String::from("some data");
    println!("{}", fp.render(&data_one).unwrap());

    // foo: other data, bar: other data
    // note that this doesn't require processing the format again
    let data_two = String::from("other data");
    println!("{}", fp.render(&data_two).unwrap());
}

funcfmt's People

Contributors

cdown avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

funcfmt's Issues

Use &[u8]s

We don't really care about the contents of the strings -- we know it's valid coming in because it comes from a string, but after that we might as well be dealing with u8s.

Allow being called with traits

Right now attempting to do FormatMap<dyn SomeTrait> doesn't work because we need to relax Sized in many places. However, trying to do that then breaks more things, like Eq on Formatter<T>, which need looking at.

optim: Store indices instead of making new SmartStrings

About ~12% of time is spent in doing conversions from &str to SmartString, which may require a reallocation anyway.

  1. https://github.com/bodil/smartstring/blob/e407ca23c747257a812d2d2e70bf336412718c3a/src/lib.rs#L646
  2. (or if inline): https://github.com/bodil/smartstring/blob/e407ca23c747257a812d2d2e70bf336412718c3a/src/inline.rs#L83

Maybe we can just store the indices instead of converting to and from SmartStrings? We only need a Borrowed anyway at self.get() time. No idea if it would actually be faster.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.