#suffix #proc-macro #unwrap #string #no-alloc

macro no-std parse-suffix

Process the string suffix as .parse::<suffix>().unwrap()

3 releases

new 0.1.2 Apr 18, 2025
0.1.1 Apr 18, 2025
0.1.0 Apr 18, 2025

#832 in Procedural macros

MIT license

7KB
113 lines

Process the string suffix as .parse::<suffix>().unwrap()

Examples

use std::{net::Ipv4Addr, path::PathBuf};

#[parse_suffix::parse_string_suffix]
fn test() {
    assert_eq!("23"i32, 23);
    assert_eq!("23"PathBuf, PathBuf::from("23"));
    assert_eq!("true"bool, true);
    assert_eq!("false"bool, false);
    assert_eq!("192.168.1.1"Ipv4Addr, Ipv4Addr::new(192, 168, 1, 1));
    assert_eq!(r"192.168.1.1"Ipv4Addr, Ipv4Addr::new(192, 168, 1, 1));
    assert_eq!(r#"192.168.1.1"#Ipv4Addr, Ipv4Addr::new(192, 168, 1, 1));
}

Expand to:

use std::{net::Ipv4Addr, path::PathBuf};

fn test() {
    assert_eq!("23".parse::<i32>().unwrap(), 23);
    assert_eq!("23".parse::<PathBuf>().unwrap(), PathBuf::from("23"));
    assert_eq!("true".parse::<bool>().unwrap(), true);
    assert_eq!("false".parse::<bool>().unwrap(), false);
    assert_eq!("192.168.1.1".parse::<Ipv4Addr>().unwrap(), Ipv4Addr::new(192, 168, 1, 1));
    assert_eq!(r"192.168.1.1".parse::<Ipv4Addr>().unwrap(), Ipv4Addr::new(192, 168, 1, 1));
    assert_eq!(r#"192.168.1.1"#.parse::<Ipv4Addr>().unwrap(), Ipv4Addr::new(192, 168, 1, 1));
}

No runtime deps