2 releases

0.1.1 May 23, 2024
0.1.0 May 23, 2024

#675 in Parser implementations

Download history 234/week @ 2024-05-20

234 downloads per month

MIT license

65KB
2K SLoC

gomod-rs

A go.mod file parser with location information.

Implemented using nom and nom_locate.

No string copy/clone during parsing.

(except for interpreted strings, which yield Identifier::Interpreted(String) type)

Example Usage

Here is an example printing all requirements defined in a go.mod file, along with their locations and related contents.

use gomod_rs::{parse_gomod, Context, Directive};

let contents = r#"module example.com/my/thing

go 1.12

require (
    example.com/other/thing v1.0.2
    example.com/new/thing/v2 v2.3.4
)

exclude example.com/old/thing v1.2.3
replace example.com/bad/thing v1.4.5 => example.com/good/thing v1.4.5
retract [v1.9.0, v1.9.5]"#;
let gomod = parse_gomod(&contents)?;
gomod
    .iter()
    .filter_map(|i| match i {
        Context {
            value: Directive::Require { specs },
            ..
        } => Some(specs),
        _ => None,
    })
    .for_each(|require_specs| {
        require_specs.iter().for_each(|spec| {
            println!(
                "Requirement {{name: {}, version: {}}} at line {}, fragment: {}",
                spec.value.0,
                &spec.value.1 as &str,
                spec.range.0.line,
                &contents[spec.range.0.offset..spec.range.1.offset]
            );
        });
    });

Above will ouput:

Requirement {name: example.com/other/thing, version: v1.0.2} at line 6, fragment: example.com/other/thing v1.0.2

Requirement {name: example.com/new/thing/v2, version: v2.3.4} at line 7, fragment: example.com/new/thing/v2 v2.3.4

You can also cargo run --example parse -- /path/to/go.mod.

Dependencies

~1MB
~22K SLoC