| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 2 | use 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/::var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().; |
| 3 | 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/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 4 | 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/::{BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#}; |
| 5 | 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/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#; |
| 6 | |
| 7 | /// try to load a password from the various pgpass file locations |
| 8 | pub fn load_passwordrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password().( |
| 9 | host: &str, |
| 10 | port: u16, |
| 11 | username: &str, |
| 12 | database: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 13 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 14 | let custom_file = var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().("PGPASSFILE"); |
| 15 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file) = custom_file { |
| 16 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(password) = |
| 17 | load_password_from_filerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_file().(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<OsString>`]from().(file), host, port, username, database) |
| 18 | { |
| 19 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(password); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[cfg(not(target_os = "windows"))] |
| 24 | let default_file = homerust-analyzer cargo home 0.5.12 crate/::home_dirrust-analyzer cargo home 0.5.12 home_dir().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|path| path.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(".pgpass")); |
| 25 | #[cfg(target_os = "windows")] |
| 26 | let default_file = { |
| 27 | use etcetera::BaseStrategy; |
| 28 | |
| 29 | etcetera::base_strategy::Windows::new() |
| 30 | .ok() |
| 31 | .map(|basedirs| basedirs.data_dir().join("postgres").join("pgpass.conf")) |
| 32 | }; |
| 33 | load_password_from_filerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_file().(default_file?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()., host, port, username, database) |
| 34 | } |
| 35 | |
| 36 | /// try to extract a password from a pgpass file |
| 37 | fn load_password_from_filerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_file().( |
| 38 | path: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, |
| 39 | host: &str, |
| 40 | port: u16, |
| 41 | username: &str, |
| 42 | database: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 43 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 44 | let file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&path) |
| 45 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| { |
| 46 | match e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() { |
| 47 | 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#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound# => { |
| 48 | tracingrust-analyzer cargo tracing 0.1.44 crate/::debugrust-analyzer cargo tracing 0.1.44 macros/debug!!( |
| 49 | path = %path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 50 | "`.pgpass` file not found", |
| 51 | ); |
| 52 | } |
| 53 | _ => { |
| 54 | tracingrust-analyzer cargo tracing 0.1.44 crate/::warnrust-analyzer cargo tracing 0.1.44 macros/warn!!( |
| 55 | path = %path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 56 | "Failed to open `.pgpass` file: {e:?}", |
| 57 | ); |
| 58 | } |
| 59 | }; |
| 60 | }) |
| 61 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 62 | |
| 63 | #[cfg(target_os = "linux")] |
| 64 | { |
| 65 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/::PermissionsExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/PermissionsExt#; |
| 66 | |
| 67 | // check file permissions on linux |
| 68 | |
| 69 | let metadata = file.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]metadata().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 70 | let permissions = metadata.permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]permissions().(); |
| 71 | let mode = permissions.moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]mode().(); |
| 72 | if mode rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0o77 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 { |
| 73 | tracingrust-analyzer cargo tracing 0.1.44 crate/::warnrust-analyzer cargo tracing 0.1.44 macros/warn!!( |
| 74 | path = %path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 75 | permissions = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{mode:o}"), |
| 76 | "Ignoring path. Permissions are not strict enough", |
| 77 | ); |
| 78 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | let reader = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(file); |
| 83 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(reader, host, port, username, database) |
| 84 | } |
| 85 | |
| 86 | fn load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().( |
| 87 | mut reader: impl BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, |
| 88 | host: &str, |
| 89 | port: u16, |
| 90 | username: &str, |
| 91 | database: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 92 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 93 | let mut line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 94 | |
| 95 | // https://stackoverflow.com/a/55041833 |
| 96 | fn trim_newlinerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/trim_newline().(s: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) { |
| 97 | if s.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().('\n') { |
| 98 | s.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]pop().(); |
| 99 | if s.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().('\r') { |
| 100 | s.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]pop().(); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) = reader.read_linerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#read_line().(&mut line) { |
| 106 | if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | if line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('#') { |
| 111 | // comment, do nothing |
| 112 | } else { |
| 113 | // try to load password from line |
| 114 | trim_newlinerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/trim_newline().(&mut line); |
| 115 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(password) = load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().(&line, host, port, username, database) { |
| 116 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(password); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | line.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 121 | } |
| 122 | |
| 123 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 124 | } |
| 125 | |
| 126 | /// try to check all fields & extract the password |
| 127 | fn load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().( |
| 128 | mut line: &str, |
| 129 | host: &str, |
| 130 | port: u16, |
| 131 | username: &str, |
| 132 | database: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 133 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 134 | let whole_line = line; |
| 135 | |
| 136 | // Pgpass line ordering: hostname, port, database, username, password |
| 137 | // See: https://www.postgresql.org/docs/9.3/libpq-pgpass.html |
| 138 | match line.trim_startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start().().charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 139 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('#') => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 140 | _ => { |
| 141 | matches_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/matches_next_field().(whole_line, &mut line, host)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 142 | matches_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/matches_next_field().(whole_line, &mut line, &port.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 143 | matches_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/matches_next_field().(whole_line, &mut line, database.unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 144 | matches_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/matches_next_field().(whole_line, &mut line, username)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 145 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(line.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// check if the next field matches the provided value |
| 151 | fn matches_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/matches_next_field().(whole_line: &str, line: &mut &str, value: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<()> { |
| 152 | let field = find_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/find_next_field().(line); |
| 153 | match field { |
| 154 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field) => { |
| 155 | if field rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq()."*" || field rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq().value { |
| 156 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(()) |
| 157 | } else { |
| 158 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 159 | } |
| 160 | } |
| 161 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 162 | tracingrust-analyzer cargo tracing 0.1.44 crate/::warnrust-analyzer cargo tracing 0.1.44 macros/warn!!(line = whole_line, "Malformed line in pgpass file"); |
| 163 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// extract the next value from a line in a pgpass file |
| 169 | /// |
| 170 | /// `line` will get updated to point behind the field and delimiter |
| 171 | fn find_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/find_next_field().<'a>(line: &mut &'a str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>> { |
| 172 | let mut escaping = false; |
| 173 | let mut escaped_string = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 174 | let mut last_added = 0; |
| 175 | |
| 176 | let char_indicies = line.char_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]char_indices().(); |
| 177 | for (idx, c) in char_indicies { |
| 178 | if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().':' && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().escaping { |
| 179 | let (field, rest) = line.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(idx); |
| 180 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().line = &rest[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 181 | |
| 182 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut escaped_string) = escaped_string { |
| 183 | escaped_string rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().+=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().&field[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().last_added..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 184 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(escaped_string)); |
| 185 | } else { |
| 186 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(field)); |
| 187 | } |
| 188 | } else if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\\' { |
| 189 | let s = escaped_string.get_or_insert_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]get_or_insert_with().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().); |
| 190 | |
| 191 | if escaping { |
| 192 | s.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\\'); |
| 193 | } else { |
| 194 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().s rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().+=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AddAssign<&str>`]add_assign().&line[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().last_added..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#idx]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 195 | } |
| 196 | |
| 197 | escaping = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().escaping; |
| 198 | last_added = idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 199 | } else { |
| 200 | escaping = false; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 205 | } |
| 206 | |
| 207 | #[cfg(test)] |
| 208 | mod testsrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/ { |
| 209 | use superrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/::{find_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/find_next_field()., load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line()., load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().}; |
| 210 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 211 | |
| 212 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 213 | fn test_find_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_find_next_field().() { |
| 214 | fn test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().<'a>(mut input: &'a str, result: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>, rest: &str) { |
| 215 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(find_next_fieldrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/find_next_field().(&mut input), result); |
| 216 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input, rest); |
| 217 | } |
| 218 | |
| 219 | // normal field |
| 220 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().("foo:bar:baz", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#("foo")), "bar:baz"); |
| 221 | // \ escaped |
| 222 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().( |
| 223 | "foo\\\\:bar:baz", |
| 224 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#("foo\\".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())), |
| 225 | "bar:baz", |
| 226 | ); |
| 227 | // : escaped |
| 228 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().( |
| 229 | "foo\\::bar:baz", |
| 230 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#("foo:".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())), |
| 231 | "bar:baz", |
| 232 | ); |
| 233 | // unnecessary escape |
| 234 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().( |
| 235 | "foo\\a:bar:baz", |
| 236 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#("fooa".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())), |
| 237 | "bar:baz", |
| 238 | ); |
| 239 | // other text after escape |
| 240 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().( |
| 241 | "foo\\\\a:bar:baz", |
| 242 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#("foo\\a".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())), |
| 243 | "bar:baz", |
| 244 | ); |
| 245 | // double escape |
| 246 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().( |
| 247 | "foo\\\\\\\\a:bar:baz", |
| 248 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#("foo\\\\a".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())), |
| 249 | "bar:baz", |
| 250 | ); |
| 251 | // utf8 support |
| 252 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().("🦀:bar:baz", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#("🦀")), "bar:baz"); |
| 253 | |
| 254 | // missing delimiter (eof) |
| 255 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().("foo", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, "foo"); |
| 256 | // missing delimiter after escape |
| 257 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().("foo\\:", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, "foo\\:"); |
| 258 | // missing delimiter after unused trailing escape |
| 259 | test_caserust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_case().("foo\\", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, "foo\\"); |
| 260 | } |
| 261 | |
| 262 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 263 | fn test_load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_load_password_from_line().() { |
| 264 | // normal |
| 265 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 266 | load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().( |
| 267 | "localhost:5432:bar:foo:baz", |
| 268 | "localhost", |
| 269 | 5432, |
| 270 | "foo", |
| 271 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar") |
| 272 | ), |
| 273 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 274 | ); |
| 275 | // wildcard |
| 276 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 277 | load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().("*:5432:bar:foo:baz", "localhost", 5432, "foo", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar")), |
| 278 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 279 | ); |
| 280 | // accept wildcard with missing db |
| 281 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 282 | load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().("localhost:5432:*:foo:baz", "localhost", 5432, "foo", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 283 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 284 | ); |
| 285 | |
| 286 | // doesn't match |
| 287 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 288 | load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().( |
| 289 | "thishost:5432:bar:foo:baz", |
| 290 | "thathost", |
| 291 | 5432, |
| 292 | "foo", |
| 293 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar") |
| 294 | ), |
| 295 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 296 | ); |
| 297 | // malformed entry |
| 298 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 299 | load_password_from_linerust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_line().( |
| 300 | "localhost:5432:bar:foo", |
| 301 | "localhost", |
| 302 | 5432, |
| 303 | "foo", |
| 304 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar") |
| 305 | ), |
| 306 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 311 | fn test_load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/tests/test_load_password_from_reader().() { |
| 312 | let file = b"\ |
| 313 | localhost:5432:bar:foo:baz\n\ |
| 314 | # mixed line endings (also a comment!)\n\ |
| 315 | *:5432:bar:foo:baz\r\n\ |
| 316 | # trailing space, comment with CRLF! \r\n\ |
| 317 | thishost:5432:bar:foo:baz \n\ |
| 318 | # malformed line \n\ |
| 319 | thathost:5432:foobar:foo\n\ |
| 320 | # missing trailing newline\n\ |
| 321 | localhost:5432:*:foo:baz |
| 322 | "; |
| 323 | |
| 324 | // normal |
| 325 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 326 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(&mut &file[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., "localhost", 5432, "foo", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar")), |
| 327 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 328 | ); |
| 329 | // wildcard |
| 330 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 331 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(&mut &file[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., "localhost", 5432, "foo", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("foobar")), |
| 332 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 333 | ); |
| 334 | // accept wildcard with missing db |
| 335 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 336 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(&mut &file[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., "localhost", 5432, "foo", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 337 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("baz".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 338 | ); |
| 339 | |
| 340 | // doesn't match |
| 341 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 342 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(&mut &file[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., "thathost", 5432, "foo", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("foobar")), |
| 343 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 344 | ); |
| 345 | // malformed entry |
| 346 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 347 | load_password_from_readerrust-analyzer cargo sqlx-postgres 0.8.3 options/pgpass/load_password_from_reader().(&mut &file[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., "thathost", 5432, "foo", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("foobar")), |
| 348 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 349 | ); |
| 350 | } |
| 351 | } |