Skip to content

Commit b9f37ad

Browse files
committed
refactor ConnectParams::get_cookie_values
return a struct with named fields instead of a tuple avoid panicking for index access in case the content of the file doesn't respect the format user:password avoid returning anyhow error, since this method may be used by downstream libraries
1 parent bada7eb commit b9f37ad

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,24 @@ pub struct ConnectParams {
6565
pub p2p_socket: Option<SocketAddrV4>,
6666
}
6767

68+
pub struct CookieValues {
69+
pub user: String,
70+
pub password: String,
71+
}
72+
6873
impl ConnectParams {
6974
/// Parses the cookie file content
70-
fn parse_cookie(content: String) -> anyhow::Result<(Option<String>, Option<String>)> {
71-
let values: Vec<&str> = content.splitn(2, ':').collect();
72-
Ok((Some(values[0].into()), Some(values[1].into())))
75+
fn parse_cookie(content: String) -> Option<CookieValues> {
76+
let values: Vec<_> = content.splitn(2, ':').collect();
77+
let user = values.first()?.to_string();
78+
let password = values.get(1)?.to_string();
79+
Some(CookieValues { user, password })
7380
}
7481

7582
/// Return the user and password values from cookie file
76-
pub fn get_cookie_values(&self) -> anyhow::Result<(Option<String>, Option<String>)> {
83+
pub fn get_cookie_values(&self) -> Result<Option<CookieValues>, std::io::Error> {
7784
let cookie = std::fs::read_to_string(&self.cookie_file)?;
78-
self::ConnectParams::parse_cookie(cookie)
85+
Ok(self::ConnectParams::parse_cookie(cookie))
7986
}
8087
}
8188

@@ -754,11 +761,10 @@ mod test {
754761
)
755762
.unwrap();
756763

757-
let result_values: (Option<String>, Option<String>) =
758-
bitcoind.params.get_cookie_values().unwrap();
764+
let result_values = bitcoind.params.get_cookie_values().unwrap().unwrap();
759765

760-
assert_eq!(user, result_values.0.unwrap().as_str());
761-
assert_eq!(password, result_values.1.unwrap().as_str());
766+
assert_eq!(user, result_values.user);
767+
assert_eq!(password, result_values.password);
762768
}
763769

764770
fn peers_connected(client: &Client) -> usize {

0 commit comments

Comments
 (0)