Add tags chapter

This commit is contained in:
Jon Staab
2026-04-16 16:49:18 -07:00
parent 2553cff300
commit 8a29ff39d6
6 changed files with 901 additions and 13 deletions
+18 -10
View File
@@ -38,8 +38,16 @@ use sha2::{Digest, Sha256};
use std::fmt;
use crate::keys::PublicKey;
use crate::tags::Tags;
```
The `Tag` and `Tags` types are introduced in the next chapter. For
this chapter, treat `Tag` as a transparent wrapper around
`Vec<String>` and `Tags` as a transparent wrapper around `Vec<Tag>` —
serde sees them as bare arrays, the canonical form hashes identically,
and you can build them with `Tag::new("t", ["nostr"])` and
`Tags::from(vec![...])`.
## Errors
```rust {file=coracle-lib/src/events.rs}
@@ -104,12 +112,12 @@ metadata in the "could be added later" sense.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventContent {
pub content: String,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
}
impl EventContent {
pub fn new(content: impl Into<String>, tags: Vec<Vec<String>>) -> Self {
EventContent { content: content.into(), tags }
pub fn new(content: impl Into<String>, tags: impl Into<Tags>) -> Self {
EventContent { content: content.into(), tags: tags.into() }
}
}
```
@@ -126,7 +134,7 @@ which kinds mean what — that's the next layer of the stack.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventTemplate {
pub content: String,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
pub kind: u16,
}
@@ -151,7 +159,7 @@ it is.
pub struct StampedEvent {
pub content: String,
pub kind: u16,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
pub created_at: u64,
}
@@ -179,7 +187,7 @@ claims responsibility for.
pub struct OwnedEvent {
pub content: String,
pub kind: u16,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
pub created_at: u64,
pub pubkey: PublicKey,
}
@@ -216,7 +224,7 @@ fn canonical(
pubkey: &PublicKey,
created_at: u64,
kind: u16,
tags: &[Vec<String>],
tags: &Tags,
content: &str,
) -> String {
serde_json::json!([
@@ -235,7 +243,7 @@ fn canonical(
pub struct HashedEvent {
pub content: String,
pub kind: u16,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
pub created_at: u64,
pub pubkey: PublicKey,
pub id: [u8; 32],
@@ -271,7 +279,7 @@ wire.
pub struct Event {
pub content: String,
pub kind: u16,
pub tags: Vec<Vec<String>>,
pub tags: Tags,
pub created_at: u64,
pub pubkey: PublicKey,
pub id: [u8; 32],
@@ -423,7 +431,7 @@ impl<'de> Visitor<'de> for EventVisitor {
let mut pubkey: Option<String> = None;
let mut created_at: Option<u64> = None;
let mut kind: Option<u16> = None;
let mut tags: Option<Vec<Vec<String>>> = None;
let mut tags: Option<Tags> = None;
let mut content: Option<String> = None;
let mut sig: Option<String> = None;