Skip to content

Commit 0cbe68f

Browse files
committed
Add a way to rename tags.
1 parent eb2f9fb commit 0cbe68f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/rpc/client/tags.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use std::ops::{Bound, RangeBounds};
1414

1515
use anyhow::Result;
16-
use futures_lite::{Stream, StreamExt};
16+
use futures_lite::{io, Stream, StreamExt};
1717
use quic_rpc::{client::BoxedConnector, Connector, RpcClient};
1818
use serde::{Deserialize, Serialize};
1919

@@ -221,6 +221,20 @@ where
221221
stream.next().await.transpose()
222222
}
223223

224+
/// Rename a tag
225+
///
226+
/// This is done in steps, so it is not atomic!
227+
pub async fn rename(&self, from: impl AsRef<[u8]>, to: impl AsRef<[u8]>) -> Result<()> {
228+
let from = from.as_ref();
229+
let to = to.as_ref();
230+
let Some(old) = self.get(from.as_ref()).await? else {
231+
return Err(io::Error::new(io::ErrorKind::NotFound, "Tag not found").into());
232+
};
233+
self.set(to.as_ref(), old.hash_and_format()).await?;
234+
self.delete(from.as_ref()).await?;
235+
Ok(())
236+
}
237+
224238
/// List a range of tags
225239
pub async fn list_range<R, E>(&self, range: R) -> Result<impl Stream<Item = Result<TagInfo>>>
226240
where
@@ -287,3 +301,13 @@ pub struct TagInfo {
287301
/// Hash of the data
288302
pub hash: Hash,
289303
}
304+
305+
impl TagInfo {
306+
/// Get the hash and format of the tag.
307+
pub fn hash_and_format(&self) -> HashAndFormat {
308+
HashAndFormat {
309+
hash: self.hash,
310+
format: self.format,
311+
}
312+
}
313+
}

0 commit comments

Comments
 (0)