Use traits to keep event methods dry

This commit is contained in:
Jon Staab
2026-05-20 14:27:26 -07:00
parent 8ad0bc393b
commit d0709e1811
7 changed files with 138 additions and 61 deletions
+14 -12
View File
@@ -48,7 +48,7 @@ pub mod protected;
//! challenge followed by a pubkey comparison) needs a live, authenticated
//! connection and is handled in the relay layer.
use crate::events::{Event, HashedEvent};
use crate::events::HasTags;
use crate::tags::Tags;
```
@@ -91,23 +91,25 @@ impl Tag {
## Methods on event types
A caller holding an event wants to ask whether it is protected without
reaching for its tags directly. Both `HashedEvent` and `Event` carry a
`Tags` field, so the method facades delegate to the free function.
reaching for its tags directly. Following the pattern from expiration, the
query is an extension trait bounded on [`HasTags`], so one default method
serves every event type. With `coracle_lib::prelude::*` in scope,
`event.is_protected()` works on both `HashedEvent` and `Event`.
```rust {file=coracle-lib/src/protected.rs}
impl HashedEvent {
/// Protected-event queries on any event that carries tags.
pub trait EventExtensionProtected: HasTags {
/// Whether this event carries the NIP-70 protected marker.
pub fn is_protected(&self) -> bool {
is_protected(&self.tags)
fn is_protected(&self) -> bool {
is_protected(self.tags())
}
}
impl Event {
/// Whether this event carries the NIP-70 protected marker.
pub fn is_protected(&self) -> bool {
is_protected(&self.tags)
}
}
impl<T: HasTags> EventExtensionProtected for T {}
```
```rust {file=coracle-lib/src/prelude.rs}
pub use crate::protected::EventExtensionProtected;
```
## Usage patterns