| 1 | <h1 align="center">formatx</h1> |
| 2 | |
| 3 | <p align="center"> |
| 4 | <a href="https://crates.io/crates/formatx"> |
| 5 | <img src="https://img.shields.io/crates/d/formatx?style=flat-square"> |
| 6 | </a> |
| 7 | <a href="https://crates.io/crates/formatx"> |
| 8 | <img src="https://img.shields.io/crates/v/formatx?style=flat-square"> |
| 9 | </a> |
| 10 | <a href="https://github.com/clitic/formatx"> |
| 11 | <img src="https://img.shields.io/github/actions/workflow/status/clitic/formatx/ci.yml?logo=github&style=flat-square"> |
| 12 | </a> |
| 13 | <a href="https://docs.rs/formatx/latest/formatx"> |
| 14 | <img src="https://img.shields.io/docsrs/formatx?logo=docsdotrs&style=flat-square"> |
| 15 | </a> |
| 16 | <a href="https://github.com/clitic/formatx#license"> |
| 17 | <img src="https://img.shields.io/crates/l/formatx?style=flat-square"> |
| 18 | </a> |
| 19 | <a href="https://github.com/clitic/formatx"> |
| 20 | <img src="https://img.shields.io/github/repo-size/clitic/formatx?style=flat-square"> |
| 21 | </a> |
| 22 | </p> |
| 23 | |
| 24 | <p align="center">A macro for formatting non literal strings at runtime in Rust.</p> |
| 25 | |
| 26 | `formatx` is a dependency free string templating library with syntax derived from [std::fmt](https://doc.rust-lang.org/std/fmt/#syntax). formatx exports [formatx!](https://docs.rs/formatx/latest/formatx/macro.formatx.html) macro which is similar to [format!](https://doc.rust-lang.org/std/macro.format.html) macro. formatx works by first parsing the template string and then it uses `format!` macro internally to replicate it's behaviour. formatx aims for formatting strings and numbers although an generic type can also be formatted like an [struct](https://github.com/clitic/formatx/blob/main/examples/struct.rs). |
| 27 | |
| 28 | ## Getting Started |
| 29 | |
| 30 | Add this to your Cargo.toml file. |
| 31 | |
| 32 | ```toml |
| 33 | [dependencies] |
| 34 | formatx = "0.2.4" |
| 35 | ``` |
| 36 | |
| 37 | Or add from command line. |
| 38 | |
| 39 | ```bash |
| 40 | $ cargo add formatx |
| 41 | ``` |
| 42 | |
| 43 | See [docs](https://docs.rs/formatx/latest/formatx) and [examples](https://github.com/clitic/formatx.rs/tree/main/examples) to |
| 44 | know how to use it. |
| 45 | |
| 46 | ## Example |
| 47 | |
| 48 | SOURCE: [format! with non literal string](https://users.rust-lang.org/t/format-with-non-literal-string/2057) |
| 49 | |
| 50 | ```rust |
| 51 | use formatx::formatx; |
| 52 | |
| 53 | fn message(language: &str, name: &str, number: i32) -> String { |
| 54 | let s = match language { |
| 55 | "french" => "Bonjour {}, le nombre est {}", |
| 56 | "spanish" => "Hola {}, el numero es {}", |
| 57 | _ => "Hi {}, the number is {}", |
| 58 | }; |
| 59 | formatx!(s, name, number).unwrap() |
| 60 | } |
| 61 | |
| 62 | fn main() { |
| 63 | println!("{}", message("french", "Léa", 1)); |
| 64 | println!("{}", message("spanish", "Sofia", 2)); |
| 65 | println!("{}", message("english", "Ashley", 3)); |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | OUTPUT |
| 70 | |
| 71 | ``` |
| 72 | Bonjour Léa, le nombre est 1 |
| 73 | Hola Sofia, el numero es 2 |
| 74 | Hi Ashley, the number is 3 |
| 75 | ``` |
| 76 | |
| 77 | ## Syntax |
| 78 | |
| 79 | The main goal of `formatx` is to provide a almost identical syntax as of rust's format syntax. The syntax of `formatx` is identical to [std::fmt](https://doc.rust-lang.org/std/fmt/#syntax). You can also see gettext [documentation](https://www.gnu.org/software/gettext/manual/html_node/rust_002dformat.html), the rust's format syntax specifications is mentioned there too. |
| 80 | |
| 81 | ## Limitations |
| 82 | |
| 83 | > **Warning** |
| 84 | > Examples given below will always panic. |
| 85 | |
| 86 | 1. Only types which implements [Display](https://doc.rust-lang.org/std/fmt/trait.Display.html) + [Debug](https://doc.rust-lang.org/std/fmt/trait.Debug.html) traits are supported. Other [formatting-traits](https://doc.rust-lang.org/std/fmt/#formatting-traits) aren't supported. |
| 87 | |
| 88 | 2. Local variable interpolation isn't supported. |
| 89 | |
| 90 | ```rust |
| 91 | let people = "Rustaceans"; |
| 92 | formatx!("Hello {people}!").unwrap(); |
| 93 | ``` |
| 94 | |
| 95 | 3. Intermingling the two types of [positional](https://doc.rust-lang.org/std/fmt/#positional-parameters) specifiers isn't supported. |
| 96 | |
| 97 | ```rust |
| 98 | formatx!("{1} {} {0} {}", 1, 2).unwrap(); |
| 99 | ``` |
| 100 | |
| 101 | 4. Parameter setting through `$` sign argument isn't supported. |
| 102 | |
| 103 | ```rust |
| 104 | formatx!("{:width$}!", "x", width = 5).unwrap(); |
| 105 | ``` |
| 106 | |
| 107 | 5. An asterisk `.*` can't be used to set [precision](https://doc.rust-lang.org/std/fmt/#precision). |
| 108 | |
| 109 | ```rust |
| 110 | formatx!("{:.*}", 5, 0.01).unwrap(); |
| 111 | ``` |
| 112 | |
| 113 | ## Handling Of Positional Arguments |
| 114 | |
| 115 | Positional arguments are handled by an internal key which increments itself whenever an postional argument is passed through the macro. So, the behaviour is very different when compared to `format` macro. |
| 116 | |
| 117 | By default this internal key is set to `0` and when an positional argument is passed. `formatx` resolves the replacement by first checking if there is `0` key present in format string (eg. `Hello {0}`) or not, if the `0` is present then it replaces it, else if the `0` key is not present in the format string (eg.`Hello {}`) it replaces the first blank placeholder. After a replacement is made this internal key is increased by `1`. |
| 118 | |
| 119 | ```rust |
| 120 | asserteq!( |
| 121 | format!("{} {2} {} {1} {4} {} {3} {5} {6}", "zero", "one", "two", "three", "four", "five", "six"), |
| 122 | formatx!("{} {2} {} {1} {4} {} {3} {5} {6}", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight").unwrap() // This line won't panic |
| 123 | ); // This line will panic |
| 124 | ``` |
| 125 | |
| 126 | An error cannot be raised in such cases because `formatx` works at runtime and it doesn't know the future upcoming postitional arguments. So while designing your app keep this point in mind or simply use key value arguments. See issue [#7](https://github.com/clitic/formatx/issues/7) for more info. |
| 127 | |
| 128 | ## Handling Of Unused Arguments |
| 129 | |
| 130 | Unlike rust's built-in `format!` macro, which reports an error if any provided arguments are not used in the format string, `formatx!` allows unused arguments. This can be particularly useful in localization scenarios, where translations may or may not require certain arguments depending on grammatical rules. |
| 131 | |
| 132 | ## Alternatives |
| 133 | |
| 134 | 1. [strfmt](https://github.com/vitiral/strfmt) |
| 135 | 2. [runtime-fmt](https://github.com/SpaceManiac/runtime-fmt) |
| 136 | |
| 137 | ## License |
| 138 | |
| 139 | Dual Licensed |
| 140 | |
| 141 | - [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) ([LICENSE-APACHE](LICENSE-APACHE)) |
| 142 | - [MIT license](https://opensource.org/licenses/MIT) ([LICENSE-MIT](LICENSE-MIT)) |