Skip to content

Commit

Permalink
nostr: add mutable methods to Tags struct
Browse files Browse the repository at this point in the history
Add `push`, `pop`, `insert` and `remove` methods to `Tags` struct.

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Feb 11, 2025
1 parent 65d35a3 commit 706404c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* nostr: manually impl eq and cmp traits for `RelayUrl` ([Yuki Kishimoto])
* nostr: use `Cow` in `ClientMessage` and `RelayMessage` ([Yuki Kishimoto])
* nostr: derive `PartialOrd`, `Ord`, and `Hash` traits in `Nip21` enum ([Yuki Kishimoto])
* nostr: add `push`, `pop`, `insert` and `remove` methods to `Tags` struct ([Yuki Kishimoto])
* pool: take event reference in `send_event` methods ([Yuki Kishimoto])
* pool: use the relay ingester to perform actions ([Yuki Kishimoto])
* pool: avoid spawning a task for every authentication request ([Yuki Kishimoto])
Expand Down
77 changes: 77 additions & 0 deletions crates/nostr/src/event/tag/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,76 @@ impl Tags {
self.list.is_empty()
}

/// Appends a [`Tag`] to the back of the collection.
///
/// Check [`Vec::push`] doc to learn more.
///
/// This erases the [`TagsIndexes`].
pub fn push(&mut self, tag: Tag) {
// Erase indexes
self.erase_indexes();

// Append
self.list.push(tag);
}

/// Removes the last [`Tag`] and returns it, or `None` if it's empty.
///
/// Check [`Vec::pop`] doc to learn more.
///
/// This erases the [`TagsIndexes`].
pub fn pop(&mut self) -> Option<Tag> {
// Erase indexes
self.erase_indexes();

// Pop last item
self.list.pop()
}

/// Inserts a [`Tag`] at position `index` within the vector,
/// shifting all tags after it to the right.
///
/// Returns `true` if the [`Tag`] is inserted successfully.
/// Returns `false` if `index > len`.
///
/// Check [`Vec::insert`] doc to learn more.
///
/// This erases the [`TagsIndexes`].
pub fn insert(&mut self, index: usize, tag: Tag) -> bool {
// Check if `index` is bigger than collection len
if index > self.list.len() {
return false;
}

// Erase indexes
self.erase_indexes();

// Insert at position
self.list.insert(index, tag);

// Inserted successfully
true
}

/// Removes and returns the [`Tag`] at position `index` within the vector,
/// shifting all tags after it to the left.
///
/// Check [`Vec::remove`] doc to learn more.
///
/// This erases the [`TagsIndexes`].
pub fn remove(&mut self, index: usize) -> Option<Tag> {
// Check if `index` is bigger than collection len
if index > self.list.len() {
return None;
}

// Erase indexes
self.erase_indexes();

// Remove from collection
Some(self.list.remove(index))
}

/// Get first tag
#[inline]
pub fn first(&self) -> Option<&Tag> {
Expand Down Expand Up @@ -242,6 +312,13 @@ impl Tags {
idx
}

#[inline]
fn erase_indexes(&mut self) {
if self.indexes.get().is_some() {
self.indexes = OnceCell::new();
}
}

/// Get indexes
#[inline]
pub fn indexes(&self) -> &TagsIndexes {
Expand Down

0 comments on commit 706404c

Please sign in to comment.