Skip to content

Commit a9954f9

Browse files
committed
feat!: add Client::path and allow more path types for Client::chroot
1 parent 990965f commit a9954f9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/client/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod watcher;
22

3+
use std::borrow::Cow;
34
use std::future::Future;
45
use std::time::Duration;
56

@@ -212,6 +213,11 @@ impl Client {
212213
ChrootPath::new(self.chroot.as_ref(), path, true)
213214
}
214215

216+
/// Path of chroot.
217+
pub fn path(&self) -> &str {
218+
self.chroot.path()
219+
}
220+
215221
/// ZooKeeper session id.
216222
pub fn session_id(&self) -> SessionId {
217223
self.session.0
@@ -251,7 +257,7 @@ impl Client {
251257
///
252258
/// # Notable behaviors
253259
/// * Existing watchers are not affected.
254-
pub fn chroot(mut self, path: &str) -> std::result::Result<Client, Client> {
260+
pub fn chroot<'a>(mut self, path: impl Into<Cow<'a, str>>) -> std::result::Result<Client, Client> {
255261
if self.chroot.chroot(path) {
256262
Ok(self)
257263
} else {

tests/zookeeper.rs

+9
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ async fn test_chroot() {
448448
let cluster = format!("127.0.0.1:{}", zk_port);
449449
let client = zk::Client::connect(&cluster).await.unwrap();
450450

451+
assert_eq!(client.path(), "/");
452+
let client = client.chroot("abc").unwrap_err();
453+
assert_eq!(client.path(), "/");
454+
451455
let path = "/abc";
452456
let data = random_data();
453457
let child_path = "/abc/efg";
@@ -458,13 +462,18 @@ async fn test_chroot() {
458462
let (stat, _) = client.create(path, &data, &create_options).await.unwrap();
459463

460464
let path_client = client.clone().chroot(path).unwrap();
465+
assert_eq!(path_client.path(), path);
461466
assert_eq!((data, stat), path_client.get_data("/").await.unwrap());
462467

463468
let relative_child_path = child_path.strip_prefix(path).unwrap();
464469
let (child_stat, _) = path_client.create(relative_child_path, &child_data, &create_options).await.unwrap();
465470
assert_eq!((child_data.clone(), child_stat), path_client.get_data(relative_child_path).await.unwrap());
466471
assert_eq!((child_data.clone(), child_stat), client.get_data(child_path).await.unwrap());
467472

473+
let child_client = client.clone().chroot(child_path.to_string()).unwrap();
474+
assert_eq!(child_client.path(), child_path);
475+
assert_eq!((child_data.clone(), child_stat), child_client.get_data("/").await.unwrap());
476+
468477
let relative_grandchild_path = grandchild_path.strip_prefix(path).unwrap();
469478
let (_, grandchild_watcher) = client.check_and_watch_stat(grandchild_path).await.unwrap();
470479
let (_, relative_grandchild_watcher) = path_client.check_and_watch_stat(relative_grandchild_path).await.unwrap();

0 commit comments

Comments
 (0)