Giter Site home page Giter Site logo

rust-sformat-dynamic's Introduction

sformat-dynamic

Dynamic string formatting for Rust.

This crate tries to mimic the std::fmt syntax as closely as possible. One notable differentiation thus far is that the dynamic string format only supports named parameters like {name}, and not positional parameters like {}.

Always use the compile-time format! macro if you can. There are some situations where you might need a dynamic string formatting engine, for example if you need a lightweight library way for users to specify their own formats in a config file or something similar. Again, using dynamic string formatting is far less secure than using the compile-time format! macro, so always prefer that!

This is still a WIP, so use at your own risk!

Examples

See ./sformat-dynamic/examples for more examples.

// Note: This example requires the "derive" feature.
use sformat_dynamic::{compile, derive::Context};

#[derive(Context)]
struct ContextImpl {
  value: usize
}

let context = ContextImpl { value: 99 };
let format = compile("Value is {value:+010}").unwrap();
let formatted = format.format_str(&context).unwrap();

assert_eq!(formatted, "Value is +000000099");

You can also use a HashMap when specifying the context, although it is significantly more verbose.

use sformat_dynamic::{compile, TypedValue};
use std::collections::HashMap;

let context = HashMap::from([
    ("name", TypedValue::Str("Ferris"))
]);
let format = compile("Hello {name: >16}!").unwrap();
let formatted = format.format_str(&context).unwrap();

assert_eq!(formatted, "Hello           Ferris!")

Feature Parity

Consult the str::fmt documentation for what these features actually mean.

Feature Implemented Future Plan to Implement
Named Argument {name} N/A
Positional Argument {}
Fill / Alignment < , ^ , > N/A
Sign Flag + N/A
Alternate Form Flag # 🤔
Zero Flag 0 N/A
Precision - Fixed .N N/A
Precision - Arg .N$
Precision - Astrix .*

Derive Types

A table of types that can be derived using sformat-dynamic-derive.

Type Implemented Future Plan to Implement
&T : Debug
&T : Display
&str
isize N/A
i64 N/A
i32 N/A
i16 N/A
i8 N/A
usize N/A
u64 N/A
u32 N/A
u16 N/A
u8 N/A
f64 N/A
f32 N/A
bool N/A

License

Licensed under either of

at your option.

rust-sformat-dynamic's People

Contributors

ferristseng avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.