#credentials #parser #netrc

nightly netrc-parser

A Rust library for parsing and manipulating .netrc files

2 releases

Uses new Rust 2024

new 0.1.1 May 17, 2025
0.1.0 May 17, 2025

#309 in Authentication

36 downloads per month

MIT license

54KB
865 lines

netrc-parser

CI

A Rust library for parsing and manipulating .netrc files.

netrc-parser provides a modern, idiomatic parser for .netrc files, supporting machine entries, login credentials, accounts, and macro definitions (macdef). It includes serialization to JSON and TOML, file I/O, and comprehensive error handling.

Installation

Add to your Cargo.toml:

[dependencies]
netrc-parser = "0.1.0"

Usage

Parse a .netrc file and retrieve credentials:

use netrc_parser::{Netrc, NetrcError};

fn main() -> Result<(), NetrcError> {
    let path = dirs::home_dir()
        .ok_or_else(|| NetrcError::FileNotFound("Home directory not found".to_string()))?
        .join(".netrc");
    let netrc = Netrc::parse_from_path(&path)?;
    if let Some(creds) = netrc.get("surge.surge.sh") {
        println!("Login: {}, Password: {}", creds.login, creds.password);
    }
    Ok(())
}

Run the example

cargo run --example simple

Documentation

Run cargo doc --open to view the API documentation.

Development

Format code with rustfmt nightly

rustup toolchain install nightly
rustup component add rustfmt --toolchain nightly
cargo +nightly fmt

License

Licensed under the MIT. See LICENSE for details.

Contributing

Contributions are welcome! Please open a issue or pull request on Github

Dependencies

~4–12MB
~138K SLoC