Skip to content

Commit 26cf71b

Browse files
committed
Add FromStr for ConnectionInfo
1 parent 739692f commit 26cf71b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/connection.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22
use std::io::{self, Write};
33
use std::net::{self, TcpStream, ToSocketAddrs};
44
use std::path::PathBuf;
5-
use std::str::from_utf8;
5+
use std::str::{from_utf8, FromStr};
66
use std::time::Duration;
77

88
use crate::cmd::{cmd, pipe, Pipeline};
@@ -101,6 +101,14 @@ pub struct ConnectionInfo {
101101
pub passwd: Option<String>,
102102
}
103103

104+
impl FromStr for ConnectionInfo {
105+
type Err = RedisError;
106+
107+
fn from_str(s: &str) -> Result<Self, Self::Err> {
108+
s.into_connection_info()
109+
}
110+
}
111+
104112
/// Converts an object into a connection info struct. This allows the
105113
/// constructor of the client to accept connection information in a
106114
/// range of different formats.

tests/test_basic.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::let_unit_value)]
22

3-
use redis::{Commands, ConnectionLike, ControlFlow, PubSubCommands};
3+
use redis::{Commands, ConnectionInfo, ConnectionLike, ControlFlow, PubSubCommands};
44

55
use std::collections::{BTreeMap, BTreeSet};
66
use std::collections::{HashMap, HashSet};
@@ -20,6 +20,11 @@ fn test_parse_redis_url() {
2020
assert!(redis::parse_redis_url("127.0.0.1").is_err());
2121
}
2222

23+
#[test]
24+
fn test_redis_url_fromstr() {
25+
let _info: ConnectionInfo = "redis://127.0.0.1:1234/0".parse().unwrap();
26+
}
27+
2328
#[test]
2429
fn test_args() {
2530
let ctx = TestContext::new();

0 commit comments

Comments
 (0)