Struct Netrc

Source
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

Source

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");
Source

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.

Source

pub fn get_credentials(&self, machine: &str) -> Option<Credentials>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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");
Source

pub fn to_netrc_string(&self) -> String

Serializes the Netrc struct to .netrc format.

Returns a string in the standard .netrc file format.

Source

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)?;
Source

pub fn set_credentials( &mut self, machine: &str, login: &str, password: &str, ) -> Result<(), NetrcError>

Source

pub fn remove_credentials(&mut self, machine: &str) -> Option<NetrcMachine>

Source

pub fn merge(&mut self, other: Netrc)

Trait Implementations§

Source§

impl Debug for Netrc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Netrc

Source§

fn default() -> Netrc

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Netrc

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Netrc

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Netrc

§

impl RefUnwindSafe for Netrc

§

impl Send for Netrc

§

impl Sync for Netrc

§

impl Unpin for Netrc

§

impl UnwindSafe for Netrc

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,