Giter VIP home page Giter VIP logo

serp's Introduction

Purpose

The rust format! macro requires the string template to be passed as a string literal at compile time. This crate uses the well known template language handlebars to allow for everything to be passed at runtime. Using the entirety of handlebars is definitely overkill for simply making an ergonomic string interpolation function, but I'm far more confident in the correctness of a well tested and popular library than I would be if I rolled my own. I make no modifications to the syntax of handlebars, so unfortunately I can't replicate the syntax of format! exactly.

Usage

You can initialize an empty template with Template::new() and then modify the template and/or push new key/value pairs. This isn't much more succinct than handlebars directly, but it's here for completeness.

let mut s = Serp::default();
s.template = "{{sample}} {{string}}".to_string();
s.push("sample".into(), "hello".into());
s.push("string".into(), "world".into());

assert_eq!(s.format(), "hello world");

The primary function is serp which takes a template as a String and a HashMap<String, String> of key/value pairs.

let template = "Hello, {{name}}".to_string();
let map = HashMap::from([("name".into(), "world".into())]);

assert_eq!(serp(&template, &map), "Hello, world".to_string());

The final methods are the final product, shorthand ways to use serp. t takes a template as a &str and an array of key/value tuples as a &[(&str, &str)]

let t = t("{{sample}} {{string}}", &[("sample", "Hello"), ("string", "World")]);
assert_eq!(t, "Hello World");

Finally, the laziest example, which uses a little convention to be both standard handlebars and succinct. a simply takes a template as a &str and an array of strings in &[&str]. The template brackets are numbered, 0 indexed.

let a = a("{{0}} {{1}}", &["Hello", "World"]);
assert_eq!(a, "Hello World");

License

Dual licensed under MIT and Apache 2.0

serp's People

Contributors

thedustinmiller avatar

Watchers

 avatar

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.