#macro #timed #procedural #proc-macro #parser #split #inpt

macro inpt-macros

Procedural macro implementation for the inpt crate

2 releases

0.1.1 Nov 26, 2022
0.1.0 Sep 10, 2022

#16 in #timed

Download history 8/week @ 2024-08-31 3/week @ 2024-09-07 5/week @ 2024-09-14 25/week @ 2024-09-21 13/week @ 2024-09-28 3/week @ 2024-10-05 6/week @ 2024-10-12 2/week @ 2024-10-19 6/week @ 2024-10-26 13/week @ 2024-11-02 3/week @ 2024-11-09 3/week @ 2024-11-16 7/week @ 2024-11-23 18/week @ 2024-11-30 54/week @ 2024-12-07 30/week @ 2024-12-14

109 downloads per month
Used in inpt

MIT license

32KB
805 lines

Inpt

Inpt is a derive crate for dumb type-level text parsing.

Read the lastest documentation for more information.

Introduction

Imagine you need to chop up an annoying string and convert all the bits to useful types. You could write that sort of code by hand using split and from_str, but the boiler-plate of unwrapping and checking quickly looses all charm. Especially since that sort of parsing shows up a lot in timed programming competitions like advent of code.

Inpt tries to write that sort of parsing code for you, automatically splitting input strings based on field types and an optional regex. Inpt is absolutely not performant, strict, or formal. Whenever possible, it does the obvious thing:

#[inpt::main]
fn main(x: f32, y: f32) {
    println!("{}", x * y);
}
$ echo '6,7' | cargo run
42

Example

use inpt::{Inpt, inpt};

#[derive(Inpt)]
#[inpt(regex = r"(.)=([-\d]+)\.\.([-\d]+),?")]
struct Axis {
    name: char,
    start: i32,
    end: i32,
}

#[derive(Inpt)]
#[inpt(regex = "target area:")]
struct Target {
    #[inpt(after)]
    axes: Vec<Axis>,
}

impl Target {
    fn area(&self) -> i32 {
        self.axes.iter().map(|Axis { start, end, ..}| end - start).product()
    }
}


let target = inpt::<Target>("target area: x=119..176, y=-114..84").unwrap();
assert_eq!(target.area(), 11286);

Dependencies

~3.5MB
~95K SLoC