pub struct Netrc {
pub machines: HashMap<String, NetrcMachine>,
}Expand description
Represents a complete .netrc file with multiple machine entries.
Stores machine entries in a HashMap keyed by machine name for efficient
lookup. Provides methods for parsing, serialization, and manipulation of
entries.
Fields§
§machines: HashMap<String, NetrcMachine>Implementations§
Source§impl Netrc
impl Netrc
Sourcepub fn parse_from_str(input: &str) -> Result<Self, NetrcError>
pub fn parse_from_str(input: &str) -> Result<Self, NetrcError>
Parses a .netrc string into a Netrc struct.
Returns a Netrc containing all machine entries or a NetrcError if
parsing fails.
§Example
use netrc_parser::{Netrc, NetrcError};
let input = "machine example.com login user password pass";
let netrc = Netrc::parse_from_str(input)?;
assert_eq!(netrc.get("example.com").unwrap().login, "user");Sourcepub fn parse_from_path<P: AsRef<Path>>(path: P) -> Result<Self, NetrcError>
pub fn parse_from_path<P: AsRef<Path>>(path: P) -> Result<Self, NetrcError>
Reads and parses a .netrc file from the given path.
Checks file permissions (must be 0600 or stricter on Unix) and returns a
Netrc struct. Returns a NetrcError for I/O or parsing errors.
§Example
use netrc_parser::{Netrc, NetrcError};
use std::fs;
let temp_file = std::env::temp_dir().join("test_netrc_doc");
fs::write(&temp_file, "machine example.com login user password pass")?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&temp_file, fs::Permissions::from_mode(0o600))?;
}
let netrc = Netrc::parse_from_path(&temp_file)?;
if let Some(creds) = netrc.get("example.com") {
println!("Login: {}", creds.login);
}
fs::remove_file(&temp_file)?;§Note
On Unix systems, the file must have permissions set to 0600 (owner
read/write only). Files with more permissive settings (e.g., group
or world readable) will result in NetrcError::InsecurePermissions.
Sourcepub fn get(&self, machine: &str) -> Option<&NetrcMachine>
pub fn get(&self, machine: &str) -> Option<&NetrcMachine>
Retrieves a machine entry by its name.
Returns Some(&NetrcMachine) if found, or None if no entry exists.
Sourcepub fn to_json(&self) -> Result<String, NetrcError>
pub fn to_json(&self) -> Result<String, NetrcError>
Serializes the Netrc struct to JSON format.
Returns a pretty-printed JSON string or a NetrcError if serialization
fails.
Sourcepub fn to_toml(&self) -> Result<String, NetrcError>
pub fn to_toml(&self) -> Result<String, NetrcError>
Serializes the Netrc struct to TOML format.
Returns a pretty-printed TOML string or a NetrcError if serialization
fails.
Sourcepub fn insert_machine(&mut self, machine: NetrcMachine)
pub fn insert_machine(&mut self, machine: NetrcMachine)
Inserts or replaces a machine entry in the Netrc.
Overwrites any existing entry with the same machine name.
Sourcepub fn remove_machine(&mut self, machine_name: &str) -> Option<NetrcMachine>
pub fn remove_machine(&mut self, machine_name: &str) -> Option<NetrcMachine>
Removes a machine entry by name.
Returns the removed NetrcMachine if found, or None if no entry
exists.
Sourcepub fn update_machine<F>(
&mut self,
machine_name: &str,
update_fn: F,
) -> Result<(), NetrcError>where
F: FnOnce(&mut NetrcMachine),
pub fn update_machine<F>(
&mut self,
machine_name: &str,
update_fn: F,
) -> Result<(), NetrcError>where
F: FnOnce(&mut NetrcMachine),
Updates a machine entry with the provided function.
Applies the closure to the entry if found, returning Ok(()) on success
or NetrcError::NotFound if no entry exists.
§Example
use netrc_parser::{Netrc, NetrcError, NetrcMachine};
let mut netrc = Netrc::default();
netrc.insert_machine(NetrcMachine {
machine: "example.com".to_string(),
login: "user".to_string(),
password: "pass".to_string(),
account: None,
macdef: None,
});
netrc.update_machine("example.com", |m| m.login = "new_user".to_string())?;
assert_eq!(netrc.get("example.com").unwrap().login, "new_user");Sourcepub fn to_netrc_string(&self) -> String
pub fn to_netrc_string(&self) -> String
Serializes the Netrc struct to .netrc format.
Returns a string in the standard .netrc file format.
Sourcepub fn save_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), NetrcError>
pub fn save_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), NetrcError>
Saves the .netrc content to the specified path.
Writes the serialized .netrc content to the given file path, setting
permissions to 0600 (owner read/write only) on Unix systems. Returns
Ok(()) on success or a NetrcError for I/O errors.
§Example
use netrc_parser::{Netrc, NetrcError, NetrcMachine};
use std::fs;
let temp_file = std::env::temp_dir().join("test_netrc_save");
let mut netrc = Netrc::default();
netrc.insert_machine(NetrcMachine {
machine: "example.com".to_string(),
login: "user".to_string(),
password: "pass".to_string(),
account: None,
macdef: None,
});
netrc.save_to_path(&temp_file)?;
let loaded = Netrc::parse_from_path(&temp_file)?;
assert_eq!(loaded.get("example.com").unwrap().login, "user");
fs::remove_file(&temp_file)?;