| 1 | /*! |
| 2 | The `netrc` crate provides a parser for the netrc file. |
| 3 | |
| 4 | The `reqwest-netrc` crate adds the support of netrc to the `reqwest` crate via the `reqwest-middleware` wrapper. |
| 5 | |
| 6 | # Setup |
| 7 | |
| 8 | ```text |
| 9 | $ crago add rust-netrc |
| 10 | ``` |
| 11 | |
| 12 | # Example |
| 13 | |
| 14 | ```no_run |
| 15 | use netrc::Netrc; |
| 16 | |
| 17 | // ... |
| 18 | |
| 19 | let nrc = Netrc::new().unwrap(); |
| 20 | |
| 21 | // ... |
| 22 | println!( |
| 23 | "login = {}\naccount = {}\npassword = {}", |
| 24 | nrc.hosts["my.host"].login, |
| 25 | nrc.hosts["my.host"].account, |
| 26 | nrc.hosts["my.host"].password, |
| 27 | ); |
| 28 | ``` |
| 29 | |
| 30 | */ |
| 31 | |
| 32 | pub use netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/::{Authenticatorrust-analyzer cargo rust-netrc 0.1.2 netrc/Authenticator#, Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#}; |
| 33 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/; |
| 34 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 35 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#; |
| 36 | #[cfg(windows)] |
| 37 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().; |
| 38 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 39 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/; |
| 40 | |
| 41 | mod lexrust-analyzer cargo rust-netrc 0.1.2 lex/; |
| 42 | mod netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/; |
| 43 | |
| 44 | pub type Resultrust-analyzer cargo rust-netrc 0.1.2 Result#<T> = resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo rust-netrc 0.1.2 Error#>; |
| 45 | |
| 46 | /// An error that can occur when processing a Netrc file. |
| 47 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(thiserrorrust-analyzer cargo thiserror 1.0.69 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.69 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 48 | pub enum Errorrust-analyzer cargo rust-netrc 0.1.2 Error# { |
| 49 | /// Wrap `std::io::Error` when we try to open the netrc file. |
| 50 | #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("I/O error: {0}")] |
| 51 | Iorust-analyzer cargo rust-netrc 0.1.2 Error#Io#(#[from] stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 52 | |
| 53 | /// Parsing error. |
| 54 | #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("{parser} in the file '{filename}'")] |
| 55 | Parsingrust-analyzer cargo rust-netrc 0.1.2 Error#Parsing# { |
| 56 | parserrust-analyzer cargo rust-netrc 0.1.2 Error#Parsing#parser.: netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/::ParsingErrorrust-analyzer cargo rust-netrc 0.1.2 netrc/ParsingError#, |
| 57 | filenamerust-analyzer cargo rust-netrc 0.1.2 Error#Parsing#filename.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | impl Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc# { |
| 62 | /// Create a new `Netrc` object. |
| 63 | /// |
| 64 | /// Look up the `NETRC` environment variable if it is defined else that the |
| 65 | /// default `~/.netrc` file. |
| 66 | pub fn newrust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]new().() -> Resultrust-analyzer cargo rust-netrc 0.1.2 Result#<Selfrust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]> { |
| 67 | Selfrust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]::get_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]get_file().() |
| 68 | .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo rust-netrc 0.1.2 Error#::Iorust-analyzer cargo rust-netrc 0.1.2 Error#Io#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 69 | ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#, |
| 70 | "no netrc file found", |
| 71 | ))) |
| 72 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|f| Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#::from_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]from_file().(f.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().())) |
| 73 | } |
| 74 | |
| 75 | /// Create a new `Netrc` object from a file. |
| 76 | pub fn from_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]from_file().(file: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo rust-netrc 0.1.2 Result#<Selfrust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]> { |
| 77 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_lossy().(&fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read().(file)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 78 | .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() |
| 79 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| Errorrust-analyzer cargo rust-netrc 0.1.2 Error#::Parsingrust-analyzer cargo rust-netrc 0.1.2 Error#Parsing# { |
| 80 | parserrust-analyzer cargo rust-netrc 0.1.2 Error#Parsing#parser.: e, |
| 81 | filenamerust-analyzer cargo rust-netrc 0.1.2 Error#Parsing#filename.: file.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | /// Search a netrc file. |
| 86 | /// |
| 87 | /// Look up the `NETRC` environment variable if it is defined else use the .netrc (or _netrc |
| 88 | /// file on windows) in the user's home directory. |
| 89 | pub fn get_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]get_file().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 90 | let env_var = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("NETRC") |
| 91 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<String>`]from().) |
| 92 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|f| shellexpandrust-analyzer cargo shellexpand 3.1.2 crate/::pathrust-analyzer cargo shellexpand 3.1.2 path/::tilderust-analyzer cargo shellexpand 3.1.2 path/funcs/tilde().(&f).into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().()); |
| 93 | |
| 94 | #[cfg(windows)] |
| 95 | let default = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("USERPROFILE") |
| 96 | .into_iter() |
| 97 | .flat_map(|home| repeat(home).zip([".netrc", "_netrc"])) |
| 98 | .map(|(home, file)| PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::from(home).join(filerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/file!)); |
| 99 | |
| 100 | #[cfg(not(windows))] |
| 101 | let default = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("HOME").maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|home| PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<String>`]from().(home).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(".netrc")); |
| 102 | |
| 103 | env_var.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][IntoIterator]into_iter().().chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(default).findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/chain/impl#[`Chain<A, B>`][Iterator]find().(|f| f.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #[cfg(test)] |
| 108 | mod testsrust-analyzer cargo rust-netrc 0.1.2 tests/ { |
| 109 | use superrust-analyzer cargo rust-netrc 0.1.2 crate/::*; |
| 110 | |
| 111 | const CONTENTrust-analyzer cargo rust-netrc 0.1.2 tests/CONTENT.: &str = "\ |
| 112 | machine cocolog-nifty.com |
| 113 | login jmarten0 |
| 114 | password cC2&yt7OT |
| 115 | |
| 116 | machine wired.com |
| 117 | login mstanlack1 |
| 118 | password gH4={wx=>VixU |
| 119 | |
| 120 | machine joomla.org |
| 121 | login mbutterley2 |
| 122 | password hY5>yKqU&$vq&0 |
| 123 | "; |
| 124 | |
| 125 | fn create_netrc_filerust-analyzer cargo rust-netrc 0.1.2 tests/create_netrc_file().() -> PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# { |
| 126 | let dest = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("mynetrc"); |
| 127 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().dest.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() { |
| 128 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/write().(&dest, CONTENTrust-analyzer cargo rust-netrc 0.1.2 tests/CONTENT.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 129 | } |
| 130 | dest |
| 131 | } |
| 132 | |
| 133 | fn check_nrcrust-analyzer cargo rust-netrc 0.1.2 tests/check_nrc().(nrc: &Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#) { |
| 134 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(nrc.hostsrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#hosts..lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().(), 3); |
| 135 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 136 | nrc.hostsrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#hosts.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()."cocolog-nifty.com"]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()., |
| 137 | Authenticatorrust-analyzer cargo rust-netrc 0.1.2 netrc/Authenticator#::newrust-analyzer cargo rust-netrc 0.1.2 netrc/impl#[Authenticator]new().("jmarten0", "", "cC2&yt7OT") |
| 138 | ); |
| 139 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 140 | nrc.hostsrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#hosts.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()."wired.com"]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()., |
| 141 | Authenticatorrust-analyzer cargo rust-netrc 0.1.2 netrc/Authenticator#::newrust-analyzer cargo rust-netrc 0.1.2 netrc/impl#[Authenticator]new().("mstanlack1", "", "gH4={wx=>VixU") |
| 142 | ); |
| 143 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 144 | nrc.hostsrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#hosts.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()."joomla.org"]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index()., |
| 145 | Authenticatorrust-analyzer cargo rust-netrc 0.1.2 netrc/Authenticator#::newrust-analyzer cargo rust-netrc 0.1.2 netrc/impl#[Authenticator]new().("mbutterley2", "", "hY5>yKqU&$vq&0") |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 150 | fn test_new_envrust-analyzer cargo rust-netrc 0.1.2 tests/test_new_env().() { |
| 151 | let fi = create_netrc_filerust-analyzer cargo rust-netrc 0.1.2 tests/create_netrc_file().(); |
| 152 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::set_varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/set_var().("NETRC", fi); |
| 153 | let nrc = Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#::newrust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 154 | check_nrcrust-analyzer cargo rust-netrc 0.1.2 tests/check_nrc().(&nrc); |
| 155 | } |
| 156 | |
| 157 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 158 | fn test_new_defaultrust-analyzer cargo rust-netrc 0.1.2 tests/test_new_default().() {} |
| 159 | |
| 160 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 161 | fn test_from_file_failedrust-analyzer cargo rust-netrc 0.1.2 tests/test_from_file_failed().() { |
| 162 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 163 | Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#::from_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]from_file().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().("/netrc/file/not/exists/on/no/netrc")) |
| 164 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().() |
| 165 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 166 | "I/O error: No such file or directory (os error 2)" |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 171 | fn test_from_filerust-analyzer cargo rust-netrc 0.1.2 tests/test_from_file().() { |
| 172 | let fi = create_netrc_filerust-analyzer cargo rust-netrc 0.1.2 tests/create_netrc_file().(); |
| 173 | let nrc = Netrcrust-analyzer cargo rust-netrc 0.1.2 netrc/Netrc#::from_filerust-analyzer cargo rust-netrc 0.1.2 impl#[Netrc]from_file().(fi.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 174 | check_nrcrust-analyzer cargo rust-netrc 0.1.2 tests/check_nrc().(&nrc); |
| 175 | } |
| 176 | } |