#regex #serde #group #deserialize #capture #type-safe #recap

macro recap-derive

Derives FromStr impl for types which are then deserialized using recap

3 releases

0.1.2 Dec 28, 2021
0.1.1 Apr 21, 2019
0.1.0 Apr 14, 2019

#91 in #type-safe

Download history 162/week @ 2024-08-31 114/week @ 2024-09-07 271/week @ 2024-09-14 246/week @ 2024-09-21 163/week @ 2024-09-28 239/week @ 2024-10-05 340/week @ 2024-10-12 83/week @ 2024-10-19 85/week @ 2024-10-26 168/week @ 2024-11-02 33/week @ 2024-11-09 111/week @ 2024-11-16 180/week @ 2024-11-23 189/week @ 2024-11-30 212/week @ 2024-12-07 177/week @ 2024-12-14

767 downloads per month
Used in recap

MIT license

7KB
127 lines

recap Main Software License crates.io Released API docs Master API docs

deserialize named capture groups into typesafe structs

Recap is provides what envy provides for environment variables, for named capture groups. Named regex capture groups are like any other regex capture group but have the extra property that they are associated with name. i.e (?P<name-of-capture-group>some-pattern)

🤔 who is this for

You may find this crate useful for cases where your application needs to extract information from string input provided by a third party that has a loosely structured format.

A common usecase for this is when you are dealing with log file data that was not stored in a particular structured format like JSON, but rather in a format that can be represented with a pattern.

You may also find this useful parsing other loosely formatted data patterns.

This crate would be less appropriate for cases where your input is provided in a more structured format, like JSON. I recommend using a crate like serde-json for those cases instead.

📦 install

Add the following to your Cargo.toml file.

[dependencies]
recap = "0.1"

🤸 usage

A typical recap usage looks like the following. Assuming your Rust program looks something like this...

💡 These examples use Serde's derive feature

use recap::Recap;
use serde::Deserialize;
use std::error::Error;

#[derive(Debug, Deserialize, Recap)]
#[recap(regex = r#"(?x)
    (?P<foo>\d+)
    \s+
    (?P<bar>true|false)
    \s+
    (?P<baz>\S+)
  "#)]
struct LogEntry {
    foo: usize,
    bar: bool,
    baz: String,
}

fn main() -> Result<(), Box<dyn Error>> {
    let logs = r#"1 true hello
  2 false world"#;

    for line in logs.lines() {
        let entry: LogEntry = line.parse()?;
        println!("{:#?}", entry);
    }

    Ok(())
}

👭 Consider this crate a cousin of envy, a crate for deserializing environment variables into typesafe structs.

Doug Tangren (softprops) 2019

Dependencies

~3.5–4.5MB
~91K SLoC