Skip to content

sify21/gomod-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gomod-rs

LICENSE crates.io Version

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.

About

go.mod file parser with locations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages