4 releases

0.1.3 Jan 10, 2024
0.1.2 Jan 10, 2024
0.1.1 Jan 8, 2024
0.1.0 Jan 8, 2024

#853 in Rust patterns

Download history 38/week @ 2024-06-17 37/week @ 2024-06-24 10/week @ 2024-07-01 14/week @ 2024-07-08 18/week @ 2024-07-15 37/week @ 2024-07-22 18/week @ 2024-07-29 22/week @ 2024-08-05 17/week @ 2024-08-12 17/week @ 2024-08-19 36/week @ 2024-08-26 21/week @ 2024-09-02 34/week @ 2024-09-09 26/week @ 2024-09-16 29/week @ 2024-09-23 72/week @ 2024-09-30

163 downloads per month
Used in 2 crates (via logfather)

MIT license

15KB
80 lines

Simplicio

Get rid of the annoying boilerplate in Rust and simplify creation of Strings and HashMaps.

Features

  • s!() can be used to:
    • create a new String
    • convert any value that implements the Display trait into a string
    • concatinate values together that implement the Display trait
    • concatinate while inserting a space between each value with the . prefix (e.g. s!(.a, b, c))
  • cnct!() is a wrapper around s!() for preferential purposes
  • map() creates a HashMap with initial values or from a Vec/array of tuple pairs
    • map!(k1 v1, k1 v2) | map!(k1: v1, k2:v2) | map!(k1 => v1, k2 => v2) | map!(k1 -> v1, k2 -> v2)
    • Path variables, if used as keys, are limited to =>, :, or [value] delimiters
    • Create a new HashMap with map!()
    • Can extend HashMaps

Getting Started

To start using Simplicio, add the following to your Cargo.toml:

[dependencies]
simplicio = "0.1.3"
  • Minimum supported Rust version: 1.56.1

Usage

String creation and concatination

use simplicio::{s, cnct};

// Creating an enum for example purposes
enum Enum { Value }  //Create the enum, Enum
impl std::fmt::Display for Enum {  // Implement the Display trait
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
         match self { Enum::Value => write!(f, "value"), }
    }
}

fn main() {
    assert_eq!(String::new(), s!()); // Make a new string
    assert_eq!(String::from("This is a String"), s!("This is a String")); // Stop using .to_string() or String::from()

    assert_eq!(true.to_string(), s!(true)); // Converts bools
    assert_eq!(123840.to_string(), s!(123840)); // Convert numbers fast boiiii
    assert_eq!(Enum::Value.to_string(), s!(Enum::Value)); // As long as it implements the ToString or Display traits, it will work

    let (a, b, c, d) = ("This", "is", "a", "String");
    assert_eq!(String::from("This is a String"), cnct!(.a, b, c, d)); // '.' prefix to automate spacing
    assert_eq!(s!(a, b, c, d), cnct!(a, b, c ,d)); // `cnct!()` is just a wrapper around `s!()`
}

Creating HashMaps

use simplicio::*;

fn main() {
    let mut tester = std::collections::HashMap::new();
    tester.insert("k1", "v1");
    tester.insert("k2", "v2");

    assert_eq!(map!("k1""v1", "k2" "v2"), tester);          // ' ' delimiter
    assert_eq!(map!("k1":"v1", "k2" : "v2"), tester);       // ':' delimiter
    assert_eq!(map!("k1"=>"v1", "k2" => "v2"), tester);     // '=>' delimiter
    assert_eq!(map!("k1"->"v1", "k2" -> "v2"), tester);     // '->' delimiter
    assert_eq!(map!("k1"["v1"], "k2" ["v2"]), tester);     // Key[Value]
    assert_eq!(map!([("k1", "v1"), ("k2", "v2")]), tester); // Similar to Hashmap::from(/*...*/)

    let vecmap = Vec::from([("k1", "v1"), ("k2", "v2")]);
    assert_eq!(map!(vecmap), tester); // Can convert a Vec<(_,_)>

    let arrmap = [("k1", "v1"), ("k2", "v2")];
    assert_eq!(map!(arrmap), tester); // Can convert an array [(_,_)]
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

._. why would you do this?

No runtime deps